Allow guards in `with` and `for` matches #4015

Closed
zambal opened this Issue Nov 30, 2015 · 7 comments

Projects

None yet

3 participants

@zambal
Contributor
zambal commented Nov 30, 2015

As discussed here:
https://groups.google.com/d/msg/elixir-lang-core/lzNhT87-XUU/Wn4EANOkEQAJ

This would make patterns like this possible:

iex> with x when x < 2 <- 4, do: :ok
4

iex> with x when x > 2 <- 4, do: :ok
:ok

and

iex> for x when x < 2 <- 1..4, do: x
[1]
@lexmag lexmag self-assigned this Nov 30, 2015
@zambal
Contributor
zambal commented Nov 30, 2015

BTW, I tried to make a pull request myself, but having no experience with the Erlang side of Elixir's implementation, all the stuff going on in in elixir_exp:expand/2 is a bit too advanced for me at the moment to implement this idiomatically. However, if I can help out with writing tests or updating documentation, please let me know!

@lexmag
Member
lexmag commented Nov 30, 2015

I'm wondering, shall we support guards in for though? If so, we'll have two ways for quite the same thing:

for x when x < 2 <- 1..4, do: x
# same as we have today
for x <- 1..4, x < 2, do: x

Note that the way we have today isn't real guard and accepts any expression.
Otherwise, I think we could make it consistent with with by having real guards and treating any "bare" expression as just an expression inside.
@josevalim what are your thoughts on it?

Hey @zambal, we could team-up on it if you'd like, I could ping you when initial implementation will be done. :bowtie:

@zambal
Contributor
zambal commented Nov 30, 2015

@lexmag Yes, I like :) Please ping me when I can help.

@josevalim
Member
@lexmag lexmag added this to the v1.3.0 milestone Dec 29, 2015
@lexmag
Member
lexmag commented Feb 15, 2016

@zambal ping. :)
I got working guards in for, and they are available in with but lacking documentation.

@lexmag
Member
lexmag commented Feb 26, 2016

Documented in #4334.

@lexmag lexmag closed this Feb 26, 2016
@OnorioCatenacci OnorioCatenacci added a commit to OnorioCatenacci/elixir that referenced this issue Mar 10, 2016
@josevalim @OnorioCatenacci josevalim + OnorioCatenacci Support guards in with (related to #4015) bfea59c
@OnorioCatenacci OnorioCatenacci added a commit to OnorioCatenacci/elixir that referenced this issue Mar 15, 2016
@josevalim @OnorioCatenacci josevalim + OnorioCatenacci Support guards in with (related to #4015) c1f676e
@zambal
Contributor
zambal commented May 19, 2016

@lexmag Sorry for missing the ping. My GitHub activity was almost non existing for the last couple of months. But awesome to see that this has been implemented in the meanwhile :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment