www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Are aliases supossed to be implicitly cross-module overloadable?

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Here's a few modules:

main.d:
import first;       // call(double)
import second;  // aliased: third.call(string) call

void main()
{ 
    call(5.5);
}


first.d:
void call(double) { }


second.d:
import third;
alias third.call call;


third.d
void call(string) { }


There's a problem here, the function "call" from module first.d is not
overloaded against the functions "call" from second.d & third.d when modules
first.d and second.d are imported to a separate module.

So it appears I can't implicitly overload against two modules, where one module
has an alias to a function of another module. The way to work around this would
be to add more aliases, this time to both modules in main.d:

import first;   // call(double)
import second;  // aliased: third.call(string) call

alias first.call call;
alias second.call call;

void main()
{ 
    call(5.5);
}

Is this how it's supossed to be? Btw, this is just a simplified version of the
TDPL example, but it was a little vague on whether or not main.d needs those
aliases in addition to the alias in second.d.
Aug 21 2010
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
I understand the situtation when functions from two modules compete
over a call and both are eligible for the call. In that case it's an
error (since the compiler protects against function hijacking). But in
this case, only the function from the first module is eligible for the
call.

Let me do a simple comparison to make this easier to see:

module1.d:
void foo(double)
{
}

module2.d:
void foo(string)
{
}

module3.d:
void foo(int[])
{
}

main.d:
import module1, module2, module3;

void main()
{
    foo(5.5);
}

This works fine. The functions cannot hijack each other.

Now I'll remove the import to module3 in main.d, and instead import
module3 from module2 and use an alias. The files are now:

module1.d:
void foo(double)
{
}

module2.d:
import module3;
alias module3.foo foo;

void foo(string)
{
}

module3.d:
void foo(int[])
{
}

main.d:
import module1, module2;

void main()
{
    foo(5.5);
}

But now I have a conflict:
main.d(5): Error: module1.foo at module1.d(1) conflicts with
module2.foo at module2.d(2)

Even though the functions still can't hijack each other, adding an
alias in a module seems to break compilation.

And the way to fix this, is that I need to explicitly add aliases to
the functions in main.d:

import module1, module2;

alias module1.foo foo;
alias module2.foo foo;

void main()
{
    foo(5.5);
}

So, are the aliases really needed here? Shouldn't the compiler be able
to figure out that the functions from module1.d and module2.d never
hijack each other if there's an alias in one of the modules?

On Sat, Aug 21, 2010 at 6:59 PM, Andrej Mitrovic
<andrej.mitrovich gmail.com> wrote:
 snip
Aug 22 2010
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Interestingly, a public import *does* work:

module1.d:
void foo(double)
{
}

module2.d:
public import module3;

void foo(string)
{
}

module3.d:
void foo(int[])
{
}

main.d:
import module1, module2;

void main()
{
    foo(5.5);
}

Compiles fine and runs fine. And according to TDPL, a public import is
a shorthand for an alias for every symbol found in the imported
module. So my guess is there's some kind of bug with aliasing.

On Sun, Aug 22, 2010 at 6:48 PM, Andrej Mitrovic
<andrej.mitrovich gmail.com> wrote:
 I understand the situtation when functions from two modules compete
 over a call and both are eligible for the call. In that case it's an
 error (since the compiler protects against function hijacking). But in
 this case, only the function from the first module is eligible for the
 call.

 Let me do a simple comparison to make this easier to see:

 module1.d:
 void foo(double)
 {
 }

 module2.d:
 void foo(string)
 {
 }

 module3.d:
 void foo(int[])
 {
 }

 main.d:
 import module1, module2, module3;

 void main()
 {
 =A0 =A0foo(5.5);
 }

 This works fine. The functions cannot hijack each other.

 Now I'll remove the import to module3 in main.d, and instead import
 module3 from module2 and use an alias. The files are now:

 module1.d:
 void foo(double)
 {
 }

 module2.d:
 import module3;
 alias module3.foo foo;

 void foo(string)
 {
 }

 module3.d:
 void foo(int[])
 {
 }

 main.d:
 import module1, module2;

 void main()
 {
 =A0 =A0foo(5.5);
 }

 But now I have a conflict:
 main.d(5): Error: module1.foo at module1.d(1) conflicts with
 module2.foo at module2.d(2)

 Even though the functions still can't hijack each other, adding an
 alias in a module seems to break compilation.

 And the way to fix this, is that I need to explicitly add aliases to
 the functions in main.d:

 import module1, module2;

 alias module1.foo foo;
 alias module2.foo foo;

 void main()
 {
 =A0 =A0foo(5.5);
 }

 So, are the aliases really needed here? Shouldn't the compiler be able
 to figure out that the functions from module1.d and module2.d never
 hijack each other if there's an alias in one of the modules?

 On Sat, Aug 21, 2010 at 6:59 PM, Andrej Mitrovic
 <andrej.mitrovich gmail.com> wrote:
 snip
Aug 22 2010