Skip to content

Instantly share code, notes, and snippets.

@danveloper
Created March 10, 2013 22:30
Show Gist options
  • Save danveloper/5130793 to your computer and use it in GitHub Desktop.
Save danveloper/5130793 to your computer and use it in GitHub Desktop.
Destructurable data type using getAt() to associate a result with an object in a service method call
class Destructurable<E> {
E obj
Boolean result
def getAt(idx) {
if (idx == 0) return obj
if (idx == 1) return result
throw new RuntimeException("Only two parameters are returnable")
}
}
class User {
String email
String password
boolean save() {
if (!email || !password) {
return false
}
return true
}
}
def userService = new UserService()
def (user, success) = userService.register("dan.i.el.p.woods@gmail.com", null)
assert !success
(user, success) = userService.register("dan.i.el.p.woods@gmail.com", "1234")
assert success
class UserService {
Destructurable<User> register(email, password) {
def user = new User(email: email, password: password)
def result = true
if (!user.save()) {
result = false
}
return [obj: user, result: result] as Destructurable<User>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment