Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8468615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:59:04+00:00 2026-06-10T15:59:04+00:00

For a DSL I would like to be able to do something like: object

  • 0

For a DSL I would like to be able to do something like:

object Creator { 
   def create[T](s :String) :Foo[T] = macro createImpl[T]
   def createImpl[T](c :Context)(s :c.Expr[String]) : c.Expr[Foo[T]] = {
        reify(new Foo[Any]())
   }
 }

My problem is to replace the Any in reify with something that will return the correctly parametrized version.

(above I use a string argument, but in the final version I plan to use the companion object of class T as a marker to know the argument-type of a Function1[T,Unit])

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:59:06+00:00Added an answer on June 10, 2026 at 3:59 pm

    You need to:
    a) write new Foo[T]() instead of new Foo[Any]() (easy)
    b) pass in the macro a representation of type T, namely, a value of type AbsTypeTag[T], by declaring the parameter T using a context bound: [T: c.AbsTypeTag].

    Here’s code which I tested in Scala 2.10.0-M7. Edit. In 2.10.0-RC1 AbsTypeTag has been renamed to WeakTypeTag. Everything else about macros and type tags remains the same.

    Creator.scala:

    import language.experimental.macros
    import reflect.macros.Context
    class Foo[T]
    object Creator {
      def create[T](s: String): Foo[T] = macro createImpl[T]
      def createImpl[T: c.AbsTypeTag](c: Context)(s: c.Expr[String]): c.Expr[Foo[T]] = {
        import c.universe._
        reify(new Foo[T]())
      }
    }
    

    MacroClient.scala:

    object Main extends App {
      println (Creator.create[Int](""))
    }
    

    Note that if you omit the type parameter, you will get a weird error:

    scala> Creator.create[Int]("")
    res2: Foo[Int] = Foo@4888884e
    
    scala> Creator.create("")
    <console>:12: error: macro has not been expanded
                  Creator.create("")
                          ^
    

    You also write:

    (above I use a string argument, but in the final version I plan to use
    the companion object of class T as a marker to know the argument-type
    of a Function1[T,Unit])

    but if I get it right, this sounds like a bad idea. Instead of writing Creator.create[T](otherArgs), the call syntax would be something like Creator.create(T, otherArgs), not a big advantage (if any). But you can’t even get the latter syntax: if class A and object A are companions, their types are not related: the first has type A, the second has type A.type where A is the companion object and not the type of class A.


    Update: how to get the Creator create Foo syntax to work and return an instance of Foo, if you have control over Foo.
    Since you ask about the Any type argument to reify, I assume you are asking about the type argument. That makes only sense if you want the static return type of Creator.create to be T and not Any; otherwise, you should clarify your question.

    The problem here has little to do with macros. Creator create Foo passes the object Foo to Creator.create, whose declaration needs to express, given Foo.type, the type Foo through a type expression. Type expressions in Scala are quite limited – they can’t use reflection, for instance. But given a type, they can select its type members.

    trait Companion[Class]
    //How to declare a companion
    class Foo
    object Foo extends Companion[Foo]
    /*I'm cheating: an implementation of Companion does not need to be a true Companion. You can add documentation to explain how Companion is supposed to be used. */
    object Bar extends Companion[Foo]
    //But this is also useful - you can't create your own companion objects for pre-existing types, but you can still create the right instances of Companion:
    object pInt extends Companion[Int]
    object Creator {
      //T with Companion[S] is needed to workaround a type inference bug (SI-5298) and allow S to be correctly inferred.
      def create[S, T <: Companion[S]](obj: T with Companion[S]): S = ???
    }
    

    This is limited since you need to alter the companion object, but I’m pretty sure you can’t do better. I know no way, in a type expression (what you can use in place of S when declaring the return type of create) of getting from a companion object to its associated class type in general, and I don’t think there’s one.

    Now, changing the above to use macros is straightforward:

    import language.experimental.macros
    import reflect.macros.Context
    class Foo[T]
    object Creator {
      //T with Companion[S] is needed to workaround a type inference bug (SI-5298) and allow S to be correctly inferred.
      def create[S, T <: Companion[S]](obj: T with Companion[S]): Foo[S] = macro createImpl[S, T]
      def createImpl[S: c.AbsTypeTag, T <: Companion[S]: c.AbsTypeTag](c: Context)(obj: c.Expr[T with Companion[S]]): c.Expr[Foo[S]] = {
        import c.universe._
        reify(new Foo[S]())
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a simple file format/DSL which would allow my users
For DSL purposes I want to detect methods defined like: def methodName() {} or
Suppose I need to create my own small DSL that would use Python to
I have a DSL with Java front-end and I would like to serialize an
I have a relatively simple DSL that I would like to handle more robustly
I would like to know what are the best DSL tools created to be
As you would expect from a DSL aimed at data analysis, R handles missing/incomplete
I have a DSL - like piece of Ruby code that looks like this:
I try to write DSL class Warcraft class << self def config unless @instance
I have a DSL Java object, i.e. a POJO which returns this in setters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.