-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Continue with anonymous block arguments #8764
Comments
No, it shows up in the API docs if you use |
Oh my bad. The API docs already represent this correctly. I'd still prefer always specifying |
It seems Ruby eventually wants to require For context, Ruby 3.1 allows a bare def foo1(&)
yield
end
def bar1(&)
foo1(&)
end
bar1 { 1 } # => 1
def foo2(&block)
block.call
end
def bar2(&)
proc(&) # captures anonymous block
foo2(&)
end
bar2 { 1 } # => 1 I don't know whether anonymous block forwarding is exactly equivalent to a named one which would capture the block. It is definitely not a syntactic transformation though. Also of note is that module Foo
def a1(&); end
def b1(&block); end
def c1(& : ->); end
def d1(&block : ->); end
def a2(&); yield; end
def b2(&block); yield; end
def c2(& : ->); yield; end
def d2(&block : ->); yield; end
def b3(&block); block.call; end
def d3(&block : ->); block.call; end
end
{% Foo.methods.map(&.block_arg) %} # => [, , , , , , , , block, block : (->)]
{% Foo.methods.select(&.block_arg.is_a?(Arg)).map(&.name) %} # => [b3, d3] Likewise, when the |
i don't agree with this idea, if yield exists in method body, block must be provided when invoke this method, add a extra & in method parameter is more clear way reveal this. Also see following code for a really use case need to improve in std-lib. Lines 48 to 55 in 771bddc
See discuss here, A extra & in the pamameter will make issue like this more clearly, user don't need search |
Preparing for Crystal 1.8.0 crystal-lang/crystal#8764
Follows up on #8117. This PR added syntax for anonymous block arguments
def foo(&)
. The idea is to use this syntax exclusively for yielding methods.Currently it's valid to completely omit the block argument when there's
yield
in the body. This hides the yielding nature of the method from the signatureand API docs. Such an essential property should be made visible.After the initial syntax was added, we switched usage in stdlib in #8394.
The next step would be to deprecate some of the currently valid method signatures. The idea as proposed in #8117:
&
means a non-captured block argument: because it doesn't have a name there's no way to capture it&block
means a captured block argument, and the compiler will verify that it's used in the semantic phase (because it might be mentioned inside a macro)&block
without a mention will give a compile error suggesting to either change it to&
to avoid a closure or to mention it somehowyield
and the method signature doesn't have&
the compiler will tell youThis would be a breaking change for yielding methods with a named block argument or no block argument at all, and also for methods with a named block argument that's never used in the literal scope of its body. But it improves clarity and helps to better identify the behaviour of block arguments.
Part of #7157
The text was updated successfully, but these errors were encountered: