You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2021/11/04 05:39:33 UTC

[groovy] branch master updated: tweak records doco

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


The following commit(s) were added to refs/heads/master by this push:
     new 537bd48  tweak records doco
537bd48 is described below

commit 537bd488eb118cdd06aa47ee05ea1d8fbafd9383
Author: Paul King <pa...@asert.com.au>
AuthorDate: Thu Nov 4 15:39:15 2021 +1000

    tweak records doco
---
 src/spec/doc/_records.adoc                   | 18 ++++++++++++++++--
 src/spec/test/RecordSpecificationTest.groovy | 22 ++++++++++++++++++++++
 2 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/src/spec/doc/_records.adoc b/src/spec/doc/_records.adoc
index 0ca5c30..c7873e1 100644
--- a/src/spec/doc/_records.adoc
+++ b/src/spec/doc/_records.adoc
@@ -69,6 +69,20 @@ by writing your own:
 include::../test/RecordSpecificationTest.groovy[tags=record_point3d,indent=0]
 ----
 
+You can also use generics with records in the normal way. For example, consider the following `Coord` record definition:
+
+[source,groovy]
+----
+include::../test/RecordSpecificationTest.groovy[tags=record_generics_defn,indent=0]
+----
+
+It can be used as follows:
+
+[source,groovy]
+----
+include::../test/RecordSpecificationTest.groovy[tags=record_generics_usage,indent=0]
+----
+
 == Compact constructor
 
 Records have an implicit constructor. This can be overridden in the normal way
@@ -164,7 +178,7 @@ include::../test/RecordSpecificationTest.groovy[tags=record_copywith,indent=0]
 ----
 
 The `copyWith` functionality can be disabled by setting the
-`RecordBase#copyWith` annotation attribute to `false`.
+`RecordOptions#copyWith` annotation attribute to `false`.
 
 === Deep immutability
 
@@ -190,7 +204,7 @@ Groovy supports creating _record-like_ classes as well as native records.
 Record-like classes don't extend Java's `Record` class and such classes
 won't be seen by Java as records but will otherwise have similar properties.
 
-The `@RecordBase` annotation (part of `@RecordType`) supports a `mode` annotation attribute
+The `@RecordOptions` annotation (part of `@RecordType`) supports a `mode` annotation attribute
 which can take one of three values (with `AUTO` being the default):
 
 NATIVE::
diff --git a/src/spec/test/RecordSpecificationTest.groovy b/src/spec/test/RecordSpecificationTest.groovy
index 7d29830..1698022 100644
--- a/src/spec/test/RecordSpecificationTest.groovy
+++ b/src/spec/test/RecordSpecificationTest.groovy
@@ -153,6 +153,28 @@ assert c == 'green'
 '''
     }
 
+    void testGenerics() {
+        assertScript '''
+import groovy.transform.CompileStatic
+
+// tag::record_generics_defn[]
+record Coord<T extends Number>(T v1, T v2){
+    double distFromOrigin() { Math.sqrt(v1()**2 + v2()**2 as double) }
+}
+// end::record_generics_defn[]
+
+@groovy.transform.CompileStatic def method() {
+// tag::record_generics_usage[]
+def r1 = new Coord<Integer>(3, 4)
+assert r1.distFromOrigin() == 5
+def r2 = new Coord<Double>(6d, 2.5d)
+assert r2.distFromOrigin() == 6.5d
+// end::record_generics_usage[]
+}
+method()
+'''
+    }
+
     void testComponents() {
         def assertScript = '''
 // tag::record_components[]