crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.45k stars 1.62k forks source link

Uppercase instance or class variables #14093

Open HertzDevil opened 11 months ago

HertzDevil commented 11 months ago

It is in fact possible to define instance or class variables whose name starts with an uppercase letter:

class Foo
  @A = 1
  @@B = 2

  def foo
    @A + @@B
  end
end

Foo.new.foo # => 3

The same holds for Ruby as well. However, this is causing codegen problems for us, such as in #7865.

Should we deprecate this syntax or do we accept it as valid?

straight-shoota commented 11 months ago

So the codegen issue is that class vars and constants of the same name would result in the same init function? @@A and A both use a function called something like Namespace:A:init as initializer. I suppose we could prevent this collision by introducing disambiguation (e.g. Namespace:const_A:init, Namespace:var_A:init).

But I don't see much value in allowing uppercase class or instance variables. I think it's pretty unexpected that this is even possible. Probably nobody will miss this, so I'd be happy to remove this syntax. To be sure on this, we can start deprecating it.