Search code examples
smalltalkpharo

marked and unmarked class in pharo 2.0 smalltalk


i need to implement the message markedSubclass in pharo that works just like subclass but i need the class that gets created to be somehow marked,for example i tried adding a unique instance variable to it after creating it but it's just not working,maybe i'm adding it to a wrong place. the requirments are:

  1. every subclass of this marked class should also be marked even if it was created via subclass (not markedSubclass).
  2. other than that a marked class should function just as a regular class should. any help would be appreciated.

example:

User markedSubclass: #MarkedUser
User subClass: #UnmarkedUser
MarkedUser subclass: #MarkerUser2

i need to somehow know that MarkedUser and UnmarkedUser are both marked classes. what i thought of lately is adding the method "isMarked" to Class class and this way all the classes will have it, and each class will override it accordingly so if we write

User class isMarked.

it will return false but if we write:

MarkedUser class isMarked.
MarkedUser2 class isMarked.

it will return true for both. but where can i add this method?and how can i make a class override the method in runtime?


Solution

  • Add a class method like the following to your User class:

    markedSubclass: className
        | subclass |
        subclass := self subclass: className asSymbol.
        subclass class compile: 'isMarked', String cr, String tab, ' ^ true'.
        ^ subclass
    

    Then try in a workspace:

    User markedSubclass: 'MyMarkedSubclass'
    

    Add an #unmarkedSubclass: class method accordingly.

    You could then override the general #subclass: method in your User class to set the same marker as the receiver.