Thread overview
Cannot pass by reference
Nov 16, 2013
Jeroen Bollen
Nov 16, 2013
bearophile
Nov 16, 2013
Jeroen Bollen
Nov 16, 2013
bearophile
Nov 16, 2013
Ali Çehreli
Nov 17, 2013
Meta
Nov 17, 2013
Ali Çehreli
Nov 17, 2013
Meta
Nov 17, 2013
Andrej Mitrovic
Nov 18, 2013
Jeroen Bollen
November 16, 2013
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?
November 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
November 16, 2013
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)"...
November 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
November 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

November 17, 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?
November 17, 2013
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

November 17, 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.
November 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.

November 18, 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!