Named class vector

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Named class vector

Duncan Murdoch-2
The source to the noquote() function looks like this:

noquote <- function(obj, right = FALSE) {
     ## constructor for a useful "minor" class
     if(!inherits(obj,"noquote"))
         class(obj) <- c(attr(obj, "class"),
                         if(right) c(right = "noquote") else "noquote")
     obj
}

Notice what happens with right = TRUE:

 > x <- noquote("a", right = TRUE)
 > x
[1] a
 > class(x)
     right
"noquote"

The class vector for x is named.  The print method pays attention to the
name, so we get different behaviour for a class of "noquote" and a class
of c(right = "noquote").

I had never noticed a named class vector before, and it raised some
questions for me:

- Is this used anywhere else?
- Are names preserved in all the operations normally done on a class
vector?  (As far as I can see they are, but maybe I've missed something.)
- Is it a good idea to encode a string value worth of information in the
name, rather than setting the class to something like c("noquote",
"right") instead?

Comments would be welcome.

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Named class vector

Kurt Hornik-5
>>>>> Duncan Murdoch writes:

> The source to the noquote() function looks like this:
> noquote <- function(obj, right = FALSE) {
>      ## constructor for a useful "minor" class
>      if(!inherits(obj,"noquote"))
>          class(obj) <- c(attr(obj, "class"),
>                          if(right) c(right = "noquote") else "noquote")
>      obj
> }

> Notice what happens with right = TRUE:

>> x <- noquote("a", right = TRUE)
>> x
> [1] a
>> class(x)
>      right
> "noquote"

> The class vector for x is named.  The print method pays attention to the
> name, so we get different behaviour for a class of "noquote" and a class
> of c(right = "noquote").

> I had never noticed a named class vector before, and it raised some
> questions for me:

> - Is this used anywhere else?

Not that I'd be aware of: I think MMae is the expert here.

> - Are names preserved in all the operations normally done on a class
> vector?  (As far as I can see they are, but maybe I've missed something.)
> - Is it a good idea to encode a string value worth of information in the
> name, rather than setting the class to something like c("noquote",
> "right") instead?

My preference would be to have unnamed class vectors, so that the names
could perhaps eventually be used to store the name of the package which
owns the class.  For noquote, I guess you'd want something like

  c("noquote_right", "noquote")

Best
-k

> Comments would be welcome.

> Duncan Murdoch

> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel