2
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 ?

user2725682
  • 139
  • 1
  • 9
  • Isn't "It does not fail if I add a default value for the a parameter in initialize.A method" the solution? Maybe you want to have a look at: http://stackoverflow.com/questions/18197214/how-to-define-an-s4-prototype-for-inherited-slots – sgibb Aug 28 '13 at 14:08
  • the strange thing is merely adding the line setClass... without even creating an object triggers the error. And suppose I am reusing code that I subclass, I am not going to modify it – user2725682 Aug 28 '13 at 14:32

1 Answers1

1

"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,

Community
  • 1
  • 1
Martin Morgan
  • 45,935
  • 7
  • 84
  • 112
  • Thanks for the answer. Having read the discussion, looks like it's best to simply avoid initialize methods with inheritance – user2725682 Aug 29 '13 at 11:25