Closed robertf224 closed 7 years ago
You could use ls(obj)
to get the names of members of the object. Note that there are some special built-in members, like clone
, and initialize
that you probably will want to ignore for your purposes.
@wch How about having a wch::builtin_members
character vector, that just lists the "reserved" member names? Just in case the reserved member names happen to change. This is not very likely, but it might happen. IIRC print
, format
and clone
were added later.
Sorry I'm a complete R beginner, just porting a client library that was originally written in Python, not quite sure how this advice helps me haha. If I understand correctly you're suggesting I could iterate over the members of the object less some reserved list and also use that to determine length?
Was hoping there would be something similar to print
where I could just implement a method with a particular name and there's some contract where I get some functionality (you get this for a wide range of functions/operators in python http://www.diveintopython3.net/special-method-names.html). I.e. in my case if I implement a length
method that returns the length of a list that the object has as a property (there's some other metadata on the object but I don't want that considered), and now my class works with the length
built-in. Or I implement an iter
function and I can iterate over the elements of that list by just doing for element in object
. I gather that maybe it's something that would have to be baked into R6 and so is not very low lift and I'll just have to put up with being marginally more verbose 😛
@gaborcsardi Not a bad idea. Not sure whether things like print
and format
should be considered built-in, though. They're special in that there's an S3 method that will use them if available, but the author would need to write the R6 methods for them. For what purpose do you think this vector be used?
@robertf224 I don't think R has the language support for exactly what you're trying to do. In R, length
and for .. in
are built-in and nothing that you do with an R6 object will make it do what you want. There are other functions in R, like print
, that do allow you to define a method for your class, but not the ones you are asking for.
If you want to learn more about how this works, you should read up on the S3 class system in R. The R6 package actually uses S3 dispatch to call the print
and format
methods on R6 objects. It probably sounds strange, but it's actually very simple.
@gaborcsardi Not a bad idea. Not sure whether things like print and format should be considered built-in, though. They're special in that there's an S3 method that will use them if available, but the author would need to write the R6 methods for them. For what purpose do you think this vector be used?
I don't know, introspection in general, I guess. :)
Based on (https://stackoverflow.com/questions/44333130/equivalent-of-pythons-iter-in-r6-classes-in-r)