Examples for 'methods::representation'


Construct a Representation or a Prototype for a Class Definition

Aliases: representation prototype

Keywords: programming classes

### ** Examples

## representation for a new class with a directly define slot "smooth"
## which should be a "numeric" object, and extending class "track"
representation("track", smooth ="numeric")
[[1]]
[1] "track"

$smooth
[1] "numeric"
## Don't show: 
prev <- getClassDef("class3")
setClass("class1", representation(a="numeric", b = "character"))
setClass("class2", representation(a2 = "numeric", b = "numeric"))
try(setClass("class3", representation("class1", "class2")))
Error in reconcilePropertiesAndPrototype(name, slots, prototype, superClasses,  : 
  Definition of slot 'b', in class "class3", as "character" conflicts with definition, inherited from class "class2", as "numeric"
{if(is.null(prev))
  stopifnot(!isClass("class3"))
else
  stopifnot(identical(getClassDef("class3"), prev))}
## End(Don't show)

###  >>> This *is* old syntax -- use 'contains=*, slots=*' instead <<<
###                ==========         ----------  ------   ======


setClass("Character",representation("character"))
setClass("TypedCharacter",representation("Character",type="character"),
          prototype(character(0),type="plain"))
ttt <- new("TypedCharacter", "foo", type = "character")
## Don't show: 
stopifnot(identical(as(ttt, "character"), "foo"))
## End(Don't show)

setClass("num1", representation(comment = "character"),
         contains = "numeric",
         prototype = prototype(pi, comment = "Start with pi"))

## Don't show: 
stopifnot(identical(new("num1"), new("num1", pi, comment = "Start with pi")))
for(cl in c("num1", "TypedCharacter", "Character", "class2", "class1"))
    removeClass(cl)
## End(Don't show)

[Package methods version 4.2.3 Index]