On Monday, 26 February 2024 at 22:40:49 UTC, Liam McGillivray wrote:
On Sunday, 25 February 2024 at 03:23:03 UTC, Paul Backus wrote:
You can't give a class the same name as the file it's in. If you do, then when you try to use it from another file, the compiler will get confused and think you're referring to the file instead of the class (that's what "import is used as a type" means).

Thank you. In PHP, I was told to put every class definition in a file of the same name (whether I like it or not).

However, I actually now have it working *without* having done that. Both the file name and the class name are capitalized, and it's working. However, maybe that's because they each start with a `module` line that makes the module name lowercase. I will keep this in mind, and maybe rename the files.

So D is weird about this. I always recommend you use a *package* (i.e. module foo.bar) instead of just a module (i.e. module bar).

When you omit the module declaration, the compiler assumes the module name is the same as the file name *without the path taken into account*.

What happens is if you have `Map` as a module, and then `Map` as the class name, using the name `Map` is going to be confusing (did you mean the module or the class?)

However, if you have everything under a package, for example `foo`, i.e. a file `foo/Map.d` which contains the `Map` class, then when referring to `Map`, it can't be referring to the module, since you would have to refer to `foo.Map` for that. This means the class name `Map` by itself is unambiguous.

A whole host of problems occurs with name lookup when you don't use packages.

-Steve

Reply via email to