Incorporating D

Rob T alanb at ucora.com
Fri Jan 25 15:48:57 PST 2013


On Friday, 25 January 2013 at 22:29:44 UTC, Adam D. Ruppe wrote:
> On Friday, 25 January 2013 at 22:22:44 UTC, Szymon wrote:
>> So structs in D are always passed by-value? That is 
>> unfortunate...
>
> It has both pointers and ref but they both only work with 
> lvalues, regardless of const:
>
> struct S {}
>
> void test(const ref S s) {}
> void test2(const S* s) {}
>
> S getS() { return S(); }
>
> void main() {
>         S s;
>         test(s); // ok
>         test2(&s); // ok
>         test(getS()); // not ok (line 12)
>         test2(&getS()); // not ok (line 13)
> }
>
> test.d(12): Error: function test.test (ref const(S) s) is not 
> callable using argument types (S)
> test.d(12): Error: getS() is not an lvalue
> test.d(13): Error: getS() is not an lvalue

It should be mentioned that there's a solution of sorts, but it 
is a pain to have to do and does not scale up when you have 
multiple ref arguments.

void test(const ref S s)
{
    // implementation
    ...
    return;
}
void test(const S s)
{
     test( s ); // calls test(const ref S s)
     return;
}

--rt


More information about the Digitalmars-d mailing list