Is this actually supposed to be legal?

Alex Rønne Petersen alex at lycus.org
Mon Jul 16 22:37:38 PDT 2012


On 17-07-2012 07:24, Jonathan M Davis wrote:
> This code strikes me as being a bug:
>
> --------
> class MyBase(T)
> {}
>
> class MySubA : MyBase!MySubA
> {}
>
> class MySubB : MyBase!MySubB
> {}
>
> void main()
> {}
> --------
>
> but it compiles just fine. However, given the fact that MySubA isn't even
> properly defined until its base class has been defined, I don't see how it could
> possibly _not_ be a bug for the base class to be templatized on it. You could
> get some really weird behavior if you use compile time reflection on the
> derived class in the base class definition.
>
> Does anyone know if this is actually supposed to work? Or is it in fact a bug
> like I think it is?
>
> - Jonathan M Davis
>

(Not sure if MySubB was meant to demonstrate anything; it's effectively 
semantically equal to MySubA.)

This code is meant to work. It doesn't actually introduce any circular 
inheritance. Consider, on the other hand, this:

class A : B {}
class B : A {}

or closer to your example:

class A(T) : T {}
class B : A!B {}

The difference is that here you have direct, circular inheritance, while 
in your example, the base type is merely parameterized with the deriving 
type, which is perfectly legal (and trivially resolvable in semantic 
analysis).

-- 
Alex Rønne Petersen
alex at lycus.org
http://lycus.org




More information about the Digitalmars-d-learn mailing list