Re: Java8 ContainingType::methodName references
[email protected] Thu, 3 Mar 2016 14:26:17 -0800 (PST)
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <[email protected]> |
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
On Thursday, March 3, 2016 at 10:58:40 PM UTC+1, Sébastien Doeraene wrote:
>
> 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] <javascript:>> 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] <javascript:>.
>> 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.