473,699 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

11 New Member
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 2227

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

Similar topics

5
1889
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 here
5
1920
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
2421
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
1465
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
1640
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, dictionary, etc. I was wondering if I can use decorators to accomplish this. For instance, I have the following method def getUsers(self, params): return users.query(dbc)
3
3565
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 whenever i click the button. I've tried changing the stLinkCriteria2 type to String but the same thing happens. Does anyone know why? Private Sub IndividualRpt_Click() On Error GoTo Err_Command12_Click Dim stDocName As String Dim...
7
8858
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
6443
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 enum but its members can be addressed with '.'? I don't want to have to do this using #defines, as it would be far too messy. code: static struct { static const int baz = 1; } bar;
4
1496
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 the myclass instance? Thanks, Bob= ####
2
5590
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 getting the error before it opens. The line of code that calls the form from another form is. DoCmd.OpenForm "NonConformanceAdd",,,,acFormAdd,acDialog I get the error, click ok, then the form opens and works exactly how it's supposed to. I even...
0
8705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8628
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9199
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4391
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3075
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2016
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.