Groovy Tuples
A tuple is an ordered, immutable list of elements. Groovy has it's own groovy.lang.Tuple class. We can create an instance of a Tuple by providing all elements that need to be in the Tuple via the constructor. We cannot add new elements to a Tuple instance or remove elements. We cannot even change elements in a tuple, so it is completely immutable. This makes it very useable as return value for a method where we need to return multiple values. Groovy also provides a Tuple2 class that can be used for tuple instance of only two elements. The elements are typed in a Tuple2 instance. def tuple = new Tuple ( 'one' , 1 , new Expando ( number : 1 ) ) assert tuple . size ( ) == 3 // To get the value of an element // at a certain position we use // the get(index) method. assert tuple . get ( 0 ) ==...
Comments
Post a Comment