Java Magazine, March/April 2016
ORACLE COM JAVAMAGAZINE MARCH APRIL 2016 60 jvm languages println figs filter f match when f isRectangle 10 _ then true otherwise false join n Starting with the figs collection we can use filter to discard the figures that are not rectangles whose first side has a value of 10 and then print the result union Figure Rectangle firstSideLength 10 secondSideLength 10 union Figure Rectangle firstSideLength 10 secondSideLength 30 Augmentations Golo does not provide constructions for defining classes but it provides a way to add methods to any type that it can manipulate This includes class definitions from Java APIs and also Golo struct and union types An augmentation defines a set of functions that can be called as methods The convention for these functions is to call this as the first parameter because it references the receiver object but you are free to use a diferent name Augmentations can be defined by specifying a type augment java lang String function wrap this s1 s2 s1 this s2 more methods can be added The code above adds a wrap method to Java String instances For example the following code would give abc abc wrap An augmentation applies to a type and all its subtypes an augmentation on java lang Object would apply to every type An augmentation is visible from its defining module and the modules that import this module The other way to define an augmentation is by name rather than by a target type augmentation Wrap function wrap this s1 s2 s1 this pretty s2 augmentation PrettyContact function pretty this this name this email With these two named augmentation definitions we can compose and then augment the following struct type struct Contact name email augment Contact with Wrap PrettyContact We can then use the augmented type as follows let dan Contact Dan dan@ tld println dan pretty println dan wrap The code above prints Dan dan@ tld Dan dan@ tld The advantage of named augmentations over augmentations
You must have JavaScript enabled to view digital editions.