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!
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).
In code you would then use it like this:
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:Edit 2
To apply all of the above stuff to the question of the original poster. You could use it like this
Note that the solution is different from what was proposed, but within the
methfunction you can do the same (a bit more even).