Java8 ContainingType::methodName references

130 views
Skip to first unread message

pragp...@gmail.com

unread,
Mar 3, 2016, 4:50:04 PM3/3/16
to scala-language
Java 8 supports method references using the double colon :: operator.
https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html

There are four types of these:
-ContainingClass::staticMethodName (Reference to a static method)
-containingObject::instanceMethodName (Reference to an instance method of a particular object)
-ContainingType::methodName (Reference to an instance method of an arbitrary object of a particular type)
-ClassName::new (Reference to a constructor)

In Scala, for example this one is supported: containingObject::instanceMethodName
by using:
containingObject.instanceMethodName _

But what suprises me is that Scala doesn't seem to support ContainingType::methodName.
ContainingType::methodName can be handy when for example you need to dynamically create objects and perform reflections to call getter and setter method in a strong typed way. Which can be handy in UI code and object construction/binding code.

Other languages also support this:
-Java: https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
-Kotlin: https://kotlinlang.org/docs/reference/reflection.html
-Groovy: http://groovy-lang.org/metaprogramming.html (ExpandoMetaClass)
-C#: http://joelabrahamsson.com/getting-property-and-method-names-using-static-reflection-in-c/

It would be nice if something like this would be possible in Scala:
  case class Person(name: String)

 
def getMethodName(....): String = {
   
// ....
 
}
 
  getMethodName
(Person::name) // returns "name" or "Person.name"



Am I missing something?

Sébastien Doeraene

unread,
Mar 3, 2016, 4:58:40 PM3/3/16
to scala-l...@googlegroups.com
Hi,

You can get what you want with:

val f: (Person, Person) => Int = _ instanceMethodName _

or, in the absence of an expected type helping the inference of the type of _,

val f = (_: Person) instanceMethodName (_: Person)

Cheers,
Sébastien

--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-languag...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

pragp...@gmail.com

unread,
Mar 3, 2016, 5:26:18 PM3/3/16
to scala-language
Hi, thank you for your reply.

I just tried this:
class Person {
 
def firstname() = {
   
"Scala"
  }
}

val f: (Person, Person) => Int = _ firstname _
val g = (_: Person) firstname (_: Person)


But this result in this compilation error:
Error:(9, 23) too many arguments for method firstname: ()String
  val g
= (_: Person) firstname (_: Person)
                     
^
Error:(8, 38) too many arguments for method firstname: ()String
  val f
: (Person, Person) => Int = _ firstname _
                                     
^

Cheers,
Prag

Sébastien Doeraene

unread,
Mar 3, 2016, 5:35:00 PM3/3/16
to scala-l...@googlegroups.com
For a method without parameter, it becomes:

(_: Person).firstname

or

_.firstname

Sébastien

pragp...@gmail.com

unread,
Mar 4, 2016, 5:46:18 PM3/4/16
to scala-language
Ah, that would be a solution for the syntax.
The second part of the question is how to reflect on this function body to get the property name back as a string.

Example:
class Person {
 
def age() = {
   
Random.nextInt
 
}
}

def getMethodName(property: Person => Int): String = {
   
// How to reflect the property name itself out of the anonymous function
}

val property
: Person => Int = (_: Person).age

println
(getMethodName(property)) // should print "Person.age"

This that possible with Scala reflections?

Jasper-M

unread,
Mar 11, 2016, 9:24:58 AM3/11/16
to scala-language
As far as I can see Java doesn't make it possible either to reflect on method references to get the method name. (http://stackoverflow.com/questions/19845213/how-to-get-the-methodinfo-of-a-java-8-method-reference)
I think you can use the same trick as they do in Java if you really want to do this: http://benjiweber.co.uk/blog/2013/12/28/typesafe-database-interaction-with-java-8/

Best,
Jasper

Op vrijdag 4 maart 2016 23:46:18 UTC+1 schreef pragp...@gmail.com:
Reply all
Reply to author
Forward
0 new messages