Module This::Encompassing::That

Today, I decided I was tired of

module This
module Encompassing
module That
finally_some_really_overly_indented_code
end
end
end

and tried,

module This::Encompassing::That
some_much_less_indented_code
end

since I already do things like,

This::Encompassing::That.module_method

but alas, it didn’t work.

Is there a reason why these two module aspects are not more aligned?
I haven’t looked at the source, but it seems that the module parsing
must already be in place for constructs like This::Encompassing::That,
so what not allow it for defining modules too?

Thanks

···


Bil Kleb
NASA Langley Research Center
Hampton, Virginia, USA

Hi –

Today, I decided I was tired of

module This
module Encompassing
module That
finally_some_really_overly_indented_code
end
end
end

and tried,

module This::Encompassing::That
some_much_less_indented_code
end

since I already do things like,

This::Encompassing::That.module_method

but alas, it didn’t work.

Is there a reason why these two module aspects are not more aligned?
I haven’t looked at the source, but it seems that the module parsing
must already be in place for constructs like This::Encompassing::That,
so what not allow it for defining modules too?

It’s possibly because that notation can mean more than one thing:

module X
module Y
class Z

or

module X
module Y
Z = 100

so there’s no way to determine from the :: notation what you’re
intending to create.

David

···

On Fri, 14 Feb 2003, Bil Kleb wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

dblack@candle.superlink.net wrote:

so there’s no way to determine from the :: notation what you’re
intending to create.

Excellent point.

Thanks,

···


Bil

However, since the idea of the “module” keyword is to introduce a new
module or reuse an existing one, I don’t see why the OP’s idea
couldn’t work.

module X
module Y
module Z
end
C = 100
end
end

module X::Y
def foo; end
end # could work - use existing module

module X::Y::Z
def foo; end
end # could work - use existing module

module X::Y::C
def foo; end
end # error; C is a constant

module X::Y::Z::A
def foo; end
end # could work - creates new module

I like the idea.

Gavin

···

On Friday, February 14, 2003, 2:00:56 PM, Bil wrote:

dblack@candle.superlink.net wrote:

so there’s no way to determine from the :: notation what you’re
intending to create.

Excellent point.

module X::Y

it's in the ToDo

   * class Foo::Bar<Baz .. end, module Boo::Bar .. end
   * def Foo::Bar::baz() .. end ??

Guy Decoux

Hi,

···

At Sat, 15 Feb 2003 23:04:06 +0900, Gavin Sinclair wrote:

However, since the idea of the “module” keyword is to introduce a new
module or reuse an existing one, I don’t see why the OP’s idea
couldn’t work.

module X
module Y
module Z
end
C = 100
end
end

module X::Y
def foo; end
end # could work - use existing module

You can use module_eval.

X::Y.module_eval do
def foo; end
end


Nobu Nakada

Hi –

so there’s no way to determine from the :: notation what you’re
intending to create.

Excellent point.

However, since the idea of the “module” keyword is to introduce a new
module or reuse an existing one, I don’t see why the OP’s idea
couldn’t work.

I assumed he meant a kind of autovivification, so that the first
line in a program could be:

module A::b::C

which would suffer from the ambiguity I mentioned. But if it’s
incremental – i.e., if the above requires that A and B already exist
and be a valid path to a new module definition (namely, C) – that
seems OK.

David

···

On Sat, 15 Feb 2003, Gavin Sinclair wrote:

On Friday, February 14, 2003, 2:00:56 PM, Bil wrote:

dblack@candle.superlink.net wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

I don’t see a problem with autovivification, though. If the first
line in a program is

module A::b::C

what conceptually prevents this from being equivalent to

module A
module B
module C

?

Naturally, if A::B is a (non-Module) constant, then an error would
result.

Gavin

···

On Sunday, February 16, 2003, 4:01:47 AM, dblack wrote:

Hi –

On Sat, 15 Feb 2003, Gavin Sinclair wrote:

On Friday, February 14, 2003, 2:00:56 PM, Bil wrote:

dblack@candle.superlink.net wrote:

so there’s no way to determine from the :: notation what you’re
intending to create.

Excellent point.

However, since the idea of the “module” keyword is to introduce a new
module or reuse an existing one, I don’t see why the OP’s idea
couldn’t work.

I assumed he meant a kind of autovivification, so that the first
line in a program could be:

module A::b::C

which would suffer from the ambiguity I mentioned. But if it’s
incremental – i.e., if the above requires that A and B already exist
and be a valid path to a new module definition (namely, C) – that
seems OK.

Hi,

···

In message “Re: module This::Encompassing::That” on 03/02/16, Gavin Sinclair gsinclair@soyabean.com.au writes:

I don’t see a problem with autovivification, though. If the first
line in a program is

module A::b::C

what conceptually prevents this from being equivalent to

module A
module B
module C

?

Naturally, if A::B is a (non-Module) constant, then an error would
result.

What if A or B were classes?

						matz.

What if A or B were classes?

I don't understand this is valid and same than `module A::b::C'

   class A
      class B
         module C
         end
      end
   end

no ?

Guy Decoux

If it makes sense for classes, then well and good. If not, then
error. I would avoid making it too smart (as later messages discuss),
because then you’d be forever explaining it to everybody.

I like the idea of autovivification within certain rules.

class A
end

module A::B
end # error

The rule is that the Xi in
module X1::X2::…::Xn
must all be either:

  • modules (not classes); or
  • non-existent (so an empty module can be created)

