Java Magazine, March/April 2016
ORACLE COM JAVAMAGAZINE MARCH APRIL 2016 58 jvm languages interface that is it has a single abstract method in conjunction with default methods Similarly thenAcceptAsync expects a java util function Consumer which is also a functional interface The Golo runtime automatically adapts function references to both Java single method and functional interfaces Structures Golo provides structured data definitions using the struct declaration as in the following struct Message id date payload A structure object has a constructor getter and setter accessors as well as sensible equals hashCode and toString methods Here is a sample usage of the Message structure definition above let m Message id 12345 date 2016 01 22 15 41 CEST payload I hope this message finds you well Yours sincerely Julien println m m id 6789 println m id The code above would print struct Message id 12345 date 2016 01 22 15 41 CEST payload I hope this message finds you well Yours sincerely Julien 6789 Note that defines multiline strings in Golo Immutable copies of a struct object can be made by calling the frozenCopy method Every structure also comes with a constructor for making immutable objects the name of the constructor function is prefixed by Immutable In the previous example we could have constructed m as immutable by calling ImmutableMessage rather than Message This would also cause the m id 6789 call to fail because m would be immutable Structure fields have public visibility by default Given a module importing the module that defined the Message structure above the module would have access to all fields through their accessors for example id and id newValue It is possible to restrict visibility by prefixing fields with an underscore Any such field is visible only from within its defining module code and remains hidden from other modules Finally structure objects can be destructured and enumerated This makes it possible to write the following let id date payload m println id println date println payload foreach field value in m println field value Tagged Unions As a complement to structure types Golo supports tagged unions sometimes also called sum algebraic data types Here is an example defining a Figure type
You must have JavaScript enabled to view digital editions.