groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [groovy] 02/02: GROOVY-7772: Class.&instanceMethod had better to have same meaning of Class::instanceMethod of Java8 (add doco)
Date Thu, 23 May 2019 05:19:21 GMT
This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 375b105dd745b3543d6b374c34fa5ed57eaa8ed6
Author: Paul King <paulk@asert.com.au>
AuthorDate: Thu May 23 15:18:44 2019 +1000

    GROOVY-7772: Class.&instanceMethod had better to have same meaning of Class::instanceMethod
of Java8 (add doco)
---
 src/spec/doc/core-operators.adoc   | 17 +++++++++++++++++
 src/spec/test/OperatorsTest.groovy | 15 +++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/src/spec/doc/core-operators.adoc b/src/spec/doc/core-operators.adoc
index 7fa2566..e1a64f5 100644
--- a/src/spec/doc/core-operators.adoc
+++ b/src/spec/doc/core-operators.adoc
@@ -366,6 +366,23 @@ include::{projectdir}/src/spec/test/OperatorsTest.groovy[tags=method_pointer_dis
 <4> using the method pointer with a `String` calls the `String` version of `doSomething`
 <5> using the method pointer with an `Integer` calls the `Integer` version of `doSomething`
 
+To align with Java 8 method reference expectations, in Groovy 3 and above, you can use `new`
as the
+method name to obtain a method pointer to the constructor:
+[source,groovy]
+----
+include::{projectdir}/src/spec/test/OperatorsTest.groovy[tags=method_pointer_new,indent=0]
+----
+
+Also in Groovy 3 and above, you can obtain a method pointer to an instance method of a class.
+This method pointer takes an additional parameter being the receiver instance to
+invoke the method on:
+[source,groovy]
+----
+include::{projectdir}/src/spec/test/OperatorsTest.groovy[tags=method_pointer_class_instance,indent=0]
+----
+For backwards compatibility, any static methods that happen to have the correct
+parameters for the call will be given precedence over instance methods for this case.
+
 [[method-reference-operator]]
 === Method reference operator
 
diff --git a/src/spec/test/OperatorsTest.groovy b/src/spec/test/OperatorsTest.groovy
index ca0831b..e26c73c 100644
--- a/src/spec/test/OperatorsTest.groovy
+++ b/src/spec/test/OperatorsTest.groovy
@@ -280,6 +280,21 @@ assert user.@name == 'Bob'                   // <1>
             assert reference(123)   == 246                       // <5>
             // end::method_pointer_dispatch[]
         '''
+
+        assertScript '''
+            // tag::method_pointer_new[]
+            def foo  = BigInteger.&new
+            def fortyTwo = foo('42')
+            assert fortyTwo == 42G
+            // end::method_pointer_new[]
+        '''
+
+        assertScript '''
+            // tag::method_pointer_class_instance[]
+            def instanceMethod = String.&toUpperCase
+            assert instanceMethod('foo') == 'FOO'
+            // end::method_pointer_class_instance[]
+        '''
     }
 
     void testMethodReference() {


Mime
View raw message