alehander92 / gara

MIT License
103 stars 4 forks source link

How to optionally match? #22

Open bluenote10 opened 5 years ago

bluenote10 commented 5 years ago

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

alehander92 commented 5 years ago

i agree this might make more sense, i think i intended to use e.g. empty seq / invalid values for cases when i want to make sure the unpacker doesnt match, but it's not a good solution indeed.

i still find Option[<T: having length/index>] simpler than having both Option[T] and Option[(T, ..)] but i suspect the single value case might be a sensible optimisation..