Open liufengyun opened 7 years ago
A simple solution to support TypeTags
just for macros is as follows:
TypeTag
arguments always succeed with dummy null
TypeTag
to TermTree
TypeTag
based on the type of the def macro and supplies to macro expansion.In scala.reflect, typetags in macros are used for two task: 1) to obtain definition-site types instantiated with callsite knowledge, 2) to instantiate types via typeOf
.
In the new macro system, both tasks are achieved by something else. 1) is covered by the fact that we can access types from the definition site inside meta blocks. 2) needs a different API (either termRef/typeRef/... or quasiquotes or something else like primitivizing typeOf).
Concretely, in this example, both M
and Monad
should be accessible inside the meta block by names M
and Monad
.
I've no idea how to make this work with separate compilation. It seems both M
and Monad
are inaccessible in the implementation method.
The following rule is disputable:
implicit search of TypeTag arguments always succeed with dummy null
It also makes writing macro helper non-standard: we cannot pass TypeTag as implicits!
A more correct approach is using the following rule:
null
if normal implicit resolution fails.The new rule is implemented in https://github.com/liufengyun/dotty/commit/5d7fa1f00ec47f77295c46ab44152fab5bc4a2f9, together with a small bug fix.
An even better rule is as follows:
This will reduce the unexpected behaviour to a minimum. For example, in a macro helper, programmers may accidentally forget to pass the implicit TypeTag around. In this case, an error message is more friendly than a run-time null pointer exception.
The Monadless library uses TypeTags:
Following the extractor approach, TypeBags have to be platform-independent based on abstract types.