Generic collection/element function signatures in D2 versus D1

Pelle pelle.mansson at gmail.com
Tue Sep 7 08:37:20 PDT 2010


On 09/07/2010 04:33 PM, Steven Schveighoffer wrote:
> Yes, a valid return. Your function should be:
>
> void foo(void delegate(const(C) f) const
>
> It helps to understand that inout/const/immutable has NOTHING to do with
> code generation, it only has to do with limiting what compiles. For this
> reason, an inout function is compiled once, and works on all three
> constancies (4 if you have a nested inout function). For the entire
> function any inout variable is treated as a non-changeable value, just
> like const. Then when you return, it's converted at the call site back
> to the constancy with which it was called. If the return value is void,
> then there's nothing to convert, and no reason to use inout over const.
>
> I'll repeat -- there is no benefit to inout if you are not returning
> anything.
>
> -Steve

That's not an equivalent function signature. Or maybe it is, but look at 
this (sorry it's so long):

class C {
     int x;
     this(int y) { x = y; }

     inout(int*) foo() inout {
         return &x;
     }
     void bar(void delegate(int*) f) {
         f(&x);
     }
     void bar(void delegate(const(int*)) f) const {
         f(&x);
     }
     void bar(void delegate(immutable(int*)) f) immutable {
         f(&x);
     }
}

void main() {

     immutable(int)* wah;
     void wahwah(immutable(int*) x) {
         wah = x;
     }
     auto c = new immutable(C)(10);
     wahwah(c.foo);  // why is this privilegied with inout
     c.bar(&wahwah); // and this not?

     writeln(*wah);

}

Can't use void delegate(const(int*)) there.


More information about the Digitalmars-d-learn mailing list