Page 1 of 1

Inheriting initializers

PostPosted: Wed Mar 17, 2010 2:10 pm
by Charles
I've enhanced Cobra with regards to how initializers are inherited. Classes that don't declare any initializers used to get exactly one parameterless initializer automatically provided by the compiler. This can be rather annoying when building a class hierarchy. Imagine you have these class declarations:
class NamedPoint

cue init(name as String, x as int, y as int)
...
cue init(x as int, y as int)
...

class MyNamedPoint inherits NamedPoint
pass

You should be able to do this out of the box:
p = MyNamedPoint('origin', 0, 0)
q = MyNamedPoint(1, 1)

In fact, there is no reason to believe that a parameterless initializer is a good guess when the base class does not have any. Previously, you could not make those calls without stubbing out a "cue init" in the subclass for every base init by hand.

Note that as soon as you declare even one "cue init..." then none of the base class initializers are inherited. This enables you to dictate exactly how a class' objects are to be initialized.

It's unlikely that this change breaks existing code. You would need to have:

-- a class with no "cue inits", and
-- you are are instantiating it, and
-- the base class has no parameterless init

Nevertheless, I also included a "cobra -legacy-one-default-initializer ..." option to get the old behavior back if (a) you do experience a break and (b) don't want to update your current code.