Java8 ContainingType::methodName references
[email protected] Thu, 3 Mar 2016 13:50:03 -0800 (PST)
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <[email protected]> |
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?
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.