Defining some stuff for each class in turn

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Oct 1 12:54:13 PDT 2009


Denis Koroskin wrote:
> On Thu, 01 Oct 2009 20:25:03 +0400, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org> wrote:
[...]
>> What do you think?
>>
>>
>> Andrei
> 
> This is cool. I'd also add Serialization task to the application list 
> (currently I manually insert "mixin Serializable!();" into every class.

Yah, serialization is close to reflection so it's definitely part of the 
target applications of the features.

> There is one thing that I dislike about it, though. I believe mixin(Foo) 
> {} syntax is a bit far-fetched, it is not obvious that Foo represents 
> derived type inside a mixin scope.
> 
> I'd like to remind you that we could just use typeof(this) instead of 
> Foo/Derived/etc:
> 
> static if (is (typeof(this) == Counted) {
>     ref uint getCounter() { return counter; }
> } else {
>     override ref uint getCounter() { return counter; }
> }

Yah, I thought a lot along the likes of typeof(this). We need some 
symbolic alias for "the struct or class currently being defined" and 
typeof(this) is an obvious choice. I just fear that it might be 
confusing that typeof(this) works even outside any method, i.e. when 
there is no "this".

> If you remember, we had a similar discussion about a half year ago. It 
> was about ICloneable interface that forces all derived class to 
> implement proper clone() method. We could define it as:
> 
> // forces implementor class and all the derivatives to implement proper 
> clone() method
> interface ICloneable
> {
>     scope mixin {    // alternative syntax
>         typeof(this) clone();
>     }
> }

Yah, I remember.

> and also be able to declare a method non-recursively (so that derived 
> class don't have to implement that method):
> 
> interface ICloneableNonRecursive
> {
>     typeof(this) clone();
> }
> 
> Other possible solution could be to use a special "derived" keyword. We 
> already have a class-level compile-time constant - "super". We could 
> also introduce its counter-part ("derived"), which will expand into a 
> concrete derived class type. This will simplify the syntax a bit and 
> will allow to get rid of mixin and an addition level of indentation:
> 
> interface ICloneable
> {
>     derived clone();
> }
> 
> interface IComparable
> {
>     int opCmp(derived other);
> }
> 
> As a downside, I don't know how to define a field in every class this 
> way ("int derived.counter;" maybe?).

I dislike adding yet another keyword. In fact, I want to propose Walter 
to eliminate "super" as a keyword and make it just an alias for the base 
class as if you defined it by hand.


Andrei



More information about the Digitalmars-d mailing list