digitalmars.D.learn - out parameter with default value
- AsmMan (14/14) Sep 29 2014 Why it does works:
- AsmMan (1/1) Sep 29 2014 My question is why it doesn't works and if there's a workaround
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (8/9) Sep 29 2014 For the same reason `ref` wouldn't work in this place: It doesn't
- AsmMan (1/1) Sep 29 2014 I had just forget out and ref are same as pointers... thanks guys
- ketmar via Digitalmars-d-learn (4/5) Sep 29 2014 On Mon, 29 Sep 2014 20:22:41 +0000
- ketmar via Digitalmars-d-learn (5/8) Sep 29 2014 On Mon, 29 Sep 2014 16:07:49 +0000
Why it does works: void f(out int c) { if(some_cond) c = 10; } but it doesn't? void f(out int c = 1) { if(some_cond) c = 10; } it give compiler error: Error: constant 1 is not an lvalue
Sep 29 2014
My question is why it doesn't works and if there's a workaround
Sep 29 2014
On Monday, 29 September 2014 at 16:09:11 UTC, AsmMan wrote:My question is why it doesn't works and if there's a workaroundFor the same reason `ref` wouldn't work in this place: It doesn't accept rvalues. A workaround might be to create a global variable that is initialized to the value you want, and use this as the default value. But this probably isn't what you look for, because you're most likely going to overwrite it, so the next time the function gets called, it will have a different value.
Sep 29 2014
I had just forget out and ref are same as pointers... thanks guys
Sep 29 2014
On Mon, 29 Sep 2014 20:22:41 +0000 AsmMan via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:I had just forget out and ref are same as pointers... thanks guysyeah, that syntactic sugar can be confusing sometimes. ;-)
Sep 29 2014
On Mon, 29 Sep 2014 16:07:49 +0000 AsmMan via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:but it doesn't? =20 void f(out int c =3D 1)'cause `out int c` is actually `int* c`. you can't assign int(1) to pointer.
Sep 29 2014