How to check if one of the superclasses contains a classVariable with a certain name?

StackOverflow https://stackoverflow.com/questions/18422809

  •  26-06-2022
  •  | 
  •  

質問

I want to write a method that checks the list of all the superclasses ( allSuperclass method) and returns 'true' if one of them has a classVariable with a specific name. If none of them have it it'll return false. How do I do this?

役に立ちましたか?

解決

classVarNames is a method that returns a collection of the class variable names.

For example, if you'll add this method to Behavior class you should get a functionality you're asking for:

 superclassesHaveNoClassVar: name
    ^ self allSuperclasses noneSatisfy: [:class |
        class classVarNames includes: name]

Nothing special. Just take allSuperclasses and check if none of them includes a desired variable name among it's classVarNames.

他のヒント

Just send:

superclass allClassVarNames includes: #AClassVariableName

to the class in question. If you want a separate method for this you could place it, as Uko suggested, next to allClassVarNames in the protocol of Behavior:

superclassesIncludeClassVariable: aSymbol
   ^ self superclass allClassVarNames includes: aSymbol

In Squeak, there is following method in Behavior whichClassDefinesClassVar:
You can use it like that:

^(MyClass whichClassDefinesClassVar: #ThisVariableName) notNil
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top