Created
August 13, 2012 04:27
-
-
Save joshbode/3336974 to your computer and use it in GitHub Desktop.
Nullable Reference Class Fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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