Skip to content

Some thoughts about class variables

Nat! edited this page Nov 22, 2018 · 3 revisions

Class variables are superflous

Use a sharedInstance and store the information in the instance.

Class variables support in mulle-objc via +extraClassSize

You can tack a variable number of bytes to each class using +extraClassSize. You can then use MulleObjCGetClassExtra to get a pointer to that area.

   
Pros very efficient
Cons subclasses do not inherit the +extraClassSize method. No way to call [super extraClassSize]. It is in no way clear that MulleObjCGetClassExtra( [X class]) and MulleObjCGetClassExtra( [x class]) might yield different values, if x is subclass of X.

It's unclear how this is in anyway superior to using a static struct and store the class information there.

Class variables support in mulle-objc via cvars

   
Cons Uses a concurrent hashmap. Who manages the keys ? Never used in in real life.