[
https://issues.apache.org/jira/browse/GROOVY-7772?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15179074#comment-15179074
]
UEHARA Junji edited comment on GROOVY-7772 at 3/4/16 1:09 AM:
--------------------------------------------------------------
If so, y also might better to be extracted to outside.
{code}
// Definition
x.&y
== {xarg, yarg, args... ->
if xarg instanceof Class && yarg is_not_static_method_on(xarg) then
args[0].yarg(args[1..n])
else
xarg.yarg(args)
}.curry(x,y)
{code}
then,
{code}
"abc".&toUpperCase()() // Instance instanceMethod (No 1)
== {xarg, yarg, args... ->
if xarg instanceof Class && yarg is_not_static_method then
args[0].yarg(args[1..n])
else
xarg.yarg(args)
}.curry("abc","toUpperCase"")()
== {args... ->
if "abc" instanceof Class && "toUpperCase" is_not_static_method then
args[0].toUpperCase(args[1..n])
else
"abc".toUpperCase(args)
}()
== {args... ->
"abc".toUpperCase(args)
}()
== "abc".toUpperCase()
== "ABC"
String.&toUpperCase()("abc") // Class instanceMethod (No 4)
== {xarg, args... ->
if xarg instanceof Class && y is_not_static_method then
args[0].y(args[1..n])
else
xarg.y(args)
}.curry(x)("abc")
== {xarg, args... ->
if xarg instanceof Class && "toUpperCase" is_not_static_method then
args[0].toUpperCase(args[1..n])
else
xarg.toUpperCase(args)
}.curry(String)("abc")
== {args... ->
if String instanceof Class && "toUpperCase" is_not_static_method then
args[0].toUpperCase(args[1..n])
else
String.toUpperCase(args)
}("abc")
== {args... ->
args[0].toUpperCase(args[1..n])
}("abc")
== "abc".toUpperCase()
== "ABC"
{code}
was (Author: uehaj):
If so, y also might better to be extracted to outside.
{code}
// Definition
x.&y
== {xarg, yarg, args... ->
if xarg instanceof Class && yarg is_not_static_method_on(xarg) then
args[0].yarg(args[1..n])
else
xarg.yarg(args)
}.curry(x,y)
{code}
then,
{code}
"abc".&toUpperCase()() // Instance instanceMethod (No 1)
== {xarg, yarg, args... ->
if xarg instanceof Class && yarg is_not_static_method then
args[0].yarg(args[1..n])
else
xarg.yarg(args)
}.curry("abc","toUpperCase"")()
== {xarg, yarg, args... ->
if xarg instanceof Class && yarg is_not_static_method then
args[0].yarg(args[1..n])
else
xarg.yarg(args)
}.curry("abc","toUpperCase"")()
== {args... ->
if "abc" instanceof Class && "toUpperCase" is_not_static_method then
args[0].toUpperCase(args[1..n])
else
"abc".toUpperCase(args)
}()
== {args... ->
"abc".toUpperCase(args)
}()
== "abc".toUpperCase()
== "ABC"
String.&toUpperCase()("abc") // Class instanceMethod (No 4)
== {xarg, args... ->
if xarg instanceof Class && y is_not_static_method then
args[0].y(args[1..n])
else
xarg.y(args)
}.curry(x)("abc")
== {xarg, args... ->
if xarg instanceof Class && "toUpperCase" is_not_static_method then
args[0].toUpperCase(args[1..n])
else
xarg.toUpperCase(args)
}.curry(String)("abc")
== {args... ->
if String instanceof Class && "toUpperCase" is_not_static_method then
args[0].toUpperCase(args[1..n])
else
String.toUpperCase(args)
}("abc")
== {args... ->
args[0].toUpperCase(args[1..n])
}("abc")
== "abc".toUpperCase()
== "ABC"
{code}
> Class.&instanceMethod had better to have same meaning of
> Class::instanceMethod of Java8
> ---------------------------------------------------------------------------------------
>
> Key: GROOVY-7772
> URL: https://issues.apache.org/jira/browse/GROOVY-7772
> Project: Groovy
> Issue Type: Improvement
> Components: groovy-runtime
> Affects Versions: 2.4.6
> Reporter: UEHARA Junji
> Assignee: Jochen Theodorou
> Priority: Minor
>
> Groovy's operator .& for method is similar functionality to Java8's method
> reference operator ::.
> ||No.||lhs||rhs||meaing of Groovy's .& (Closure) ||meaning of java8's ::
> (FunctionalInterface)||
> |1|instance|instanceMethod| { ..args -> instance.instanceMethod(args) | same
> as groovy |
> |2|Class|staticMethod| { ..args -> Class.staticMethod(args) | same as groovy |
> |3|instance|staticMethod| ERROR groovy.lang.MissingMethodException: | Error
> same as groovy (compile error) |
> |4|Class|instanceMethod|error| Function<RetType,Class,Args..>, where method
> instance method of Class which is declared as ```RetType
> instanceMethod(Args..) {...}```. In other words it is interpreted as a
> function which takes LHS Class as the first parameter which additionally
> inserted to the method.)|
> IMHO, i'd like to propose to change the No 4 pattern semantics of groovy
> same as Java 8 's. Because:
> * You can write:
> {code}
> ["a,b,c"].collect ( String.&toUpperCase )
> {code}
> instaed of
> {code}
> ["a,b,c"].collect { it.toUpperCase() }
> {code}
> * Can have correspond operator to java8's ::. which is understandablea and
> needed for Java programmers.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)