equivariant functions

Denis Koroskin 2korden at gmail.com
Tue Oct 14 21:41:59 PDT 2008


On Wed, 15 Oct 2008 05:44:22 +0400, Steven Schveighoffer  
<schveiguy at yahoo.com> wrote:

> "Denis Koroskin" wrote
>> Now that I thought about it a little more (please, see and comment my  
>> post
>> about typeof(this) nearby), I agree that the issues are related.
>>
>> However, the best solution could be a combination of both.
>>
>> For example, I agree that interface IClonable should be as follows:
>>
>> interface IClonable { typeof(this) clone() const; }
>
> No.  IClonable should be:
>
> IClonable clone() const;
>
> or if you prefer:
>
> Object clone() const;
>
> It can't be anything else, because IClonable cannot define how many  
> levels
> deep it goes.
>
Nope, you missed the idea. Was my post too long?

> For example, if I have:
>
> class A : IClonable
> {
>   typeof(this) clone() const { return new A();}
> }
>
> class B : A
> {
> }
>

This shouldn't compile - Error: class B doesn't implement typeof(this)  
clone() method.

> B b = new B;
> B x = b.clone();
>

Error: can't instantiate abstract class B.

> Oops, b.clone() really only returns A, so this should fail.  The real
> signature in A should be:
>
> class A : IClonable
> {
>    A clone() const { return new A(); }
> }
>

No, you missed the idea of the discussion.

> IClonable is a perfect candidate for covariant functions, which is  
> already defined.
>
> -Steve
>
>



More information about the Digitalmars-d mailing list