Hi Tariq
Some discussion of this topic a while ago on the R-devel newsgroup;
bottom line is that there is no consensus and a certain amount of
resistance to make R conform to the implementation of other languages
(and not exploit R's unique strengths).
https://stat.ethz.ch/pipermail/r-devel/2006-September/042854.html
https://stat.ethz.ch/pipermail/r-devel/2006-September/042864.html
You could implement something fancier (e.g., a method for "$" or
"[["
or "@" or a suite of generics "get_x"), but I kind of like
(because
'slot' is a function with a well-defined purpose, so why not make it a
generic?)
setGeneric("slot")
setMethod("slot",
signature=signature(object="A", name="character"),
function(object, name)
## do anything, e.g., restrict access to specific slots
## alternatively, dispatch on name with 'switch'
if (name %in% slotNames(class(object))) callNextMethod()
else stop("slot '", name, "' undefined"))
setGeneric("slot<-",
signature=c("object", "name", "value"))
setReplaceMethod("slot",
signature(object="A", name="character",
value="ANY"),
function(object, name, check=TRUE, value)
if (name %in% slotNames(class(object))) callNextMethod()
else stop("slot '", name, "' cannot be
assigned"))
and then
> setClass("A",
+ representation=representation(x="numeric"))
[1] "A"> a <- new("A", x=1:5)
> slot(a, "y")
Error in slot(a, "y") : slot 'y'
undefined> slot(a, "x") <- 5:1
> slot(a, "x")
[1] 5 4 3 2 1
A class could be defined to implement the dispatch, so other classes
just have to inherit from that.
Discussion may be veering toward the R-devel newsgroup.
Martin
"?Tariq Khan" <tariq.khan at gmail.com> writes:
> Dear R users!
>
> Several languages like C# or VB allow one to create properties; typically
> these are under 'get', 'set' tags. My question is this
really, what are
> similar or comparable strategies in R, and what might be preferred? I have
a
> couple of situations where I want a certain computation to belong to a
> class, but I do not really want to seperate it out to a stand-alone
> function.
>
> Here are a couple of options I see possible in R, and if I use the last
> described option (see option C below), is there a way to access the object
> with something like in C# or C++ where one would probably use the
'this'
> keyword or the 'me' keyword in VB from within the property?:
>
> A) MyObject at Property is static and must be handled carefully if it
depends
> on other slots of the class. If one of the slots changes, then because this
> slot is static (containing only the value), it will not change to reflect
> the update.
>
> B) MyObject at Property is deprecated, and special accessor functions are
used.
> The bio group seems to favor this option. So one would use:
> Property(MyObject). The drawback I see with this is that there is no strong
> relationship between Property and MyObject; they are not explicitly related
> via a class structure, and in some way this defeats the point of class
> structures.
>
> C) Let MyObject at Property be a function, so in order to fetch the slot
object
> we would use MyObject at Property(); the question here is can I use
something
> like a 'this' keyword to access other slots of thr object without
having to
> pass the whole class as a parameter to this slot function, ie the
> round-about, clumsy way would have to look like MyObject at
Property(MyObject).
> Ideally, the pseudo-code I have in mind might look like this for an object
> with two slots (A and B)
>
> Object at A = function() return(.this at B)
>
>
> Thoughts and considerations would be very interesting.
>
> -Tariq
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Martin T. Morgan
Bioconductor / Computational Biology
http://bioconductor.org