struct inheritance

Bruno Medeiros brunodomedeiros+spam at com.gmail
Sat Sep 1 06:09:43 PDT 2007


Bruno Medeiros wrote:
> Reiner Pope wrote:
>> Bruno Medeiros wrote:
>>> Reiner Pope wrote:
>>>> Bill Baxter wrote:
>>>>> Let us all turn our copies of WalterAndrei.pdf to page 40 where yea 
>>>>> verily it doth say:
>>>>>
>>>>>     struct M { int a; }
>>>>>     struct S {
>>>>>        M m;
>>>>>        alias m this;
>>>>>        int b;
>>>>>     }
>>>>>
>>>>> Why not just allow mixin to do this?
>>>>>
>>>>>     struct M { int a; }
>>>>>     struct S {
>>>>>        mixin M;
>>>>>        int b;
>>>>>     }
>>>>>
>>>>> That's basically the way it's done now except now it would be more 
>>>>> like:
>>>>> template MMixin() { int a; }
>>>>> struct M { mixin M; }
>>>>> struct S { mixin M; int b; }
>>>>>
>>>>> Just let us treat a struct like a zero arg template for mixin 
>>>>> purposes.
>>>>>
>>>>> --bb
>>>>
>>>> Template mixins require that the sourcecode of the template is 
>>>> available to the compiler. Aliases don't. In this sense at least, 
>>>> aliases are cleaner.
>>>>
>>>>  -- Reiner
>>>
>>> Huh? Explain how the above alias example would work if the compiler 
>>> doesn't know the source code of what is being aliased:
>>>
>>>   struct M;
>>>   struct S {
>>>     M m;
>>>     alias m this;
>>>     int b;
>>>   }
>>>
>>> It's not even possible to declare m without knowing the source of M! 
>>> Perhaps I misunderstood you?
>>>
>> All you need is a declaration of M's interface, you don't need to 
>> distribute the source code of your implementation, whereas template 
>> mixins require that the implementation source is also available.
>>
>>    -- Reiner
> 
> 
> You mean like ?
> 
> --- foo.di ---
>   struct M {
>     int a;
>     void func();
>   }
> --- ----
>   import foo;
>   struct S {
>     M m;
>     alias m this;
>     int b;
>   }
> 
> And then linking with a foo.obj that has an implementation for M.func?
> 

Hum, that being the case, then yes it's different, mostly due to the 
fact that mixin templates always generate new instances of their 
content, and this "import alias" would not. Another noticeable 
difference would be if the content had static members: mixin templates 
would generate new "instances" of the static member, whereas the "import 
alias" would always refer to the same static member.

-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list