Thread overview
std.stdio breaks casting operation
Oct 23, 2014
tcak
Oct 23, 2014
anonymous
Oct 23, 2014
Marc Schütz
October 23, 2014
The main function has following:

- main.d -

	import test;
	auto t = new shared test.Test();
	auto sock = new std.socket.TcpSocket( std.socket.AddressFamily.INET6 );
	t.setIt( sock );


- test.d -

module test;

import std.socket;

public class Test{
	private std.socket.Socket s;

	public void setIt(S)( S sock) shared
		if(
			is(S: std.socket.Socket) ||
			is(S: shared(std.socket.Socket))
		)
	{
		s = cast( typeof( s ) )sock;
	}
}


Above code works properly (I mean there is no error at all).

=====

Then I change the "Test.setIt" method as follows:

		import std.stdio;
		s = cast( typeof( s ) )sock;


Error on "s = cast..." line:
cannot cast module socket of type void to shared(Socket)

===

What is casting a module? How is this happening? Is this a bird, superman, or a bug?
October 23, 2014
On Thursday, 23 October 2014 at 18:43:54 UTC, tcak wrote:
> Then I change the "Test.setIt" method as follows:
>
> 		import std.stdio;
> 		s = cast( typeof( s ) )sock;
>
>
> Error on "s = cast..." line:
> cannot cast module socket of type void to shared(Socket)

Apparently std.stdio defines an alias `sock` to some module. When
you import std.stdio, its `sock` takes precedence over the
parameter `sock`.

I don't know if symbols from local imports should override
parameters just like that. There may be some room for improvement
here.

In the meantime, you can make the import static/renamed/selective:

static import std.stdio; /* must write `std.stdio.foo` */
import io = std.stdio; /* must write `io.foo` */
import std.stdio: foo; /* only foo is imported */
October 23, 2014
On Thursday, 23 October 2014 at 19:20:54 UTC, anonymous wrote:
> On Thursday, 23 October 2014 at 18:43:54 UTC, tcak wrote:
>> Then I change the "Test.setIt" method as follows:
>>
>> 		import std.stdio;
>> 		s = cast( typeof( s ) )sock;
>>
>>
>> Error on "s = cast..." line:
>> cannot cast module socket of type void to shared(Socket)
>
> Apparently std.stdio defines an alias `sock` to some module. When
> you import std.stdio, its `sock` takes precedence over the
> parameter `sock`.

It's been discussed recently, but I can't find it now. It's a natural result of the scope hierarchy: parameters are in a higher scope than the function body, into which the identifiers from std.stdio get imported.

It's been recognized that this is often unexpected, and different lookup rules have been debated, but I think there was no clear conclusion.

But in this particular case, it has already been fixed, and will work in the next release:
https://github.com/D-Programming-Language/phobos/pull/2395