Discussion:
std.stdio breaks casting operation
tcak via Digitalmars-d-learn
2014-10-23 18:43:52 UTC
Permalink
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?
anonymous via Digitalmars-d-learn
2014-10-23 19:20:53 UTC
Permalink
Post by tcak via Digitalmars-d-learn
import std.stdio;
s = cast( typeof( s ) )sock;
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 */
via Digitalmars-d-learn
2014-10-23 19:36:09 UTC
Permalink
Post by anonymous via Digitalmars-d-learn
Post by tcak via Digitalmars-d-learn
import std.stdio;
s = cast( typeof( s ) )sock;
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

Continue reading on narkive:
Search results for 'std.stdio breaks casting operation' (Questions and Answers)
10
replies
Simple C++ question - PLEASE help.?
started 2007-06-19 22:04:16 UTC
programming & design
Loading...