Module name, type name collision


#1

module A
type A

end
end

will give an error that A is being redefined.

I was just curious as to why. I expect it’s because it’s difficult to disambiguate in some situations.

Just curious and I couldn’t find any discussion of it.

It’s a kind of a useful convention, I find myself doing a lot of

module Name

type NameT

end
end


#2

If you used the same name, how would the compiler know when to use which? Both types and modules are first-class objects.

You can use singular for the type, plural for the module. See this guide.


#3

the singular for type and plural for module strategy works for most of the time, but what if the type should also be plural?

such as:

type Points 
    xs::Vector{UInt32}
    ys::Vector{UInt32}
    zs::Vector{UInt32}
end 

#4

Then you have to come up with a different name for the module.


#5

works, but still do not sounds beautiful!


#6

I don’t understand your point. If you have two objects that share a namespace, and you want to be able to distinguish them, they have to have different names — there is no way around this. You may find this useful for choosing good names:


#7

thanks. This might be a Julia design question. python makes the file name as module name, that might be a better design.


#8

It is conventional to do the same in Julia, too; eg PkgDev.generate will set up

module Name
## ...
end

in src/Name.jl.

Nevertheless, I don’t see what this has to do with your original question (module vs type name).


#9

There has been discussion of doing this in Julia too, so that if you do using X, the content of X.jl will automatically be wrapped in a module called X.