www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can file name, module name, class name and variable name be the same?

reply WhatMeWorry <kheaser gmail.com> writes:
file 1: camera.d
-----------------------
module camera;
class Camera
{
public:
     // Camera Attributes
     vec3 position = vec3(0.0f, 0.0f, 0.0f);
         . . .
};
-----------------------


file 2: main.d
-----------------------
module main;
import camera;
Camera camera;  // Compile error (1)
-----------------------

(1) variable main.camera conflicts with import main.camera at 
main.d(30)

Renaming "Camera camera;" to "Camera cam;" gets rid of the error
but now I've got tons of new undefined references with camera.

Have I broken some rule by having file name, module name, class 
name and object name
being all cameras?   Is there some technique I can use to get 
Camera camera; to compile?

I can workaround the problem but it seems like a kludge; I'm 
curious about the subtleties of this problems.
Jan 17
parent Brian Schott <briancschott gmail.com> writes:
On Monday, 18 January 2016 at 05:20:42 UTC, WhatMeWorry wrote:
 I can workaround the problem but it seems like a kludge; I'm 
 curious about the subtleties of this problems.
You can't have a variable with the same name as a module because they're both symbols with the same name. It messes up the symbol resolution code. (Also, it confuses any programmer who reads your code) Check the "renamed imports" section at http://dlang.org/spec/module.html. You should be able to say something like `import c = camera;`.
Jan 18