On Thursday, August 25, 2016 at 9:13:00 AM UTC-6, José Valim wrote:
>
> (while using `=` instead of `<-` while having the same features, and more)
>>
>
> Coincidentally that's the one thing I dislike about happy: the overriding
> of the operator =. <- was made to be used exactly in situations such like
> this and that's why it has no semantics outside of existing constructs. :)
>
Heh, that is one of the things that I really do like about happy, it
unifies the 'matching' to be fully powerful in all cases. For example
instead of just doing:
```elixir
{:ok, str} = get_a_string_or_whatever() # Does not make sure `str`
is_binary
# -- or --
case get_a_string_or_whatever() do
{:ok, str} when is_binary(str) -> :blah # This makes sure `str`
is_binary, but wow is it wordy
end
# -- or happy's way --
{:ok, str} when is_binary(str) = get_a_string_or_whatever() # Enforce
entire matching semantics, inline!
```
I do like `<-` and `->` but when I see them I think of list stuff (I'm an
old erlang programmer) and certainly not inline matching enforcement. Like
using `<-` in for's makes sense to me, you are creating a new looping
'scope', where `=`'s do not. I think the same thing in with/happy, the <-
is creating a new looping scope, except that it isn't, there is no loop, no
enumeration work, instead it just doing doing a match/when, which should
just be done and supported by `=` everywhere as `=` is a matching construct.
Personally, I'd change Elixir so that `=` supports `when`, just naturally
and everywhere, and this is in fact my main usage of `happy_path`, to
enforce a specific match or fail out. I would also make `<-` work only on
comprehension-style things, where something will be looped (I have a few
ideas for this too), and `->` only for case-style blocks, although
personally I'd probably make case something like:
```elixir
case something() do
{:ok, str} when is_binary(str) do
something_in_a_body()
end
{:ok, _}, do: {:error, :invalid_type}
{:err, reason} = e, do: e
end
```
Although personally not with 'do', but that is an easy to match construct
in elixir, but the thing is that it makes defined blocks with a do/end
instead of arbitrary `->` that just happens to end when another `->` is
encountered. It seems magical in how those rules work (even though I know
how the parsing works internally).
Err, too much of a tangent, in essence: `with` using `<-` makes me think
comprehension-loops, not matching, and it bugs the ever-living-tar out of
me especially since `=` is already the matching operator (just needs to be
buffed to support `when`). ^.^
On Thursday, August 25, 2016 at 9:22:38 AM UTC-6, Drew Olson wrote:
>
> Has there been consideration of adding a "happy-like" macro version of
> with (that has the same syntax as happy, but with <-)? I've also
> implemented something like this locally for my projects, I find it reads
> more pleasantly.
>
> Obviously, this works fine for me but if many folks are interested in this
> syntactic form, perhaps including the alternative in elixir itself makes
> sense.
>
Not done yet, but the author is *very* responsive and if asked I'm sure he
would add such a variant (maybe call it `happy_with`?). I really really
*really* hate using `<-` as a matching construct, that is `=`'s job. ^.^
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/84fdb667-0d75-4aea-a821-d6e6491b2983%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.