user2725682
user2725682

Reputation: 139

R cannot declare setClass with inheritance

removeClass("A")
setClass('A',representation=representation(a="numeric"))

setMethod('initialize','A', function(.Object,...,a){

.Object@a=a
.Object
})

ok up to here

removeClass("B")

setClass('B',representation=representation(b="numeric"),contains="A")

This code fails on the definition of class "B" , without I had any chance to add an initialize method for "B", and without I even create any object

Error in .local(.Object, ...) : argument "a" is missing, with no default

It does not fail if I add a default value for the a parameter in initialize.A method

Can anyone explain why ?

Upvotes: 2

Views: 286

Answers (1)

Martin Morgan
Martin Morgan

Reputation: 46866

"why" is a tricky question, with the answer being "because" it's implemented that way. If you're asking what needs to be done to avoid this, the answer is to either (a) provide a default value to the argument a in the constructor or (b) avoid using an initialize method. There is some discussion here. The implicit contract is that new("A") succeeds,

Upvotes: 1

Related Questions