Forums

Inheriting initializers

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Inheriting initializers

Postby Charles » Wed Mar 17, 2010 2:10 pm

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.
Charles
 
Posts: 2510
Location: Los Angeles, CA

Return to Discussion

Who is online

Users browsing this forum: No registered users and 3 guests

cron