Class as Namespace

Hi,
I am wondering wether there is any difference between these 2 snippets:

module Foo
  class Bar
  end

  class Bar::Xyz
  end
end

and

module Foo
  class Bar
    class Xyz
    end
  end
end

In this case there is not, Bar is treated as a class in both examples. However, in the first example, if you did not have the explicit class Bar, then it would be treated as a module.

2 Likes

Ok.
I asked this question because it seems to me that nesting classes can sometimes make it more difficult to see the code as a whole (so I favor first case).
I assume that the code generated is the same in both cases.

The second example can be somewhat of a problem as per Inference of type from namespace · Issue #8685 · crystal-lang/crystal · GitHub as well. Are a few places where I have to define a class near the top of my requires so that the class Foo::Bar does not treat Foo as a module. But yes, this is more of an organizational thing and unlikely to have any affect on the underlying code.

1 Like

Fine.
Thanks for the link.

Couldn’t the compiler be smarter about this? Sorta leave it up in the air what Foo is until it’s defined? Fall back to module if it’s not explicitly stated at the end of the day, as I assume it has to defined for… Other stuff to work.

That’s literally what the issue referenced in the sentence right before the one that you quoted is about.

cough Sorry. I don’t always remember to check the links in my eagerness.