Re: Java8 ContainingType::methodName references
Jasper-M <[email protected]> Fri, 11 Mar 2016 06:24:58 -0800 (PST)
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <[email protected]> |
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 [email protected]: > > 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? > > > On Thursday, March 3, 2016 at 11:35:00 PM UTC+1, Sébastien Doeraene wrote: >> >> For a method without parameter, it becomes: >> >> (_: Person).firstname >> >> or >> >> _.firstname >> >> Sébastien >> >> On Thu, Mar 3, 2016 at 11:26 PM, <[email protected]> wrote: >> >>> 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]> 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. >>> >> >> -- 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.