![]() ![]() That's all for the introductory material, I hope the features presented in this three part series have interested you in trying Groovy. When used along with Closures and maps you may coerce that closure or map into a concrete implementation of an interface (abstract and concrete class coercion supported since Groovy 1.1)ĭef person = as Person It may be overridden/extended like an operator if your class has a method named asType( Class ). The last operator is not exactly an operator per se, it is a reserved keyword ( as) which enables Groovy casting, a better way to convert a type into another. let's grab a reference to the static method Let's see in action with a revised version of Math and let's throw in curry just for fun This feature is very useful to transform a method into a closure, and enables behavior mixin in a similar way as a Ruby does as you may call that closure from context of an external class (not in the hierarchy of the one that owns the method). In Groovy 1.1 its also possible to obtain a property reference. The next operator should bring memories to those that used to work with the C language, as it allows you to get a reference to a method (in C it was a function pointer). Listing 5.3 assert "hello" = "hello" ?: "bye" If it's, false then it will return the other expression The third operator in our list is Elvis (?:) which works like a simplified version of the ternary operator, as it will return the conditional expression if it evaluates to true. Listing 4.1 def getSlope = Īssert = *.reverse() Think "constant parameters" for the second function. Currying is a technique that transforms a function (in our case a closure) into another one while fixing one or more values, meaning that the fixed values remain constant for each invocation of the second function. The name of this section is just a play of words (unfortunately section 2 did not follow the * parameters as it did in the Spanish version) because what I really want to say is known " currying". Thus makes the use of proper code comments a critical issue (we all comment our code right?). This technique allows you to communicate in a better way what's the meaning of each parameter, but at the same time sacrifices any attempt to refactor and know which parameters are allowed just by looking at thye method signature. When you solve the equation for m you may find the value for any point in the space ( x, y) with an optional offset ( b). If you remember your math lessons from school, the straight line formula is y = mx + b where m is the slope and b is a constant. Just declare a Map as parameter and access its keys inside the methodĪssert 0.5 = Math.getSlope( x: 2, y: 2, b: 1 ) A similar trick may be used to simulate named parameters in any methods. The trick (if you recall) is that Groovy injects a generic constructor with a Map as parameter. When POGOs were discussed in the last part we saw a way to create bean instances using a combination of key/value pairs, where each key mapped to a property. Java5 equivalent also available in Groovy 1.1īefore I get slammed with hate-mail I know there are better ways to sum numbers in Groovy, this example is just to show varargs in action. then the method call may be used with varargs follow the convention, if the argument is of type Object Before the current branch for Groovy 1.1 gave us support for the three dot notation available in Java5, Groovy followed a convention if you wanted a method to have varargs: the last argument should be declared as Object But it has been a feature other language, even before Java was born (in languages like C or LiveScript ). The ability for a method to work with a variable number of arguments is relatively new to Java (since Java5 to be precise). Some you may find them on other languages, but some are exclusive to Groovy. In this third installment of Introduction to Groovy ( part I, part II) we will continue looking at some features of the Groovy language.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |