Last active
December 25, 2015 17:29
-
-
Save wch/7013262 to your computer and use it in GitHub Desktop.
R reference classes: accessing methods with .self$foo is different from other ways of accessing
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
TestClass <- setRefClass( | |
'TestClass', | |
fields = list(a = 'ANY'), | |
methods = list( | |
initialize = function() { | |
e <- environment() | |
pe <- parent.env(e) | |
ppe <- parent.env(pe) | |
# The environment of this object | |
print(ls(pe)) | |
# The environment of the class | |
print(ls(ppe)) | |
# No surprises with fields | |
cat("==================== .self[['a']] ====================\n") | |
print(.self[['a']]) | |
# Getting b these ways isn't quite what we want | |
cat("==================== .self[['b']] ====================\n") | |
print(.self[['b']]) | |
cat("==================== b ====================\n") | |
print(b) | |
# Accessing b with $ works, and it changes things from here on in | |
cat("==================== .self$b ====================\n") | |
print(.self$b) | |
# Now these return the b method with some attributes attached | |
cat("==================== .self[['b']] ====================\n") | |
print(.self[['b']]) | |
cat("==================== b ====================\n") | |
print(b) | |
cat("===============================================\n") | |
print(ls(parent.env(e))) | |
print(ls(parent.env(parent.env(e)))) | |
}, | |
b = function() { | |
"Yes, this is b." | |
} | |
) | |
) | |
tc <- TestClass$new() |
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
> tc <- TestClass$new() | |
[1] "a" "initialize" | |
[1] "b" "e" "tc" "TestClass" | |
==================== .self[['a']] ==================== | |
An object of class "uninitializedField" | |
Slot "field": | |
[1] "a" | |
Slot "className": | |
[1] "ANY" | |
==================== .self[['b']] ==================== | |
NULL | |
==================== b ==================== | |
function() { | |
"Yes, this is b." | |
} | |
==================== .self$b ==================== | |
function() { | |
"Yes, this is b." | |
} | |
<environment: 0x5a65418> | |
attr(,"mayCall") | |
character(0) | |
attr(,"name") | |
[1] "b" | |
attr(,"refClassName") | |
[1] "TestClass" | |
attr(,"superClassMethod") | |
[1] "" | |
attr(,"class") | |
[1] "refMethodDef" | |
attr(,"class")attr(,"package") | |
[1] "methods" | |
==================== .self[['b']] ==================== | |
function() { | |
"Yes, this is b." | |
} | |
<environment: 0x5a65418> | |
attr(,"mayCall") | |
character(0) | |
attr(,"name") | |
[1] "b" | |
attr(,"refClassName") | |
[1] "TestClass" | |
attr(,"superClassMethod") | |
[1] "" | |
attr(,"class") | |
[1] "refMethodDef" | |
attr(,"class")attr(,"package") | |
[1] "methods" | |
==================== b ==================== | |
function() { | |
"Yes, this is b." | |
} | |
<environment: 0x5a65418> | |
attr(,"mayCall") | |
character(0) | |
attr(,"name") | |
[1] "b" | |
attr(,"refClassName") | |
[1] "TestClass" | |
attr(,"superClassMethod") | |
[1] "" | |
attr(,"class") | |
[1] "refMethodDef" | |
attr(,"class")attr(,"package") | |
[1] "methods" | |
=============================================== | |
[1] "a" "b" "initialize" | |
[1] "b" "e" "tc" "TestClass" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment