There are numerous issues with using Event as the base for object prototyping / runtime ‘classes’.
I’ve made a PR sclang, classlib: ProtoObject, a better runtime 'class' implementation by JordanHendersonMusic · Pull Request #7219 · supercollider/supercollider · GitHub that proposes a fix.
The class is called ProtoObject right now, but it doesn’t do object prototyping, instead it does single inheritance and allows for the use of doesNotUnderstand. The implementation of ProtoObject, except from the syntax, is done completely in supercollider use the new AbstractObject sclang: introduce AbstractObject by JordanHendersonMusic · Pull Request #7205 · supercollider/supercollider · GitHub, meaning other people may wish to create their own runtime class-like structures, so this doesn’t have to work every single use case. This is why I haven’t actually implemented object-prototyping, because although it is very powerful, most of the time, people just use it to implement classes (see javascript and the class keyword). This means there is no proto or parent which you get with Event.
This is a big proposal so I’ve duplicated the post here to get more eyes on it.
Here is what it looks like to use.
I’d love any thoughts or feedback!
Same as event, slightly different syntax with #(...).
a = #(meow: 1);
a.meow \\ 1
a = #(numChannels: \meow);
a.numChannels \\ \meow --- with event this would have returned 1
a = #(meow: 10, protoObjectLock: true)
a.woof = 10; // Prints error
Create keys at runtime.
a = #(meow: 10)
a.woof = 4;
a.woof // 4
a.meow // 10
Lock object, no new keys once created.
a = #(meow: 10, protoObjectLock: true)
a.meow // 10
a.woof = 10; // Prints error
Inheritance is done by assigning to super.
parent = #(speak: {|self| "I say %".format(self.word) });
base = #(super: parent, word: \meow);
base.speak // I say meow
Can overload doesNotUnderstand
a = #(doesNotUnderstand: "I'm sorry Dave, I'm afraid I can't do that."} );
a.shutdown // "I'm sorry Dave, I'm afraid I can't do that."
Value can be overloaded.
a = #(value: \meow );
a.(); // \meow
Can overload how the object is printed to the post window.
a = #(asString: \meow ); // meow
You can also use this to create exception-like objects.