| Newsgroups |
gmane.comp.audio.supercollider.user |
| Message-ID |
<CAK6yb7Y5TOiZMe8rr7sV4K2iiU7sZ3EXtRVa4-vws=gr3Sx=UA@mail.gmail.com> |
You've already got the getters and setters, so why not use inline
conditionals ? and ?? to set defaults?
x ? y will return x if it's not nil, y otherwise.
x ?? {y} will return x if it's not nil, and evaluate y otherwise.
MyClass : MySuperClass{
var <>midinote;
var <>dur = 1;
var <>legato;
*new { |midinote, dur, legato
var myObject = super.new();
myObject.midinote = midinote ?? {#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]};
myObject.dur = dur ? 1;
myObject.legato = legato ?? {#[1, 0.1, 1]};
^ myObject;
}
}
An even better solution would be to call an init() function on myObject,
and set all the defaults there so your constructor and initializer are
decoupled.
On Mon, Mar 22, 2021 at 3:24 PM <[email protected]> wrote:
> Hello,
>
> I´m trying to sort my objects, and would like to do that using Classes,
> but can’t get one thing right:
>
> This way I can’t initialize variables when creating the object:
>
> MyClass : MySuperClass{
> var <>midinote = #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
> var <>dur = 1;
> var <>legato=#[1, 0.1, 1];
> }
> }
>
> This way it works, but then I have to initialize ALL variables, otherwise
> they are nil
>
> MyClass : MySuperClass{
> var <>midinote = #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
> var <>dur = 1;
> var <>legato=#[1, 0.1, 1];
>
> *new { |midinote, dur, legato
> ^super.newCopyArgs(midinote,dur, legato)
> }
> }
>
>
> Any hints how to make it work?
>
> Thanks and all the best,
>
> Maciej
>
> www.gamutinc.org
>