On Tuesday, April 16, 2019 at 4:04:55 PM UTC-6, José Valim wrote:
>
> Elixir already has a perfectly fine syntax via curly brackets for creating 
> ok and error tuples, that also works on matching, so my recommendation is 
> still to build an extra vocabulary in your app or as a separate library.
>

Quite agree.  Though one thing that I'd find immensely useful is a way to 
match something out of a pipeline or error otherwise.  Like currently there 
is a `match?/2` function, it would be nice to have a generic `match/2` 
function as well, which would take a matchspec (either erlang's, with of 
course a nice elixir builder, or whatever...) and the value and it returns 
back in the form as specified.  Perhaps as a thought it could work like:

```elixir
# Via a matchspec that builds based on an `fn` similar to erlang's parse 
transform:
iex(1)> match({:ok, 6.28}, fn {:ok, v} -> [v] end)
[6.28]

iex(2)> match({:error, "blah"}, fn {:ok, v} -> [v] end)
** (MatchError) no match of right hand side value: {:error, "blah"}

# Or maybe have a match/3 instead with a kind an anon-fun syntax with a 
match/2 fallback to return all values, either by itself if 1 or as a tuple 
containing all?
iex(3)> match({:ok, 6.28}, {:ok, _})
6.28

# &# syntax requires the output argument
iex(4)> match({:ok, 6.28}, {:ok, &1}, &1)
6.28

iex(4)> match({:ok, 6.28}, {:ok, &1}, [&1])
[6.28]
```
Of which a pipeline usage could look like:
```elixir
things
|> might_fail()
|> match({:ok, _})
|> assert(6.28)
```
Compared to the usual (or even larger and more wordy forms):
```elixir
things
|> might_fail()
|> case do {:ok, v} -> v end
|> assert(6.28)
```
Consequently the `match/2,3` macro can be implement fairly easily by just 
falling down to a `case` expression with some rewriting of variable names 
for ease of use (or don't, force the user to 'name' out the matcher parts 
they want to use, though I like just `_` as a single-out nice bit, 
consequently this is how I implemented it in the shell for this quick test).

Potential other alternative name might be something like `match_out`, but I 
personally like `match` as the corresponding part of `match?`.

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elixir-lang-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/0684320c-b881-4cf5-8b96-601ee291c207%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to