JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.47k stars 5.46k forks source link

Nested type definitions: all names end up global #50040

Open nsajko opened 1 year ago

nsajko commented 1 year ago

It seems that defining types in a nested manner inside other type definitions exposes the inner types as global names, even though no global was used.

Furthermore, the existence of the inner type seems to inhibit the creation of a constructor for the outer type, which seems inconsistent with the fact that the inner type is not restricted to the outer type's scope (or whatever that's called).

Reproducible on master.

julia> versioninfo()
Julia Version 1.8.5
Commit 17cfb8e65ea (2023-01-08 06:45 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × AMD Ryzen 3 5300U with Radeon Graphics
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, znver2)
  Threads: 1 on 8 virtual cores

julia> struct A struct B end end

julia> B
B

julia> methods(A)
# 0 methods for type constructor

julia> struct S0 struct S1 struct S2 struct S3 end end end end
nsajko commented 1 year ago

There's a Discourse thread here: https://discourse.julialang.org/t/weird-behavior-for-types-within-type-definitions/99581

For what it's worth, it seems to me that, if possible, constructions like these should just be disallowed.

brenhinkeller commented 1 year ago

Disallowing might be reasonable if not breaking, otherwise everything ending up global seems maybe (?) ok to me since modules are the only way I imagine types being kept from global scope

topolarity commented 1 month ago

Furthermore, the existence of the inner type seems to inhibit the creation of a constructor for the outer type, which seems inconsistent with the fact that the inner type is not restricted to the outer type's scope (or whatever that's called).

I believe almost any non-field syntax in a struct ... end definition inhibits the default constructor.

I agree that could probably use some tightening up and a proper spelling (@noconstructor?), but it'd also arguably be breaking.