Operator[] subscript function?

hi!

Is there something like a C++ - style operator[] subscript function in SC?

I’m writing a Class and would like to access a specific element of a list within the object
through a \key .
Like:

a = myClass;

a[\symbolPairedWithSomething];

now I’m doing:

a = myClass;

a.myList[\symbolPairedWithSomething];

Which works but is annoying to type.

thanks

two ideas.

override doesNotUnderstand or subclass one of the Dictionary classes.

overriding doesNotUderstand may sound like a hack, but if you look around in the standard class library it’s a quite commonly used technique.


MyClass {
var dict;
*new {
^super.new.initMyClass;
}
initMyClass {
dict = IdentityDictionary[
\random -> {1.0.rand.postln},
\postargs -> {|...args| args.postln},
\greet -> {"hello".postln}
];
}
doesNotUnderstand { |selector ... args|
var function;
if((function = [dict.at](http://dict.at)(selector)).notNil, {
^function.value(*args);
}, {
DoesNotUnderstandError(this, selector, args).throw;
});
}
}

/*
a = MyClass.new;
a.random
a.postargs(1, 2, 3)
a.greet
a.ff
*/

while the second suggestion - subclassing - can be done like this…


MyClass2 : IdentityDictionary {
*new {
^super.new.initMyClass2;
}
initMyClass2 {
this.put(\random, {1.0.rand.postln});
this.put(\postargs, {|...args| args.postln});
this.put(\greet, {"hello".postln});
}
}

/*
a = MyClass2.new;
a[\random].value
a[\postargs].value(1, 2, 3)
a[\greet].value
a[\ff]
*/

a variation of the above could be that you just add an .at method and a dictionary to your class.


MyClass3 {
var dict;
*new {
^super.new.initMyClass3;
}
initMyClass3 {
dict = IdentityDictionary[
\random -> {1.0.rand.postln},
\postargs -> {|...args| args.postln},
\greet -> {"hello".postln}
];
}
at {|key ...args|
^dict[key].value(*args);
}
}

/*
a = MyClass3.new;
a[\random]
[a.at](http://a.at)(\postargs, 1, 2, 3)
a[\greet].value
a[\ff]
*/

the at{} will magically make work too.

but it depends on which syntax you prefer… a.something or a[\something]. for the former also read up on addUniqueMethod.
good luck,
_f

10 juli 2021 kl. 21:59 skrev ChrisOrstedt via scsynth <noreply@m.scsynth.org>:

| ChrisOrstedt
July 10 |

  • | - |

hi!

Is there something like a C++ - style operator[] subscript function in SC?

I’m writing a Class and would like to access a specific element of a list within the object
through a \key .
Like:

a = myClass;

a[\symbolPairedWithSomething];

now I’m doing:

a = myClass;

a.myList[\symbolPairedWithSomething];

Which works but is annoying to type.

thanks


Visit Topic or reply to this email to respond. Email replies to this address may be posted publicly and archived on scsynth.org (privacy policy).

You are receiving this because you enabled mailing list mode. To unsubscribe from these emails, click here.

#|
fredrikolofsson.com musicalfieldsforever.com

This expression simply compiles to a method call a.at(\symbolPairedWithSomething).

So (provided that a is an instance of the class) all you need to do is to implement an at method, nothing more.

hjh

oh wow, hah.
Assumed it was the other way around.
Thank you!

One of the distinctive features is that SC has no operators, or even control structures – only methods. So there’s nothing special about operator overloading.

hjh