Re: [R] Enumeration in R

From: Paul Roebuck <>
Date: Thu 08 Jul 2004 - 03:20:17 EST

On Tue, 7 Jul 2004, Peter Dalgaard wrote:

> Paul Roebuck <> writes:
> > I want the equivalent of this 'C' declaration.
> > enum StoplightColor {
> > green = 3,
> > yellow = 5,
> > red = 7
> > };
> I think you *dis*abled it by specifying an initializer which doesn't
> check the validity:

Thanks. It all seems obvious once it's pointed out to you. I have a couple related questions:

  1. When I was reading Chambers book (pg 288), my initial impression was that there was a way I could have done this class without specifying the representation in 'setClass' - I just couldn't figure out how to assign values that way. Could this class have been defined 'slotless'? If so, how?
  2. How do you define a class and instantiate some, yet prevent more from being created after that. Possibly better stated, from the package's API view, I would like these to be instance variables of opaque types. So I would like to create my 'global' constants in an initialization routine then prevent use of 'new' to create any more. I tried the following with no success. Possible?


          function(Class, ...) stop("can't make any more"))

3) This seems kind of painful for trivial stuff. My idea was to move some of the validation error checking out of my project by converting certain function arguments into classes that could be validated upon creation, improving the clarity of project routines. What is the canonical style used in R package authoring?

green <- stoplightColor(3)
yellow <- stoplightColor(5)
red <- stoplightColor(7)

SIGSIG -- signature too long (core dumped) mailing list PLEASE do read the posting guide! Received on Thu Jul 08 03:24:01 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:54:45 EST