On Tue, 7 Jul 2004, Peter Dalgaard wrote: > Paul Roebuck <[EMAIL PROTECTED]> 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? setMethod("new", "stoplightColor", 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? --------- stoplightColor.R ------------------------------------ setClass("stoplightColor", representation(value = "integer"), prototype = integer(1)) stoplightColor <- function(value) { new("stoplightColor", value) } valid.stoplightColor <- function(object) { valid <- switch(as([EMAIL PROTECTED], "character"), "3" = TRUE, "5" = TRUE, "7" = TRUE, FALSE) if (valid == FALSE) return('Invalid value - must be [3|5|7]'); return(TRUE); } setValidity("stoplightColor", valid.stoplightColor) initialize.stoplightColor <- function(.Object, value) { if (missing(value) || is.na(value)) stop('Argument "value" is missing or NA') [EMAIL PROTECTED] <- as.integer(value) validObject(.Object) .Object } setMethod("initialize", signature(.Object = "stoplightColor"), initialize.stoplightColor) stoplightColor.as.integer <- function(from) { return([EMAIL PROTECTED]) } setAs("stoplightColor", "integer", stoplightColor.as.integer) green <- stoplightColor(3) yellow <- stoplightColor(5) red <- stoplightColor(7) ---------------------------------------------------------- SIGSIG -- signature too long (core dumped) ______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html