From: "Eregon (Benoit Daloze)" Date: 2022-07-31T14:23:03+00:00 Subject: [ruby-core:109394] [Ruby master Feature#18930] Officially deprecate class variables Issue #18930 has been updated by Eregon (Benoit Daloze). Regarding @austin's example, there is Class#subclasses now which might be an easier way to do that. Using class variables for that only works based on a fairly subtle constraint that Base needs to be inherited before any Base subclass can be inherited. Otherwise it would be broken (two subclasses could have separate state). IMHO using a constant there is what makes most sense, it is clear then where the state lives, and it is convenient to access. @Dan0042 Right, but most developers do not expect that. And those "class variables" are just prefixed global variables. With this additional catch (can be a lot less obvious if the cvar is set in a method): ```ruby class A end class B < A @@foo = 2 end class A @@foo = 1 def self.foo @@foo end end p B.foo # => 1, but should be 2 ``` > deprecating them is just removing a tool from the programmer's toolbox Yes, in this case I think this is good because it's a tool which is very difficult to use correctly and can easily be replaced by clearer ways like instance variables, constants, etc. ---------------------------------------- Feature #18930: Officially deprecate class variables https://bugs.ruby-lang.org/issues/18930#change-98545 * Author: Eregon (Benoit Daloze) * Status: Open * Priority: Normal ---------------------------------------- Ruby's class variables are very confusing, and it seem many people agree they should be avoided (#18927). How about we deprecate them officially? Concretely: * Mention in the documentation that class variables are deprecated and should be avoided/should not be used. * Add a parse-time deprecation warning, now that we only see those with `Warning[:deprecation] = true` it seems reasonable to add. -- https://bugs.ruby-lang.org/ Unsubscribe: