[Gambas-user] INHERITS, name resolution

Benoit Minisini gambas at ...1...
Fri Jun 25 17:30:49 CEST 2004


On Thursday 24 June 2004 20:24, Christian Faure wrote:
> Hi All,
> Before all, The Gambas is VERY GOOD, thanks.
>
> I have a small test for objet inheritance based on 2 classes:
>
> --------BEGIN FILE Class1-------------
> ' Gambas class file
> PRIVATE d1 AS String
>
> PUBLIC SUB m1()
>   PRINT "C1::m1 "
> END
> -------- END FILE Class1-------------
> --------BEGIN FILE Class2-------------
> ' Gambas class file
> INHERITS Class1
> PRIVATE d2 AS String
>
> PUBLIC SUB m2()
>   PRINT "C2::m2 "
> END
> -------- END FILE Class2-------------
> -------- In a form  i have a button1---
> PUBLIC SUB Button1_Click()
>   DIM c AS Object
>   c=NEW class2
>   c.m1()
> END
> ------- END source code -----------
>
> The Problem:
> when click the button
> i expect in console "C1::m1"
>
> but, i see "C2::m2"
>
> :-(
>
> It is a bug ?
>

Yes it is a big bug! It seems to be related to the Object anonymous datatype, 
because when you use static defined variable (i.e. AS Class1 or Class2), it 
works as it should.

> I have two workaround's
> workaround 1:
> -------- In a form  i have a button1---
> PUBLIC SUB Button1_Click()
>   DIM c AS Object
>   dim d as Class1
>   c=NEW class2
>   d=c ' d and c references the same object, but d as declared as Class1
>   d.m1()
> END
> ------- end source ----
> workaround 2:
> In the class2 use a "ancestor" variable and re-declare method m1 to call
> ancestor.m1()
> --------BEGIN FILE Class2-------------
> ' Gambas class file
> INHERITS Class1
> PRIVATE d2 AS String
>
> PUBLIC SUB m1()
>   dim ancestor as Class1
>   ancestor=me
>   ancestor.m1()
> END
>
> PUBLIC SUB m2()
>   PRINT "C2::m2 "
> END
> -------- END FILE Class2-------------
>
> i think it is a small bug :->, i are interesting to solve it,
> Can help me to find the algorithm of name resolution
> of methods in gambas source code?

It could be more complicated than you think. Or it could be simple.

Each class has its own symbol table, whose each element points at a structure 
describing the symbol.

When a class A inherits a class B, the class B symbol table is merged with the 
specific symbols defined in class A. The common symbols of class A & B has 
the same index in the symbol table.

When you use the Object datatype, the symbol table is always searched. When 
you use the static datatype A or B, the symbol table is searched only the 
first time. After, the index is used.

As Class1.m1() and Class2.m1() has got the same index in the symbol table, 
calling m1() with an Object variable that points at a Class2 object should 
show "C1::m1". So there is a bug there.

I must looked at the code first.

Regards,

-- 
Benoit Minisini
mailto:gambas at ...1...




More information about the User mailing list