Generality creep

Steven Schveighoffer schveiguy at gmail.com
Tue Mar 19 14:08:24 UTC 2019


On 3/18/19 10:52 PM, Andrei Alexandrescu wrote:
> @property bool empty(T)(auto ref scope const(T) a)
> if (is(typeof(a.length) : size_t))
> {
>      return !a.length;
> }
> 
> The intent is fairly clear - if a range defines empty as a size_t 
> (somewhat oddly relaxed to "convertible to size_t"), then empty can be 
> nicely defined in terms of length. Cool.

This is implemented wrong.

import std.range;

struct S
{
    size_t length() const { return 0; }
}

void main()
{
    S s;
    assert(s.length == 0); OK
    assert(s.empty); // Error, cannot deduce function
}

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


More information about the Digitalmars-d mailing list