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

bug passing all args from macro to function #3630

Closed
kostya opened this issue Dec 3, 2016 · 2 comments
Closed

bug passing all args from macro to function #3630

kostya opened this issue Dec 3, 2016 · 2 comments

Comments

@kostya
Copy link
Contributor

kostya commented Dec 3, 2016

def bla1(*args1, **args2)
  p args1
  p args2
end

macro bla2(*args1, **args2)
  bla1(*{{args1}}, **{{args2}})
end

bla2(1, a: 1) # => ok
bla2(1, 2) # => Syntax error in expanded macro: bla2:1: for empty hashes use '{} of KeyType => ValueType'
bla2(a: 1) # => Syntax error in expanded macro: bla2:1: for empty hashes use '{} of KeyType => ValueType'
@asterite
Copy link
Member

asterite commented Dec 3, 2016

You can splat inside a macro. And in master you can now double splat inside macros. It's still a bit annoying because if args1 is empty there will be a comma at the beginning and it will fail to parse, but it's still possible.

Solution 1:

macro bla2(*args1, **args2)
  {% if args1.empty? %}
    bla1({{**args2}})
  {% else %}
    bla1({{*args1}}, {{**args2}})
  {% end %}
end

Solution 2:

macro bla2(*args1, **args2)
  bla1(
    {% unless args1.empty? %}
      {{*args1}},
    {% end %}
    {{**args2}}
    )
end

We also have an argify method which the same as splatting (*), maybe we can add an argument to it that is a string to append if it's not empty. So...

macro bla2(*args1, **args2)
  bla1({{args1.argify(", ")}} {{**args2}})
end

What do you think? I needed that argify method with an optional trailing comma a couple of times now. We should probably renamed it to splat, though, and have a double_splat method too.

@asterite
Copy link
Member

asterite commented Dec 3, 2016

I just added those methods.

To be honest, I'm not super happy with the way macros currently work (or are implemented), though they usually get the work done. Before 1.0 we'll discuss better alternatives to implement them.

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

2 participants