473,387 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Getting a structural type with an anonymous class's methods from a macro ?

Suppose we want to write a macro that defines an anonymous class with some type members or methods, and then creates an instance of that class that's statically typed as a structural type with those methods, etc. This is possible with the macro system in 2.10.0, and the type member part is extremely easy:


Expand|Select|Wrap|Line Numbers
  1. object MacroExample extends ReflectionUtils {
  2.   import scala.language.experimental.macros
  3.   import scala.reflect.macros.Context
  4.  
  5.   def foo(name: String): Any = macro foo_impl
  6.   def foo_impl(c: Context)(name: c.Expr[String]) = {
  7.     import c.universe._
  8.  
  9.     val Literal(Constant(lit: String)) = name.tree
  10.     val anon = newTypeName(c.fresh)
  11.  
  12.     c.Expr(Block(
  13.       ClassDef(
  14.         Modifiers(Flag.FINAL), anon, Nil, Template(
  15.           Nil, emptyValDef, List(
  16.             constructor(c.universe),
  17.             TypeDef(Modifiers(), newTypeName(lit), Nil, TypeTree(typeOf[Int]))
  18.           )
  19.         )
  20.       ),
  21.       Apply(Select(New(Ident(anon)), nme.CONSTRUCTOR), Nil)
  22.     ))
  23.   }
  24. }
(Where ReflectionUtils is a convenience trait that provides my constructor method.)

This macro lets us specify the name of the anonymous class's type member as a string literal:

Expand|Select|Wrap|Line Numbers
  1. scala> MacroExample.foo("T")
  2. res0: AnyRef{type T = Int} = $1$$1@7da533f6
Note that it's appropriately typed. We can confirm that everything's working as expected:

Expand|Select|Wrap|Line Numbers
  1. scala> implicitly[res0.T =:= Int]
  2. res1: =:=[res0.T,Int] = <function1>
  3.  
Now suppose that we try to do the same thing with a method:

Expand|Select|Wrap|Line Numbers
  1. def bar(name: String): Any = macro bar_impl
  2. def bar_impl(c: Context)(name: c.Expr[String]) = {
  3.   import c.universe._
  4.  
  5.   val Literal(Constant(lit: String)) = name.tree
  6.   val anon = newTypeName(c.fresh)
  7.  
  8.   c.Expr(Block(
  9.     ClassDef(
  10.       Modifiers(Flag.FINAL), anon, Nil, Template(
  11.         Nil, emptyValDef, List(
  12.           constructor(c.universe),
  13.           DefDef(
  14.             Modifiers(), newTermName(lit), Nil, Nil, TypeTree(),
  15.             c.literal(42).tree
  16.           )
  17.         )
  18.       )
  19.     ),
  20.     Apply(Select(New(Ident(anon)), nme.CONSTRUCTOR), Nil)
  21.   ))
  22. }
But when we try it out, we don't get a structural type

Expand|Select|Wrap|Line Numbers
  1. scala> MacroExample.bar("test")
  2. res1: AnyRef = $1$$1@da12492
But if we stick an extra anonymous class in there:

Expand|Select|Wrap|Line Numbers
  1. def baz(name: String): Any = macro baz_impl
  2. def baz_impl(c: Context)(name: c.Expr[String]) = {
  3.   import c.universe._
  4.  
  5.   val Literal(Constant(lit: String)) = name.tree
  6.   val anon = newTypeName(c.fresh)
  7.   val wrapper = newTypeName(c.fresh)
  8.  
  9.   c.Expr(Block(
  10.     ClassDef(
  11.       Modifiers(), anon, Nil, Template(
  12.         Nil, emptyValDef, List(
  13.           constructor(c.universe),
  14.           DefDef(
  15.             Modifiers(), newTermName(lit), Nil, Nil, TypeTree(),
  16.             c.literal(42).tree
  17.           )
  18.         )
  19.       )
  20.     ),
  21.     ClassDef(
  22.       Modifiers(Flag.FINAL), wrapper, Nil,
  23.       Template(Ident(anon) :: Nil, emptyValDef, constructor(c.universe) :: Nil)
  24.     ),
  25.     Apply(Select(New(Ident(wrapper)), nme.CONSTRUCTOR), Nil)
  26.   ))
  27. }
It works:

Expand|Select|Wrap|Line Numbers
  1. scala> MacroExample.baz("test")
  2. res0: AnyRef{def test: Int} = $2$$1@6663f834
  3.  
  4. scala> res0.test
  5. res1: Int = 42
This is extremely handy—it lets you do things like this, for example—but I don't understand why it works, and the type member version works, but not bar. I know this may not be defined behavior, but does it make any sense? Is there an cleaner way to get a structural type (with the methods on it) from a macro?
Sep 18 '13 #1
0 2211

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: fotzor | last post by:
Hi, is it somehow possible to get the type of a variable in a macro? example: #define SOME_MACRO(x) // what's the type of x??? i guess the type-information is lost but i can't use a template...
5
by: bittercold | last post by:
Hi, guys, I need to overload global "new" operator but I have to know the type id "new" is begin invoked on. For example if I overload it like this void *operator new(size_t n) { ...body......
26
by: Michael McGarry | last post by:
Hi, I am pretty sure this is not possible, but maybe somehow it is. Given a variable, can I tell what type it is at runtime? Michael
1
by: jester | last post by:
For debugging purposes, I'm trying to programmatically retrieve the user account name for the anonymous and impersonated account. Using User.Identity.Name didn't do the trick. Any ideas? Thanks =)
8
by: WakeBdr | last post by:
I'm writing a class that will query a database for some data and return the result to the caller. I need to be able to return the result of the query in several different ways: list, xml,...
3
by: emmaruwa | last post by:
The following VBA code, in Access 2003, is to make a button open and populate a form based on the Surname and Period_frm data on a previous form. However, i keep getting a type mismatch error...
7
by: David Resnick | last post by:
I'm faced with a header with anonymous structures declared inside a union like this: union msg { struct { int a; int b; } s1; struct {
16
by: andreyvul | last post by:
If I try compiling this in gcc, it says: "error: request for member `baz' in something not a structure or union". Any workarounds or tips on how to make a structure such that it behaves like an...
4
by: philly_bob | last post by:
In the sample program below, I want to send a random method to a class instance. In other words, I don't know which method to send until run-time. How can I send ch, which is my random choice, to...
2
DonRayner
by: DonRayner | last post by:
This one has me stumped. I'm getting a "Type Mismatch" error on one of my forms when it's being opened. It's hapening before the forms "On Open" event, I stuck a msgbox in there to check and I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.