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

Rest (*) character not showing up in macros #5735

Open
watzon opened this issue Feb 21, 2018 · 3 comments
Open

Rest (*) character not showing up in macros #5735

watzon opened this issue Feb 21, 2018 · 3 comments

Comments

@watzon
Copy link
Contributor

watzon commented Feb 21, 2018

Crystal::Macro::Def#args is supposed to list all arguments, but if you include a rest character it only shows up as an empty string.

Example: https://carc.in/#/r/3m9b

@bew
Copy link
Contributor

bew commented Feb 22, 2018

@watzon a simple wrapper with splat (*) support: https://carc.in/#/r/3mc6

The original idea was to correctly wrap a method, and generate a new method with the exact same signature. But there doesn't seem to be a way without using a lot of boilerplate like:

macro wrap(def_node)
  {% splat_no = def_node.splat_index %}
  def {{ def_node.name }}_wrapped(
    {% for arg, i in def_node.args %}
      {% if i == splat_no %}*{% end %}{{ arg }},
    {% end %}
    {% if double_splat = def_node.double_splat %}
      **{{ double_splat }},
    {% end %}
    {% if block_arg = def_node.block_arg %}
      &{{ block_arg }}
    {% end %}
  )
    # wrapper content!
  end
end

wrap def foo(a : String, *rest : Bool, named : String)
end

wrap def bar(a : String, *, named : String, **kwargs)
end

wrap def baz(arg : Bool, **kwargs, &block : String ->)
end

https://carc.in/#/r/3mc7 (the generated def doesn't parse correctly, fixed in #5737)

It feels overkill to do that just to wrap a method..
It would be nice to have a SplatArg & DoubleSplatArg (and BlockArg?) in the array from def_node.args, that would have the correct to_s for their kind. This way we would only need to do *def_node.args to replicate the def_node's signature.

@watzon
Copy link
Contributor Author

watzon commented Feb 22, 2018

Thanks @bew, I figured you'd be able to describe it a bit better

@HertzDevil
Copy link
Contributor

Instead of changing the AST grammar, perhaps it is better to have this as part of #13082

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

No branches or pull requests

4 participants