Skip to content

Instantly share code, notes, and snippets.

@peysal
Last active December 21, 2015 13:49
Show Gist options
  • Save peysal/6315567 to your computer and use it in GitHub Desktop.
Save peysal/6315567 to your computer and use it in GitHub Desktop.
Introduction to groovy. Safe deferencing and auto boxing
class Person {
def name
def spouse
def age
}
candidate1 = new Person(name:"bill", spouse: new Person(name:"lengloy"))
candidate1.spouse.spouse = candidate1
assert candidate1.spouse.spouse != null
candidate2 = new Person(name:"bull")
assert candidate2.spouse?.name == null //defendsive programming groovy style. Safe dereferencing
candidate1.age = 32
assert candidate1.age.class == Integer.class
candidate2.age = 30.5
assert candidate2.age.class == BigDecimal.class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment