8000 How to optionally match? · Issue #22 · alehander92/gara · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
How to optionally match? #22
Open
Open
@bluenote10

Description

@bluenote10

Coming from Scala's pattern matching, I'm a bit confused what the library is doing. In Scala, pattern matching performs validation, and whether something matches is optional. Picking up on the email example, how can I actually validate whether the input type satisfies the criteria for unwrapping the values?

match("not a valid email string"):
  email(name, domain):
    echo &"Valid email: {name}@{domain}"
  _:
    echo "not a valid email"

Specifically, how should I express that s.split('@', 1) may or may not work? Is it only possible to unwrap things unconditionally with this library or is there a mechanism for expression optional matches?

My expectation from Scala were to write an unapply function (what is called unpackers here), which can return either

  • a bool for direct value matching
  • an Option[T] for unwrapping a single value, or
  • an Option[(T, ...)] for unwrapping multiple values.
    For matching an email for instance, the unapply implementation would get an input string, and return some((name, domain)) only if s.split('@', 1) is valid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0