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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:35:07+00:00 2026-06-19T03:35:07+00:00

Not sure how to properly formulate this (hence, how to look it up), but

  • 0

Not sure how to properly formulate this (hence, how to look it up), but here goes:

I understand how a method applied to an object can become a function object. For example:

case class User(name: String, age: Int)
val user = User("", 0)
user.name _   // () => String

So, if I had a method def meth(f: () => String), I could do: meth(user.name _)

Is is possible to define a type that has as instances the methods of class User ?
(the function objects obtained from these methods, more precisely)

In order words, what would the type of f in def meth(f: ???) be, in order to be able to do this: meth(user.name _) and meth(user.age _)

Thanks!

  • 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-19T03:35:08+00:00Added an answer on June 19, 2026 at 3:35 am

    I guess the only way to do something like that is to use macro’s. I once created a macro that does the following (note that some details changed in the current implementation and thus are empty).

    case class Metadata(instance: AnyRef, name: String) {
    
      def value = ??? // use reflection to invoke `name` on `instance`
    }
    
    object Metadata extends ((AnyRef, String) => Metadata) {
    
      implicit def anyToMetadata(sym: Any): Metadata = 
        macro MetadataMacro.anyToMetadataImpl
    }
    
    private[staticReflection] object MetadataMacro {
    
      def anyToMetadataImpl(c: Context)(sym: c.Expr[Any]): c.Expr[Metadata] = {
    
        import c.universe._
    
        val metadata = // match the tree and create the appropriate metadata instance
    
        c.Expr(metadata)
      }
    }
    

    In code you would then use it like this:

    case class User(name:String)
    
    def test(metadata:Metadata) {
      println(metadata.name + "->" + metadata.value)
    }
    
    val u = User("test")
    
    test(u.name) // name -> test
    

    The code as it was valid almost a year ago can be found here: ee/scala/staticReflection/Metadata.scala. More info about macros as they are now.

    If this is what you were looking for, please let me know so I can see if I can convert the original to a working version.


    Edit

    I managed to get the old one working. To use it simply copy-past the code into a separate project (I use Eclipse) and then link the projects via the Java Build Path. Working version:

    package ee.scala.staticReflection
    
    import scala.language.experimental.macros
    import scala.reflect.macros.Context
    import language.implicitConversions
    
    case class Metadata(instance: AnyRef, name: String) {
      // any comments on how to improve this part are welcome
      val runtimeUniverse = scala.reflect.runtime.universe
      val mirror = runtimeUniverse.runtimeMirror(getClass.getClassLoader)
      val instanceMirror = mirror.reflect(instance)
      val method = instanceMirror.symbol.selfType.member(runtimeUniverse newTermName name).asMethod
      val methodMirror = instanceMirror.reflectMethod(method)
    
      def value = methodMirror()
    }
    
    object Metadata extends ((AnyRef, String) => Metadata) {
    
      implicit def anyToMetadata(sym: Any): Metadata = macro MetadataMacro.anyToMetadataImpl
    }
    
    private[staticReflection] object MetadataMacro {
    
      def anyToMetadataImpl(c: Context)(sym: c.Expr[Any]): c.Expr[Metadata] = {
        import c.universe._
    
        def createMetadataInstance(select: Select): Tree =
          treeBuild.mkMethodCall(
            c.mirror.staticModule("ee.scala.staticReflection.Metadata"),
            newTermName("apply"),
            List(select.qualifier, Literal(Constant(select.name.toString))))
    
        val metadata = sym.tree match {
          //normal select
          case select: Select => createMetadataInstance(select)
    
          //could be a call using a right associative operator
          case Ident(name) =>
            c.enclosingMethod.collect {
              case ValDef(_, refName, _, select: Select) if refName == name => createMetadataInstance(select)
            }
              .headOption
              .getOrElse(throw new Exception("Could not find ValDef for " + name))
    
          case _ => throw new Exception("Could not create metadata")
        }
    
        c.Expr(metadata)
      }
    }
    

    Edit 2

    To apply all of the above stuff to the question of the original poster. You could use it like this

    case class User(name: String, age: Int)
    val user = User("", 0)
    
    def meth(metadata: Metadata) = {
      println(metadata.name + "->" + metadata.value)
    }
    
    meth(user.name)
    meth(user.age)
    

    Note that the solution is different from what was proposed, but within the meth function you can do the same (a bit more even).

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

Sidebar

Related Questions

I'm not sure if this question fits properly, but I'm wondering in tile-based games,
Not sure if im just being stupid or something but here goes i work
Not sure I can explain this properly.... I have the following sequence of code
I'm not exactly sure how to properly debug this but have tried a few
I'm sorry, I can't really google this because I'm not sure how to properly
Not sure whats going on here, or what could be the integer in this
Not sure how to ask this, but i'll give it a try: I have
Not sure if this is a big deal. But wondering why when the site
not sure what I'm missing here, but i keep getting the error. SQLSTATE[HY093]: Invalid
Not sure if there's a offical name, but by DataContext I mean an object

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.