Version 1 of Multiple inheritance with TclOO - how to resolve ambiguities

Updated 2011-01-20 08:15:40 by arjen

Arjen Markus (20 january 2011) A further experiment with TclOO: I remember from a book on Eiffel that multiple inheritance in C++ is actually a potential nightmare: what happens if you have two superclasses with methods or variables with the same name? This leads to ambiguities and they are not elegantly dealt with in C++ (at least that is how I remember it).

The code below constructs a class that has two superclasses, each with a variable myname and a method print. The method seems to be resolved as the print method from the first superclass, but then I get stuck trying to determine which version of the variable myname this composite class gets: I can not reach this variable and the man pages do not seem to give me the information.

A more general question therefore: do we need to explicitly create methods to get the values of an object's variables or is there some syntax that I overlook? (I can live with both, but I'd like to know what is expected). And for this specific case: can we get at the second superclass's print method? (Via next perhaps?) and how do we get at the superclasses's variables at all?

(I hope Donal is not offended by this series of questions - he put a lot of work in TclOO and I am merely going where I have never gone before)

# multiple_inheritance.tcl --
#     How does TclOO behave if two superclasses have the same features?
#

#
# First superclass
#
oo::class create first {
    variable myname

    constructor {} {
        set myname "First"
    }

    method print {} {
        puts $myname
    }
}
#
# Second superclass
#
oo::class create second {
    variable myname

    constructor {} {
        set myname "Second"
    }

    method print {} {
        puts "Hi, my name is $myname"
    }
}

#
# Class with two superclasses
#
oo::class create third {
    superclass first second

    method check {} {
        #
        # This does not work: myname not available!
        #
        puts "Value of myname: $myname"
    }
}

#
# Create an object and check its methods
#
set chk [third new]

$chk print
$chk check
puts "Direct access to myname? [$chk myname]"