Login  Register

Re: Dealing with multiple cases of a string while keeping code short and OOP friendly

Posted by Sean P. DeNigris on Mar 09, 2015; 9:02pm
URL: https://forum.world.st/Dealing-with-multiple-cases-of-a-string-while-keeping-code-short-and-OOP-friendly-tp4810731p4810778.html

kilon.alios wrote
Is this possible ? Is this a smart way ? How would you do it ?
IIUC I would parse just enough to separate the class name from the data, and then let each subclass parse the data. Something like:
    PythonObject fromString: '< Bone [ "side bone" , IK = True , ( 1.0 , 0.1 , 0.2) ]>'.
where:
    PythonObject>>#fromString: aString
        | bracketContents pythonClassName data class |
        bracketContents := aString allButFirst allButLast trimBoth.
        pythonClassName := bracketContents copyUpTo: Character space.
        data := bracketContents copyAfter: Character space.
        class := self subclasses detect: [ :c | c pythonClassName = pythonClassName ].
        ^ class data: data.
Cheers,
Sean