Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could this library use normal for comprehension syntax for generators instead of something custom? #28

Open
myronmarston opened this issue Jul 9, 2016 · 1 comment

Comments

@myronmarston
Copy link

myronmarston commented Jul 9, 2016

The current generator syntax--e.g. for_all x in int--requires that I learn a new syntax, but it sure looks similar to Elixir's existing for comprehension syntax. Is there any reason that can't be used instead?

For example, looking over the examples in the readme:

property :square do
  for_all x in int, do: x * x >= 0
end

Could be:

property :squre do
  for x <- int, do: x * x >= 0
end

For a more involved example involving a such_that like:

for_all {x, y} in such_that({xx, yy} in {int, int} when xx < yy) do
  x < y
end

Could perhaps be:

for x <- int, y <- int, x < y do
  x < y
end

I just tried ExCheck for the first time today so I'm sure there are aspects of it's API I'm not understanding, but it would greatly reduce the learning curve if my existing knowledge of Elixir's for comprehensions could be used instead of learning a new API.

@luc-tielen
Copy link
Contributor

It should be possible since all those constructs are based upon macros which basically expand the language. I think parroty made them the way they are so the API looks similar to triq syntax, but this syntax does indeed look really nice and intuitive.

A quick experiment using macros:

Interactive Elixir (1.3.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> quote do: for x <- [1,2,3], do: x
{:for, [], [{:<-, [], [{:x, [], Elixir}, [1, 2, 3]]}, [do: {:x, [], Elixir}]]}
iex(2)> quote do: for x <- [1,2,3], x < 2, do: x
{:for, [],
[{:<-, [], [{:x, [], Elixir}, [1, 2, 3]]},
{:<, [context: Elixir, import: Kernel], [{:x, [], Elixir}, 2]},
[do: {:x, [], Elixir}]]}

It should be possible with the power of macros.
Maybe we could even keep the constructs we have now and just build these macros on top of them in order to reduce the amount of work..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants