View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide

no way to create new classes at run-time

Home   How To   Code Pool   Public Library   Theory   Events

Is it possible to create new classes at run time in SC3?

The short answer is "No".
James McCartney explains that
"SC's constant time method lookup tables would complicate doing this. It could be done but it would be expensive."
So this feature has been excluded from the language for performance reasons.
Julian Rohrhuber suggested 2 alternatives. The first is using a dictionary, like this:
d =[
        \post -> { arg item; }
The other one is the used of the addUniqueMethod method
9.addUniqueMethod(\ohNo, { \hello });
Of course to really add new classes you need to restart the interpreter so that it can recompile the library.

Object prototyping using Environments or Events is becoming more common. Basic notes may be found in the latest svn version of the Environment help file.

e = (a: 1, b: 2, addAB: { |self| self.a + self.b });
e.addAB;  // prints 1+2 == 3
e.a = 5;
e.addAB;  // prints 5+2 == 7

Also see the ddwPrototype quark for a slightly different approach to the same problem.

Links to this Page