Proposal to add 'Elements of Programming' Concepts to std.traits

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Jun 17 06:13:21 PDT 2012


On 6/17/12 1:38 AM, Philippe Sigaud wrote:
> On Sat, Jun 16, 2012 at 9:22 PM, bearophile<bearophileHUGS at lycos.com>  wrote:
>
>> I see. is that semantically different from this (beside being shorter)?
>>
>> struct NoZero {
>>     int value;
>>     this(int x) { value = x; }
>>     alias value this;
>>     invariant() { assert(value != 0); }
>> }
>> void main() {
>>     auto a = NoZero(5);
>>     auto b = NoZero(0);
>> }
>
> The invariant isn't invoked through alias this, it seems:
>
> void main() {
>     auto a = NoZero(5);
>     a = 0; // compiles and runs happily
> }

To avoid this, expose an rvalue via alias this, not an lvalue. Then 
implement opAssign etc.

Andrei


More information about the Digitalmars-d mailing list