Skip to content

Instantly share code, notes, and snippets.

@dmorgan-github
Last active July 10, 2022 19:50
Show Gist options
  • Save dmorgan-github/02e2652ba227353d729a to your computer and use it in GitHub Desktop.
Save dmorgan-github/02e2652ba227353d729a to your computer and use it in GitHub Desktop.
SuperCollider pseudo-object cheatsheet
(
/*
Collection
|
|-- Set
|
|-- Dictionary
|
|-- IdentityDictionary
|
|-- Environment
|
|-- Event/"PseudoObject"
*/
// Define a pseudo-object
~obj = (
prop1: "prop1",
method1: {arg self; self.prop1.postln;}
);
// get/set a property
// with dot notation
~obj.prop1.postln;
// at notation
~obj[\prop1].postln;
// Execute a method with dot notation.
// The object is passed as first argument
// to method function
~obj.method1(); //> prop1
// NOTE: It is not possible to reference a method
// without executing
~obj.method1; //> prop1
// However, you can reference a method as a function
// without executing it using at notation
~obj[\method1]; //> a Function
// execute method as function
// with at notation
~obj[\method1].value(~obj).value; //> prop1
{
// execute function as method
var func = {arg self; self.prop1.postln;};
func.applyTo(~obj); //> prop1
}.value
/**************************
Prototype Inheritance
**************************/
{
// use proto to set base object
var obj1 = (
proto: ~obj
);
// obj1 inherits ~obj methods and properties
obj1.method1(); //> prop1
obj1.prop1.postln; //> prop1
// override
obj1.prop1 = "overriden prop1";
obj1.prop1.postln; //> overriden prop1
obj1.method1(); //> overriden prop1
// base object remains unchanged
~obj.prop1; //> prop1
// NOTE: it is not possible to override the methods
// defined by or inherited by Event
// Subclass inherits changes to prototype
~obj.prop2 = "prop2";
obj1.prop2.postln;
// property changed in prototype does not change
// in subclass with override
~obj.prop1 = "new value";
obj1.prop1.postln; //> overriden prop1
}.value;
/**************************
Polymorphism
**************************/
{
// Method/function overrides are not supported
// However, you can evaluate the type
// of the method arguments and handle accordingly
var func = {arg parms;
if (parms.isKindOf(Function)) {
parms.value();
};
if (parms.isKindOf(Event)) {
parms.keysValuesDo({arg key, value;
(key ++ "=" ++ value).postln;
});
};
};
func.value ( { "arg is function".postln; } ); //> arg is function
func.value( ("a": "b") ); //> a=b
}.value;
/**************************
Encapsulation
**************************/
// It's not possible to define private members
// on a pseudo-object. However, variable retain
// the scope in which they were created in.
{
var localVariable = "hello";
~obj2 = (
getLocalVariable: {
localVariable.postln;
}
);
}.value;
~obj2.getLocalVariable(); //> hello;
// The following will cause an error
// localVariable.postln; //> Variable 'localVariable' not defined.
/**************************
Reflection
**************************/
~obj.class.postln; //> Event
~obj.isKindOf(Event); //> true
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment