Generality creep

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sat Mar 30 18:52:22 UTC 2019


On Thursday, March 21, 2019 10:11:04 AM MDT Joseph Rushton Wakeling via 
Digitalmars-d wrote:
> 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).

Honestly, this is another example of over-generality causing problems.
length shouldn't ever be anything other than size_t. Anything else causes
problems.

- Jonathan M Davis





More information about the Digitalmars-d mailing list