www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.stdio breaks casting operation

reply "tcak" <tcak gmail.com> writes:
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?
Oct 23 2014
parent reply "anonymous" <anonymous example.com> writes:
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 */
Oct 23 2014
parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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
Oct 23 2014