www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Cannot pass by reference

reply "Jeroen Bollen" <jbinero gmail.com> writes:
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
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent reply "Jeroen Bollen" <jbinero gmail.com> writes:
On Saturday, 16 November 2013 at 14:28:45 UTC, bearophile wrote:
 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
Same error, but "is not callable using argument types (TcpSocket)"...
Nov 16 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
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
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
next sibling parent reply "Meta" <jared771 gmail.com> writes:
 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
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/16/2013 11:21 PM, Meta wrote:

 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?
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. Ali
Nov 16 2013
next sibling parent "Meta" <jared771 gmail.com> writes:
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
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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
prev sibling parent "Jeroen Bollen" <jbinero gmail.com> writes:
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