[ruby-core:121148] [Ruby master Feature#21155] File scoped namespace declarations as in C#

Issue #21155 has been reported by artur86 (Artur *). ---------------------------------------- Feature #21155: File scoped namespace declarations as in C# https://bugs.ruby-lang.org/issues/21155 * Author: artur86 (Artur *) * Status: Open ---------------------------------------- Given there is a class that needs to be namespaced. `module` requires indenting the class by one level: ```ruby module MyNamespace class MyClass; end end ``` Scope resolution operator (`::`) does not require it, but it works differently than using `module` and repeating module name for every class inside seems tedious and verbose to me: ```ruby class MyNamespace::MyClass; end class MyNamespace::MyAnotherClass; end class MyNamespace::OneMoreClass; end end ``` What if something similar to [File Scoped Namespaces in C#](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals...) would be implemented in Ruby? The concept basically states as "everything in this file should be in some module", as it is formulated in [a question on StackOverflow](https://stackoverflow.com/q/40809717/2987689). -- https://bugs.ruby-lang.org/

Issue #21155 has been updated by artur86 (Artur *). Given there is a class that needs to be namespaced. `module` requires indenting the class by one level: ```ruby module MyNamespace class MyClass; end end ``` Scope resolution operator (`::`) does not require it, but it works differently than using `module` and repeating module name for every class inside seems tedious and verbose to me: ```ruby class MyNamespace::MyClass; end class MyNamespace::MyAnotherClass; end class MyNamespace::OneMoreClass; end end ``` What if something similar to [File Scoped Namespaces in C#](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals...) would be implemented in Ruby? The concept is "everything in this file should be in some module", as it is formulated in [a question on StackOverflow](https://stackoverflow.com/q/40809717/2987689). ---------------------------------------- Feature #21155: File scoped namespace declarations as in C# https://bugs.ruby-lang.org/issues/21155#change-112078 * Author: artur86 (Artur *) * Status: Open ---------------------------------------- Given there is a class that needs to be namespaced. `module` requires indenting the class by one level: ```ruby module MyNamespace class MyClass; end end ``` Scope resolution operator (`::`) does not require it, but it works differently than using `module` and repeating module name for every class inside seems tedious and verbose to me: ```ruby class MyNamespace::MyClass; end class MyNamespace::MyAnotherClass; end class MyNamespace::OneMoreClass; end end ``` What if something similar to [File Scoped Namespaces in C#](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals...) would be implemented in Ruby? The concept basically states as "everything in this file should be in some module", as it is formulated in [a question on StackOverflow](https://stackoverflow.com/q/40809717/2987689). -- https://bugs.ruby-lang.org/

Issue #21155 has been updated by Hanmac (Hans Mackowiak). @artur86 the difference between these two are the module nesting See this doku about this: https://ruby-doc.org/3.2/syntax/modules_and_classes_rdoc.html#label-Constant... In Short, the second variant can raise NameError if you don't use `MyNamespace` everywhere. ---------------------------------------- Feature #21155: File scoped namespace declarations as in C# https://bugs.ruby-lang.org/issues/21155#change-112082 * Author: artur86 (Artur *) * Status: Open ---------------------------------------- Given there is a class that needs to be namespaced. `module` requires indenting the class by one level: ```ruby module MyNamespace class MyClass; end end ``` Scope resolution operator (`::`) does not require it, but it works differently than using `module` and repeating module name for every class inside seems tedious and verbose to me: ```ruby class MyNamespace::MyClass; end class MyNamespace::MyAnotherClass; end class MyNamespace::OneMoreClass; end end ``` What if something similar to [File Scoped Namespaces in C#](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals...) would be implemented in Ruby? The concept is very clearly formulated in [a question on StackOverflow](https://stackoverflow.com/q/40809717/2987689) and sounds like "everything in this file should be in some module". -- https://bugs.ruby-lang.org/

Issue #21155 has been updated by artur86 (Artur *). Hanmac (Hans Mackowiak) wrote in #note-2:
@artur86 the difference between these two are the module nesting
See this doku about this: https://ruby-doc.org/3.2/syntax/modules_and_classes_rdoc.html#label-Constant...
In Short, the second variant can raise NameError if you don't use `MyNamespace` everywhere.
I edited the description. ---------------------------------------- Feature #21155: File scoped namespace declarations as in C# https://bugs.ruby-lang.org/issues/21155#change-112086 * Author: artur86 (Artur *) * Status: Open ---------------------------------------- Given there is a file with a class that needs to be namespaced. There are basically two options currently. `module` implies indenting the class by one level: ```ruby module MyNamespace class MyClass; end end ``` Scope resolution operator (`::`) needs no indentation, but it works differently than using `module` and repeating module name for every class inside seems tedious and verbose to me: ```ruby class MyNamespace::MyClass; end class MyNamespace::MyAnotherClass; end class MyNamespace::OneMoreClass; end end ``` Neither options enables to declare a namespace once at the top of a file and let Ruby treat all the subsequent constants to be under that namespace. Wouldn't it be better to implement something similar in Ruby? This is implemented in C# under the name of [File Scoped Namespaces](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals...). [The discussion on StackOverflow](https://stackoverflow.com/q/40809717/2987689). -- https://bugs.ruby-lang.org/
participants (2)
-
artur86 (Artur *)
-
Hanmac (Hans Mackowiak)