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

[Experimental] Compiler: replace &->proc with block #9197

Closed

Conversation

asterite
Copy link
Member

@asterite asterite commented Apr 28, 2020

Before this PR

For example before this PR you can do:

["foo.cr", "bar.cr"].find &->File.exists?(String)

That's passing the method File.exists?(String) as a block argument to find.

However, having to specify the type is a bit redundant: we already know that the block arg is a string, and that it's just one argument.

One can also write:

["foo.cr", "bar.cr"].find { |arg| File.exists?(arg) }

which is the same. However, one has to mention arg.

After this PR

This PR essentially rewrites lets you write this:

["foo.cr", "bar.cr"].find &->File.exists?

and it just works. It gets rewritten to:

["foo.cr", "bar.cr"].find { |arg| File.exists?(arg) }

It adds as many arguments to the call as expressions are yielded in find (or any other method).

This also works with &->foo and &->obj.foo. For example (probably not a nice example :-P):

x = 10
p [1, 2, 3].map &->x.+ # => [11, 12, 13]

s = "a"
p [2, 3, 4].map &->s.rjust # => [" a", "  a", "   a"]

Why experimental?

Because I don't know if it's intuitive.

What do you think?

Why are you sending this?

I see in gitter a lot of people use &->, and having to specify the types is not nice.

Well... mmm... maybe it's just Kai Leahy that does that, I don't know 😊

@jkthorne
Copy link
Contributor

This seems really convenient but I have not used it directly.

@j8r
Copy link
Contributor

j8r commented Apr 28, 2020

Looks great.
One use case is registering procs on the Grip framework, which is Phoenix-like web framework. It will prevent using macros for this task.

@Blacksmoke16
Copy link
Member

@j8r Isn't that syntax ->method_name? This PR is related to the block version of ->, i.e. &->.

@j8r
Copy link
Contributor

j8r commented Apr 29, 2020

I'm not wrong @Blacksmoke16. Currently, you're right – but we are working with @grkek to merge our web frameworks.

@asterite
Copy link
Member Author

Probably too cryptic and not general enough

@asterite asterite closed this Apr 29, 2020
@asterite asterite deleted the magic-block-arg-proc-pointer branch April 29, 2020 02:21
@tenebrousedge
Copy link

Not having to specify types is nice, but it would be best if specifying a type were optional. I don't see this as being more cryptic than &.

@jhass
Copy link
Member

jhass commented Apr 29, 2020

My biggest gripe here would be the inconsistency with passing a method to a proc argument, especially around C interfaces. But I agree we could try to find improvements here for the passing a method as a block usecases.

@RX14
Copy link
Contributor

RX14 commented Apr 29, 2020

I've thought about this very optimization before, and inferring the argument types. So I'd definitely be in favour of merging this

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

Successfully merging this pull request may close these issues.

None yet

7 participants