digitalmars.D.learn - Cannot pass by reference
- Jeroen Bollen (18/18) Nov 16 2013 I cannot seem to pass values to functions by referece.
- bearophile (5/6) Nov 16 2013 It's caused by the cast. The solution is to use an auxiliary
- Jeroen Bollen (3/9) Nov 16 2013 Same error, but "is not callable using argument types
- bearophile (4/6) Nov 16 2013 Have you used an auxiliary value inside the main with the cast?
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (28/46) Nov 16 2013 First, just a reminder: Classes in D are reference types so in most
- Meta (2/5) Nov 16 2013 Just out of curiosity, what does ref Object actually do? Is it
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (29/33) Nov 16 2013 It allows changing the actual object that the caller had. Actually, my
- Meta (3/4) Nov 17 2013 Oh, right. I don't think I've ever done that before in any
- Andrej Mitrovic (4/5) Nov 17 2013 Technically no, it changes the reference in the caller, not the actual
- Jeroen Bollen (2/5) Nov 18 2013 Thanks, this is what I needed!
I cannot seem to pass values to functions by referece. ---------------------------------------------------------------------------------- safe public nothrow this(ref Socket socket) { // Inside class modulename.classname this.socket = socket; } ---------------------------------------------------------------------------------- void main() { auto variablename = new modulename.classname( cast(Socket) new TcpSocket() // main.d line 5 ); } ---------------------------------------------------------------------------------- This code gives me a compile error: main.d(5): Error: constructor modulename.classname.this (ref Socket socket) is not callable using argument types (Socket) main.d(5): Error: no constructor for classname Why is that?
Nov 16 2013
Jeroen Bollen:Why is that?It's caused by the cast. The solution is to use an auxiliary value inside the main, or remove the cast. Bye, bearophile
Nov 16 2013
On Saturday, 16 November 2013 at 14:28:45 UTC, bearophile wrote:Jeroen Bollen:Same error, but "is not callable using argument types (TcpSocket)"...Why is that?It's caused by the cast. The solution is to use an auxiliary value inside the main, or remove the cast. Bye, bearophile
Nov 16 2013
Jeroen Bollen:Same error, but "is not callable using argument types (TcpSocket)"...Have you used an auxiliary value inside the main with the cast? Bye, bearophile
Nov 16 2013
On 11/16/2013 06:08 AM, Jeroen Bollen wrote:I cannot seem to pass values to functions by referece. ---------------------------------------------------------------------------------- safe public nothrow this(ref Socket socket) { // Inside class modulename.classname this.socket = socket; } ---------------------------------------------------------------------------------- void main() { auto variablename = new modulename.classname( cast(Socket) new TcpSocket() // main.d line 5 ); } ---------------------------------------------------------------------------------- This code gives me a compile error: main.d(5): Error: constructor modulename.classname.this (ref Socket socket) is not callable using argument types (Socket) main.d(5): Error: no constructor for classname Why is that?First, just a reminder: Classes in D are reference types so in most cases there is no need for ref; it already is a reference to the actual object. Assuming that you really want to pass the class reference by ref, so that the function wants to change the actual object, the following workes: class C {} class D1 : C {} class D2 : C {} void foo(ref C c) { // Change the caller's object c = new D2(); } void main() { C c = new D1(); foo(c); } The problem with your example is that unlike main.c in my example, what you pass is an rvalue, which may not be bound to the ref parameter. So, if you didn't want ref to begin with, just drop it. If you really wanted to change the caller's object, then provide an lvalue (e.g. a regular local variable) so that it can be changed. Ali
Nov 16 2013
First, just a reminder: Classes in D are reference types so in most cases there is no need for ref; it already is a reference to the actual object.Just out of curiosity, what does ref Object actually do? Is it just a no-op?
Nov 16 2013
On 11/16/2013 11:21 PM, Meta wrote:It allows changing the actual object that the caller had. Actually, my previous code did demonstrate this, but it lacked some print statements: import std.stdio; class C {} class D1 : C {} class D2 : C {} void foo(ref C c) { writefln("foo is changing the caller's object"); c = new D2(); } void main() { C c = new D1(); writefln("Before calling foo: %s", c); foo(c); writefln("After foo returned: %s", c); } The output: Before calling foo: deneme.D1 foo is changing the caller's object After foo returned: deneme.D2 If the classes had virtual functions, then main's object would start behaving differently. AliFirst, just a reminder: Classes in D are reference types so in most cases there is no need for ref; it already is a reference to the actual object.Just out of curiosity, what does ref Object actually do?
Nov 16 2013
On Sunday, 17 November 2013 at 07:32:22 UTC, Ali Çehreli wrote:It allows changing the actual object that the caller had.Oh, right. I don't think I've ever done that before in any language. Thanks.
Nov 17 2013
On 11/17/13, Ali Çehreli <acehreli yahoo.com> wrote:It allows changing the actual object that the caller had.Technically no, it changes the reference in the caller, not the actual object itself. That object is likely still alive until collected if there are no other references to it.
Nov 17 2013
On Saturday, 16 November 2013 at 16:34:31 UTC, Ali Çehreli wrote:The problem with your example is that unlike main.c in my example, what you pass is an rvalue, which may not be bound to the ref parameter.Thanks, this is what I needed!
Nov 18 2013