Gavin

···

On Tuesday, February 18, 2003, 3:13:53 AM, Yukihiro wrote:

Hi,

In message “Re: module This::Encompassing::That” > on 03/02/16, Gavin Sinclair gsinclair@soyabean.com.au writes:

I don’t see a problem with autovivification, though. If the first
line in a program is

module A::b::C

what conceptually prevents this from being equivalent to

module A
module B
module C

?

Naturally, if A::B is a (non-Module) constant, then an error would
result.

What if A or B were classes?

Hi,

What if A or B were classes?

I don’t understand this is valid and same than `module A::b::C’

class A
class B
module C
end
end
end

no ?

No. Classes and modules are not always interchangeable.

class A
end

module A
module B
module C
end
end
end

						matz.
···

In message “Re: module This::Encompassing::That” on 03/02/18, ts decoux@moulon.inra.fr writes:

Hi –

What if A or B were classes?

I don’t understand this is valid and same than `module A::b::C’

class A
class B
module C
end
end
end

no ?

Yes, but the question is about autovivification – that is, if this
were the first line in a program:

module A::b::C

then this:

module A
module B
module C

would be created automatically. My point a few messages back was that
this is too magical, since A::b::C does not necessarily refer only to
Modules.

David

···

On Tue, 18 Feb 2003, ts wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi –

I like the idea of autovivification within certain rules.

class A
end

module A::B
end # error

In this case, it’s not autoviv., because A already exists, so it’s an
explicit creation of B.

But still, why would the above be an error? Why not equivalent to:

class A
module B
end
end

The rule is that the Xi in
module X1::X2::…::Xn
must all be either:

  • modules (not classes); or

Why not classes?

  • non-existent (so an empty module can be created)

But it could be an empty Class – meaning, if X2 (or whatever)
doesn’t exist yet, there’s no reason to favor the idea that
it’s supposed to be a Module rather than a Class. There’s just
no way to know.

A case that’s somewhat analogous to this, but without the ambiguity,
is:

mkdir -p a/b/c

which creates all three directories if they don’t exist. In that
case, there’s no ambiguity; there’s nothing else they can be, because
mkdir only does one thing in the first place. But the :: path
notation in module A::b::C does not have that kind of single meaning.
Of course, C has to be a module, and there are things that A and B
can’t be, but there’s more than one thing each of them can be.

David

···

On Tue, 18 Feb 2003, Gavin Sinclair wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

No. Classes and modules are not always interchangeable.

I really don't see where is the problem, sorry, with

   module A::b::C

  class A
  end

in this case this give

   class A
      module B
         module C
         end
      end
   end

  module A
    module B
      module C
      end
    end
  end

it just retrieve C

personnaly my problem is more with

  class A::b::C < D
  end

Guy Decoux

would be created automatically. My point a few messages back was that
this is too magical, since A::b::C does not necessarily refer only to
Modules.

I'm agree with you : it's magical, but if you write `module A::b::C' this
imply fatally that A and B are classes or modules.

Guy Decoux

dblack@candle.superlink.net writes:

Yes, but the question is about autovivification – that is, if this
were the first line in a program:

module A::b::C

then this:

module A
module B
module C

Ruby can raise a runtime error if A and B are unknown.

Same solution can be used to “fix” autoload to allow autoloading of
nested classes/modules.

···


matt

Hi –

I like the idea of autovivification within certain rules.

class A
end

module A::B
end # error

In this case, it’s not autoviv., because A already exists, so it’s an
explicit creation of B.

But still, why would the above be an error? Why not equivalent to:

class A
module B
end
end

Well, you could do that if you like.

The rule is that the Xi in
module X1::X2::…::Xn
must all be either:

  • modules (not classes); or

Why not classes?

I was over-zealously aiming for simplicity.

  • non-existent (so an empty module can be created)

But it could be an empty Class – meaning, if X2 (or whatever)
doesn’t exist yet, there’s no reason to favor the idea that
it’s supposed to be a Module rather than a Class. There’s just
no way to know.

The idea of the “module” keyword is to create modules :slight_smile:

This entire proposal - autovivification or otherwise - is just syntax
sugar. It meets a common need: to define methods within a nested
module (that already exists) or to create a new nested module (i.e.
only one module is vivified).

Given that, it’s probably best to drop the autovivification idea, and
only allow that the entire path is unambiguous, save for the creation
of a new module.

module A
end

module A::b::C
end # error

module A::B
end

[…]

Gavin

···

On Wednesday, February 19, 2003, 12:52:44 AM, dblack wrote:

On Tue, 18 Feb 2003, Gavin Sinclair wrote:

Hi,

No. Classes and modules are not always interchangeable.

I really don’t see where is the problem, sorry, with

module A::b::C

class A
end

in this case this give

class A
module B
module C
end
end
end

I think too “smart” behavior delays class/module inconsistency
detection.

personnaly my problem is more with

class A::b::C < D
end

Do you worry about which namespace D searched in? I guess it must be
top level (not A nor B).

						matz.
···

In message “Re: module This::Encompassing::That” on 03/02/18, ts decoux@moulon.inra.fr writes:

> personnaly my problem is more with
>
> class A::b::C < D
> end

Do you worry about which namespace D searched in? I guess it must be
top level (not A nor B).

Yes, my problem is with

   class D
   end

   module A
      class D
      end
   end

if I'm right

   class A::B < D
   end

it's not the same than

   module A
      class B < D
      end
   end

Guy Decoux