-
Notifications
You must be signed in to change notification settings - Fork 3
Conversation
Implementation of this macro shows that writing macros in gestalt is much easier than scala.reflect, as programmers don't have to deal with owners. Due to the separation of typed and untyped trees, this implementation is also much solid. |
|
||
val Function(param :: Nil, body) = f | ||
val tempValDef = ValDef(toolbox.fresh("_temp"), prefix) | ||
val tempIdent = Ident(tempValDef.symbol) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder whether just using tempValDef.name should be enough here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually not, because tempIdent has to be a typed tree.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why's that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because transform take typed tree to typed tree.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the tpd => tpd transform essential to this macro, or we can be just fine with untpd => untpd?
q""" | ||
$tempValDef | ||
if ($tempIdent.isEmpty) new Optional(null) | ||
else new Optional($newBody) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a quick idea. How about we prohibit all non-fresh/non-fullyqualified names in untyped portions of macro expansions? This will make new macros even more robust.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we should write fully qualified name, but here Optional is in the empty package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should add support for _empty_
, much like we have support for _root_
. Wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems difficult to impose this constraint in a simple and consistent way -- there are also local variables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Best practice in macrology involves gensymming local variables. Otherwise, there's always the danger of inadvertent name capture (and, in the case of Scala, potentially import conflicts).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is definitely a degree of inconvenience that would be imposed on macro programmers, but it'll also fix the hygiene problems that plague almost all macro tutorials and presentations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see -- then it means we can remove the constructors for untyped Ident
and , then we have hygiene for free? That's a very good idea.Select
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could spin it like that, too. I'm not sure I fully understand the consequences of this particular implementation, but I'm really looking forward to seeing further experiments!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's indeed a very cool idea -- we also solve the hygiene problem in the new macro system. I'll experiment on it tomorrow.
@liufengyun The new macro system is indeed significantly more robust that the old one. What I find extremely satisfying is that all these robustness guarantees are achieved by simply separating typed and untyped trees in API signatures, without any additional APIs like |
I don't think we can avoid |
Not trying to make any argument whether monadless should or shouldn't be supported - that'll be a different discussion. Just noted that this discussion is orthogonal to the already non-trivial benefits provided by the new macro system. |
Typed to typed transform is essential.
Le 15 mai 2017 19:22, "Eugene Burmako" <notifications@github.com> a écrit :
… ***@***.**** commented on this pull request.
------------------------------
In macros/src/main/scala/gestalt/macros/Optional.scala
<#77 (comment)>:
> +
+ val temp = toolbox.fresh("_temp")
+ val tempIdent = Ident(temp)
+
+ q"""
+ val $temp = $prefix
+ if ($tempIdent.isEmpty) $alt else $tempIdent.value
+ """
+ }
+
+ def map[B >: Null](f: A => B)(implicit m: toolbox.WeakTypeTag[A] @uncheckVar): Optional[B] = meta {
+ import toolbox._
+
+ val Function(param :: Nil, body) = f
+ val tempValDef = ValDef(toolbox.fresh("_temp"), prefix)
+ val tempIdent = Ident(tempValDef.symbol)
Is the tpd => tpd transform essential to this macro, or we can be just
fine with untpd => untpd?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#77 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAuDyaqZTuRT9o7b8G1e93bAVoUN2M1Qks5r6InsgaJpZM4NbaQY>
.
|
Yes, I agree it's fine to delay the support for But generally, I see there's no escape from hiding the concept |
Another observation: macro writers (including me) tend to use extractors directly in patmat a lot: My guess is that in patmat, extractors give us more certainty -- there's always some obscurity about what's happening behind quasiqutoes. |
Implementation of the Optional macro:
https://github.com/scalamacros/macrology201/blob/part1/macros/src/main/scala/Macros.scala