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

Add .each and .each_with_index to various macro types #9120

Merged

Conversation

Blacksmoke16
Copy link
Member

@Blacksmoke16 Blacksmoke16 commented Apr 18, 2020

Since #9091 is in a holding pattern; I figured it would good to at least add some of macro methods that it was adding. I was actually able to reimplement my DI refactor (minus the compiler passes) with just this PR, and a lot of duplication. This will satisfy my needs until #9091 can go through a proper design phase.

@RX14
Copy link
Member

RX14 commented Apr 19, 2020

Isn't this redundant with macro {% for?

Though I'm happy to add this, I'd rather have one or the other eventually, not both

@Blacksmoke16
Copy link
Member Author

Blacksmoke16 commented Apr 19, 2020

Isn't this redundant with macro {% for?

No because:

{% begin %}
  {% hash = {"one" => {data: [[2, 2, 2], 2, 3]}, "two" => {data: [4, 5, 6]}} %}

  {% for key, value in hash %}
    {% arguments = value[:data].map do |v|
         if v.is_a? ArrayLiteral
           c = 0
           # Can't iterate over the inner array since you cant nest macro expressions
           c
         else
           v + 1
         end
       end %}
    {{pp arguments}}
  {% end %}
{% end %}

This enables that. I'm sure you could rewrite this to use {% for .. but It would be quite an unwieldy beast.

EDIT: It's more needed for iterating over HashLiterals, since you can't use .map as a workaround like you can with array or tuples.

@Blacksmoke16
Copy link
Member Author

Can I get another review on this?

@straight-shoota straight-shoota added this to the 0.35.0 milestone Apr 26, 2020
@Blacksmoke16
Copy link
Member Author

Anything left to do? Would be nice to get this into the nightly build.

@straight-shoota straight-shoota merged commit 7210610 into crystal-lang:master Apr 27, 2020
@straight-shoota
Copy link
Member

Thanks @Blacksmoke16

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

Successfully merging this pull request may close these issues.

4 participants