You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This causes an inconvenience of always having to use positional method to create Procs, but moreover it makes it impossible to create Procs to methods that have required named args (e.g. def a(*, x)).
The text was updated successfully, but these errors were encountered:
Proc itself must have a double splat generic parameter for this to happen. Alternatively you can do some tricks with a trailing NamedTuple parameter:
# F = Proc(*T, NT, R)structNamedProc(F, T, NT, R)
protecteddefinitialize(*, __proc@proc : F)
enddefself.new(proc : *T, NT->R) forallT, NT, R
{%unlessNT<NamedTuple%}
{%raise"'proc' must end with a NamedTuple parameter"%}
{%end%}
NamedProc(Proc(*T, NT, R), T, NT, R).new(__proc: proc)
enddefcall(*args : *T, **opts : **NT) : R@proc.call(*args, opts)
endendmacronamed_proc(method, *args, **opts)
NamedProc.new(->(
{%for arg, i in args %}
__arg{{ i }} : ({{ arg }}),
{%end%}
{%unless opts.empty? %}
__named_args : {{ opts }}
{%end%}
) do
{{ method.id }}(
{%for arg, i in args %}
__arg{{ i }},
{%end%}
{%unless opts.empty? %}
**__named_args
{%end%}
)
end)
{% debug %}
enddefa(x, *, y)
x * y
end
f = named_proc(:a, Int32, y:Int32)
f.call(3, y:4) # => 12
g = named_proc("a", x:Int32, y:Int32)
g.call(x:5, y:6) # => 30
g.call(y:10, x:8) # => 80
Procs that have named parameters will never be C functions (but then not all C functions are Crystal Procs either). Another thing is they are needed for blocks with named parameters / yields with named arguments.
Discussion
Hello, asking first to see if this is a bug or an intended feature.
Consider the following where a Proc is created as a reference to a method. Creating it with positional arguments works, but not with named arguments:
(https://play.crystal-lang.org/#/r/brpw)
This causes an inconvenience of always having to use positional method to create Procs, but moreover it makes it impossible to create Procs to methods that have required named args (e.g.
def a(*, x)
).The text was updated successfully, but these errors were encountered: