This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Improved custom builder lookup (solves #6) #7
Merged
kciesielski
merged 1 commit into
kciesielski:master
from
mkubala:feature/moreElegantBuilderLookup
Sep 30, 2014
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
macros/src/main/scala/com/softwaremill/macmemo/BuilderResolver.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.softwaremill.macmemo | ||
|
||
import scala.language.experimental.macros | ||
|
||
object BuilderResolver { | ||
|
||
def resolve(methodFullName: String): MemoCacheBuilder = macro builderResolverMacro_impl | ||
|
||
def builderResolverMacro_impl(c: scala.reflect.macros.whitebox.Context)(methodFullName: c.Expr[String]): c.Expr[MemoCacheBuilder] = { | ||
import c.universe._ | ||
|
||
def bringDefaultBuilder: Tree = { | ||
val Literal(Constant(mfn: String)) = methodFullName.tree | ||
val msg = s"Cannot find custom memo builder for '$mfn' - default builder will be used" | ||
c.info(c.enclosingPosition, msg, false) | ||
reify { | ||
MemoCacheBuilder.guavaMemoCacheBuilder | ||
}.tree | ||
} | ||
|
||
val builderTree = c.inferImplicitValue(typeOf[MemoCacheBuilder]) match { | ||
case EmptyTree => bringDefaultBuilder | ||
case foundBuilderTree => foundBuilderTree | ||
} | ||
|
||
c.Expr[MemoCacheBuilder](Block(List(), builderTree)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
So it seems that putting an auxiliary macro call inside a quasiquote makes c.inferImplicitValue() work well, where calling this from an annotation macro context doesn't work in all cases? That's really meta-meta :) But it looks kinda like a bug to me, or at least some weird incosistency I cannot understand...
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'm going to do some investigation about this behaviour.
My Current clues:
Edit:
AFAIK def macros are an (experimental) part of scala compiler, while annotation macros are still available via macroparadise plugin. Maybe this is the reason?
@xeno-by could you elaborate on this?
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.
@kciesielski The issue with c.typecheck (and c.inferImplicitValue that's essentially based on c.typecheck) in macro annotations is a yet unsolved problem in the design of macro annotations. That's indeed a bug.
@mkubala Your high-level hypothesis about the reason for this limitation is spot-on (if you're interested in details, scalamacros/paradise#14 provides a lot of information on the nature of the issue).
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.
Thanks for explanation 👍