[Issue 7177] $ should forward to length by default

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Apr 2 04:06:09 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=7177



--- Comment #65 from Andrei Alexandrescu <andrei at erdani.com> 2013-04-02 04:06:03 PDT ---
(In reply to comment #63)
> Your proposal is to make opDollar refer to length whenever there is a length,
> 
>     auto ref opDollar(R)(auto ref R r) if (is(typeof(r.length))) {
>         return r.length;
>     }

Well whenever there's a length and [].

> by default allowing things like:
> 
>     int[int] x = [1:0,3:2];
>     x[$]=1;
> 
>     assert(x==[1:0,2:1,3:2]);

That's not necessarily the case. Under the proposed semantics built-in hash
tables would disable opDollar.

BTW disabling opDollar should mean "pass through". Consider:

    import std.stdio;

    void main(){
        int[size_t] a;
        int[] b = [ 1, 2, 3, 4, 5 ];
        a[5] = 1;
        a[2] = 2;
        writeln(b[a[a.length]]);
        writeln(b[a[b.length]]);
        writeln(b[a[$]]);
    }

This program prints "3 2 2" so the $ is transparently interpreted as the
array's length. I'm not a fan of that particular behavior, but probably we need
to preserve it.

> As far as I understand it, his proposal is to restrict $ to ranges.
> 
>     auto ref opDollar(R)(auto ref R r) if (isInputRange!R && hasLength!R) {
>         return r.length;
>     }
> 
> std.range vs. object.d is a separate issue.

Is that correct, Kenji? (I don't think we should mix ranges into this without
necessity.)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list