Incorporating D

Dmitry Olshansky dmitry.olsh at gmail.com
Sat Jan 26 00:15:00 PST 2013


26-Jan-2013 03:48, Rob T пишет:
> 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;
> }

void test(const S s){ return test(s); }

D is not Java and allows returning void functions directly ;)


-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list