Generality creep

Steven Schveighoffer schveiguy at gmail.com
Fri Mar 22 17:53:22 UTC 2019


On 3/21/19 12:03 PM, Kagamin wrote:
> On Wednesday, 20 March 2019 at 19:45:13 UTC, Steven Schveighoffer wrote:
>> Yep, that's a good idea actually. Make it inout, to cut down on 
>> template bloat.
> 
> T property(T)(auto ref T a);
> 
> This still compiles. Will it bloat?

It's not the auto ref, it's the mutability:

auto ref property1(T)(auto ref T a)
{
   pragma(msg, T.stringof ~ " without inout");
   return a;
}

auto ref property2(T)(auto ref inout(T) a)
{
    pragma(msg, T.stringof ~ " with inout");
    return a;
}

void main()
{
    const int a = 1;
    immutable int b = 2;
    int c = 3;

    // 3 instantiations
    assert(a.property1 == a);
    assert(b.property1 == b);
    assert(c.property1 == c);

    // 1 instantiation
    assert(a.property2 == a);
    assert(b.property2 == b);
    assert(c.property2 == c);
}

output:

const(int) without inout
immutable(int) without inout
int without inout
int with inout

-Steve


More information about the Digitalmars-d mailing list