Reflection in macros

105 views
Skip to first unread message

Jason Pellerin

unread,
Aug 17, 2015, 9:25:55 AM8/17/15
to Crystal
I'm pretty sure I'm doing this wrong: http://play.crystal-lang.org/#/r/bqq and reflection of this kind (finding the base generic types of an array of nested arrays) (yes, still trying to make Iterator.flatten work) shouldn't require using regexes to pry apart stringifed type params. But I don't see another way to do this, using the existing type reflection facilities available in macros. What am I missing? Is there a nicer way to do this, or does the macro system maybe need more functions for traversing and introspecting nodes and sets of nodes?

jhp

Ben Miller

unread,
Aug 17, 2015, 10:44:09 AM8/17/15
to Crystal
The ASTNode returned by TypeNode#type_params is an ArrayLiteral(TypeNode), so even though it isn't a "proper" array literal (I got a lot of parse errors while looking at this), we have access to ArrayLiteral#map and ArrayLiteral#argify, so it is possible to at least eliminate the regexes: http://play.crystal-lang.org/#/r/bqy

On Mon, Aug 17, 2015 at 9:25 AM Jason Pellerin <j...@stripe.com> wrote:
I'm pretty sure I'm doing this wrong: http://play.crystal-lang.org/#/r/bqq and reflection of this kind (finding the base generic types of an array of nested arrays) (yes, still trying to make Iterator.flatten work) shouldn't require using regexes to pry apart stringifed type params. But I don't see another way to do this, using the existing type reflection facilities available in macros. What am I missing? Is there a nicer way to do this, or does the macro system maybe need more functions for traversing and introspecting nodes and sets of nodes?

jhp

--
You received this message because you are subscribed to the Google Groups "Crystal" group.
To unsubscribe from this group and stop receiving emails from it, send an email to crystal-lang...@googlegroups.com.
To post to this group, send email to crysta...@googlegroups.com.
Visit this group at http://groups.google.com/group/crystal-lang.
To view this discussion on the web visit https://groups.google.com/d/msgid/crystal-lang/f4e745be-bc6e-4b20-9633-456d7c0193e3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jason Pellerin

unread,
Aug 17, 2015, 12:29:31 PM8/17/15
to crysta...@googlegroups.com
Ahhh! Interesting, that's much nicer -- though not quite what I want, because it doesn't fully extract the internal types. For the array example it ends up with `(String | Int32 | Array(Int32) | Array(String))` where it should be just `(String|Int32)`. I'll have to dig into it a little. Thank you!

Ary Borenszweig

unread,
Aug 18, 2015, 2:03:44 PM8/18/15
to crysta...@googlegroups.com
I wouldn't use macros for that, typeof is much better for this:


Check typeof(Array.elem_type(...))


For more options, visit https://groups.google.com/d/optout.



--
Ary Borenszweig         Manas Technology Solutions
[ar.phone]                      5258.5240       #ARY(279)
[us.phone]                      312.612.1050    #ARY(279)
[email]                         aboren...@manas.com.ar
[web]                           www.manas.com.ar
Reply all
Reply to author
Forward
0 new messages