Re: Java8 ContainingType::methodName references

Sébastien Doeraene <[email protected]> Thu, 3 Mar 2016 22:58:17 +0100
Newsgroups gmane.comp.lang.scala
Message-ID <CAJwkOg7ucBbbHRwKoSsTMOO+ykhea2bfUro-DVPSN0mWM8ro4A@mail.gmail.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

On Thu, Mar 3, 2016 at 10:50 PM, <[email protected]> wrote:

> 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.
>

-- 
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.