www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can't use function with same name as module?

reply Shriramana Sharma writes:
Hello.

fun.d:
import std.stdio;
void fun() { writeln("Hello"); }

main.d:
import fun;
void main() { fun(); }

$ dmd -oftest fun.d main.d
main.d(2): Error: function expected before (), not module fun of 
type void

Why can't I use a function of the same name as the module? IIUC 
import fun imports all the top-level symbols within module fun as 
well as the symbol fun referring to the module itself. This is 
just another case of overloading, no? Can't the compiler 
understand that I am trying to call the function fun.fun() and 
not the module even when it is followed by ()?

Thanks!
Oct 16
next sibling parent reply evilrat <evilrat666 gmail.com> writes:
On Tuesday, 17 October 2017 at 06:13:45 UTC, Shriramana Sharma 
wrote:
 Hello.

 fun.d:
 import std.stdio;
 void fun() { writeln("Hello"); }

 main.d:
 import fun;
 void main() { fun(); }

 $ dmd -oftest fun.d main.d
 main.d(2): Error: function expected before (), not module fun 
 of type void

 Why can't I use a function of the same name as the module? IIUC 
 import fun imports all the top-level symbols within module fun 
 as well as the symbol fun referring to the module itself. This 
 is just another case of overloading, no? Can't the compiler 
 understand that I am trying to call the function fun.fun() and 
 not the module even when it is followed by ()?

 Thanks!
Compiler made that way so it doesn't guess or assume too much, because later on it will bite you when you don't even expect that, and in some unpredictable place too. you can always access symbols with fully qualified name, so in this case void main() { fun.fun(); }
Oct 17
parent reply Shriramana Sharma writes:
On Tuesday, 17 October 2017 at 07:33:15 UTC, evilrat wrote:
 Compiler made that way so it doesn't guess or assume too much, 
 because later on it will bite you when you don't even expect 
 that, and in some unpredictable place too.
Can you give an example for when it will bite me? It seems very natural to name a module with the name of the main class or function implemented in that module. Python modules like datetime follow this. What is the problem? Especially in D the compiler is all-powerful and knows when to resolve to which symbol. If D compiler can resolve between multiple overloads in other cases, why not resolve here?
Oct 17
parent reply Daniel Kozak <kozzi11 gmail.com> writes:
You can:

import fun : fun;

int main(string[] args)
{
    fun();
    return 0;
}

On Tue, Oct 17, 2017 at 10:08 AM, Shriramana Sharma via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 On Tuesday, 17 October 2017 at 07:33:15 UTC, evilrat wrote:

 Compiler made that way so it doesn't guess or assume too much, because
 later on it will bite you when you don't even expect that, and in some
 unpredictable place too.
Can you give an example for when it will bite me? It seems very natural to name a module with the name of the main class or function implemented in that module. Python modules like datetime follow this. What is the problem? Especially in D the compiler is all-powerful and knows when to resolve to which symbol. If D compiler can resolve between multiple overloads in other cases, why not resolve here?
Oct 17
parent reply Shriramana Sharma writes:
On Tuesday, 17 October 2017 at 08:26:12 UTC, Daniel Kozak wrote:
 You can:
 import fun : fun;
Yes I found this but it is unnecessarily verbose. At the same time I also find that it is possible to declare a struct or class with the same name as module: str.d: struct str { int a; } strmain.d: import str; void main() { str var; var.a = 2; } cla.d: class cla { int a; } clamain.d: import cla; void main() { auto var = new cla; var.a = 2; } $ dmd -ofstr str.d strmain.d $ dmd -ofcla cla.d clamain.d Problem only with function. If there can be a class or struct then what's the problem with function? Doesn't look like any of the problems which "will come and bite you later" in this caseā€¦
Oct 17
parent Shriramana Sharma writes:
Have reported https://issues.dlang.org/show_bug.cgi?id=17907
Oct 17
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 10/17/17 2:13 AM, Shriramana Sharma wrote:
 Hello.
 
 fun.d:
 import std.stdio;
 void fun() { writeln("Hello"); }
 
 main.d:
 import fun;
 void main() { fun(); }
 
 $ dmd -oftest fun.d main.d
 main.d(2): Error: function expected before (), not module fun of type void
 
 Why can't I use a function of the same name as the module? IIUC import 
 fun imports all the top-level symbols within module fun as well as the 
 symbol fun referring to the module itself. This is just another case of 
 overloading, no? Can't the compiler understand that I am trying to call 
 the function fun.fun() and not the module even when it is followed by ()?
I know you have filed a report, and probably it should be considered (it surprises me that types work but functions don't, they are all symbols). But just as reference, the reason it doesn't come up much is because most modules are within a package, which means you never have top level modules. But it probably doesn't make a lot of sense to create a package if your library only has one module. It's just not very common. -Steve
Oct 17