Project

General

Profile

Actions

Feature #21155

open

File scoped namespace declarations as in C#

Added by artur86 (Artur *) 10 days ago. Updated 9 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:121148]

Description

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:

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:


class MyNamespace::MyClass; end
class MyNamespace::MyAnotherClass; end
class MyNamespace::OneMoreClass; end
end

Neither option 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.

The discussion on StackOverflow.

Actions #1

Updated by artur86 (Artur *) 10 days ago

  • Description updated (diff)

Updated by Hanmac (Hans Mackowiak) 10 days ago

@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-Constants

In Short, the second variant can raise NameError if you don't use MyNamespace everywhere.

Actions #3

Updated by artur86 (Artur *) 10 days ago

  • Description updated (diff)
Actions #4

Updated by artur86 (Artur *) 10 days ago

  • Description updated (diff)
Actions #5

Updated by artur86 (Artur *) 10 days ago

  • Description updated (diff)

Updated by artur86 (Artur *) 10 days ago

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-Constants

In Short, the second variant can raise NameError if you don't use MyNamespace everywhere.

I edited the description.

Actions #7

Updated by artur86 (Artur *) 9 days ago

  • Description updated (diff)
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0