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 9024635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:04:18+00:00 2026-06-16T06:04:18+00:00

I’ve been working with Scala Macros and have the following code in the macro:

  • 0

I’ve been working with Scala Macros and have the following code in the macro:

    val fieldMemberType = fieldMember.typeSignatureIn(objectType) match {
      case NullaryMethodType(tpe)   => tpe
      case _                      => doesntCompile(s"$propertyName isn't a field, it must be another thing")
    }

    reify{
      new TypeBuilder() {
        type fieldType = fieldMemberType.type
      }
    }

As you can see, I’ve managed to get a c.universe.Type fieldMemberType. This represents the type of certain field in the object. Once I get that, I want to create a new TypeBuilder object in the reify. TypeBuilder is an abstract class with an abstract parameter. This abstract parameter is fieldType. I want this fieldType to be the type that I’ve found before.

Running the code shown here returns me a fieldMemberType not found. Is there any way that I can get the fieldMemberType to work inside the reify clause?

  • 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-16T06:04:20+00:00Added an answer on June 16, 2026 at 6:04 am

    The problem is that the code you pass to reify is essentially going to be placed verbatim at the point where the macro is being expanded, and fieldMemberType isn’t going to mean anything there.

    In some cases you can use splice to sneak an expression that you have at macro-expansion time into the code you’re reifying. For example, if we were trying to create an instance of this trait:

    trait Foo { def i: Int }
    

    And had this variable at macro-expansion time:

    val myInt = 10
    

    We could write the following:

    reify { new Foo { def i = c.literal(myInt).splice } }
    

    That’s not going to work here, which means you’re going to have to forget about nice little reify and write out the AST by hand. You’ll find this happens a lot, unfortunately. My standard approach is to start a new REPL and type something like this:

    import scala.reflect.runtime.universe._
    
    trait TypeBuilder { type fieldType }
    
    showRaw(reify(new TypeBuilder { type fieldType = String }))
    

    This will spit out several lines of AST, which you can then cut and paste into your macro definition as a starting point. Then you fiddle with it, replacing things like this:

    Ident(TypeBuilder)
    

    With this:

    Ident(newTypeName("TypeBuilder"))
    

    And FINAL with Flag.FINAL, and so on. I wish the toString methods for the AST types corresponded more exactly to the code it takes to build them, but you’ll pretty quickly get a sense of what you need to change. You’ll end up with something like this:

    c.Expr(
      Block(
        ClassDef(
          Modifiers(Flag.FINAL),
          anon,
          Nil,
          Template(
            Ident(newTypeName("TypeBuilder")) :: Nil,
            emptyValDef,
            List(
              constructor(c),
              TypeDef(
                Modifiers(),
                newTypeName("fieldType"),
                Nil,
                TypeTree(fieldMemberType)
              )
            )
          )
        ),
        Apply(Select(New(Ident(anon)), nme.CONSTRUCTOR), Nil)
      )
    )
    

    Where anon is a type name you’ve created in advance for your anonymous class, and constructor is a convenience method I use to make this kind of thing a little less hideous (you can find its definition at the end of this complete working example).

    Now if we wrap this expression up in something like this, we can write the following:

    scala> TypeMemberExample.builderWithType[String]
    res0: TypeBuilder{type fieldType = String} = $1$$1@fb3f1f3
    

    So it works. We’ve taken a c.universe.Type (which I get here from the WeakTypeTag of the type parameter on builderWithType, but it will work in exactly the same way with any old Type) and used it to define the type member of our TypeBuilder trait.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have been unable to fix a problem with Java Unicode and encoding. The
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,

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.