Generality creep

Steven Schveighoffer schveiguy at gmail.com
Wed Mar 20 19:45:13 UTC 2019


On 3/20/19 1:41 PM, Kagamin wrote:
> On Tuesday, 19 March 2019 at 14:08:24 UTC, Steven Schveighoffer wrote:
>> The problem is a simple one: typeof(a.length) is not size_t, but a 
>> function type.
>>
>> If you put @property on the length function it would work, but I think 
>> we should not require that.
>>
>> -Steve
> 
> struct S
> {
>     size_t length() const { return 0; }
> }
> 
> auto ref property(T)(auto ref T a){ return a; }
> 
> void f()
> {
>     S s;
>     assert(s.length == 0); //OK
>     static assert(is(typeof(property(s.length))==size_t));
> }
> 
> Can this work?

Yep, that's a good idea actually. Make it inout, to cut down on template 
bloat.

I actually use something less nice in iopipe[1], so I may switch to this.

-Steve

[1] 
https://github.com/schveiguy/iopipe/blob/master/source/iopipe/traits.d#L111-L119


More information about the Digitalmars-d mailing list