Generality creep

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Thu Mar 21 16:11:04 UTC 2019


On Wednesday, 20 March 2019 at 19:45:13 UTC, Steven Schveighoffer 
wrote:
> On 3/20/19 1:41 PM, Kagamin wrote:
>> 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.

Rather than define a new `property` symbol, why not rewrite the 
template constraint for `empty` as:


@property bool empty (T) (auto ref scope T a)
     if(is(ReturnType!((T t) => t.length) : size_t))
{
     return !a.length;
}

This matches what is done in the `isInputRange` check for the 
existence of `empty` (I suspect it's done that way because it's 
quite common for `SomeRange.empty` to be an enum rather than a 
method or property).


More information about the Digitalmars-d mailing list