-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoiding Naming Collisions with Namespaces #13203
Comments
You can just do I don't actually think this is really documented anywhere, so if you would open an issue on https://github.com/crystal-lang/crystal-book, that would be 🙏. Going to close this one given it's already a thing. |
I PRed the doc: crystal-lang/crystal-book#680 |
The top-level scope prefix is actually documented in https://crystal-lang.org/reference/1.7/syntax_and_semantics/the_program.html#top-level-scope |
Bug Report
I'm sorry if it is not a bug, as the variable workaround works in ruby and not in crystal I thought this could be a bug.
Summary
Avoiding Naming Collisions with Namespaces can be solved by setting the built-in class to a variable before defining the custom module class in Ruby, but it doesn't work in Crystal (cf. https://flylib.com/books/en/2.44.1/avoiding_naming_collisions_with_namespaces.html).
Details
Crystal naming collision
test.cr
Ruby naming collision
test.rb
Workaround naming collision in Ruby with variable
test.rb
ruby test.rb
(no error)(Trying to) Workaround naming collision in Crystal with variable
test.cr
Still encounter the same error.
Yes it is possible to rename the class from the custom module
Yes I know it is possible to rename the class in the module (like the code below) to avoid conflicts with the built-in class inside the module scope. But I would rather know how can I explicitly call the built-in
Socket
in amodule
scope while there is a class with the same name. Outside ofTest1
scope there is no conflict withSocket
andTest1::Socket
.Env
The text was updated successfully, but these errors were encountered: