Operator overhaul

bearophile bearophileHUGS at lycos.com
Tue Oct 21 19:40:11 PDT 2008


Andrei Alexandrescu:
> struct Whatever
> {
>      Whatever opAdd(Whatever rhs) { return member + rhs.member; }
>      Whatever opSubtract(Whatever rhs) { return member - rhs.member; }
>      ...
> }
> and lines and lines of brain damaged repetition, repetition, repetition. 
> If we add opWhateverAssign that will get even worse. The challenge is, 
> then, to define a way to overload operators that makes the classic 
> applications of the feature short and sweet.

I'm a newbie still regarding such stuff.

At a first sight a possible solution is to resort to mathematics: state somehow that class has the property of being a Number, maybe Additive too, then such methods become automatically defined for free... :-)

I think functional languages have similar things, it may be named structural subtyping... I am too much ignorant about this topics, I am sorry. Once I know Haskell well enough I'll be able to talk about this stuff much better.

I suggest you to ask this question in the "Lambda the Ultimate" blog, they are quite expert of such matters, and they are very willing to help. If you say them you want to make D "more functional" they will work for you in the nights too for free :-)

At the moment D1 has class mixins to do something like that, they can find the name of the class by themselves too.

Bye,
bearophile



More information about the Digitalmars-d mailing list