Skip to content

Instantly share code, notes, and snippets.

@joshbode
Created August 13, 2012 04:27
Show Gist options
  • Save joshbode/3336974 to your computer and use it in GitHub Desktop.
Save joshbode/3336974 to your computer and use it in GitHub Desktop.
Nullable Reference Class Fields
nullable = function(name) {
union_name = paste(name, 'NULL', sep='_')
tryCatch({
# existing definition
getClass(union_name)
return(union_name)
},
error=function(e) {
# new definition
return(setClassUnion(union_name , c(name, 'NULL')))
})
}
X = setRefClass('X',
fields=list(name=nullable('character')),
methods=list(
initialize=function(name=NULL) {
.self$name = name
return(.self)
}
)
)
x = X$new()
y = Y$new('Josh')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment