Pfunc scope [was: Why are local variables preferable to global ones?]

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Pfunc scope [was: Why are local variables preferable to global ones?]

julian.rohrhuber
> E.g. an expression like
>
> Pfunc { ~a ** 2 - 1 }
>
> doesn't refer to a "global" ~a, Streams, which are made from this Pattern,
> will refer to the value of ~a in the Environment, where they are made.
> This is not a mess, but quite practical.


You have to watch out here: also Pfunc uses dynamic scope for environment variables.


(
~a = 2;
x = Pfunc({ ~a ** 2 - 1 });

Environment.use {
        ~a = 1;
        x.asStream.next // returns 0
}
)


(
~a = 2;
x = Pfunc({ ~a ** 2 - 1 }.inEnvir);

Environment.use {
        ~a = 1;
        x.asStream.next // returns 3
}
)




_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
Reply | Threaded
Open this post in threaded view
|

Re: Pfunc scope [was: Why are local variables preferable to global ones?]

daniel-mayer

Am 14.05.2017 um 20:52 schrieb [hidden email]:

>> E.g. an expression like
>>
>> Pfunc { ~a ** 2 - 1 }
>>
>> doesn't refer to a "global" ~a, Streams, which are made from this Pattern,
>> will refer to the value of ~a in the Environment, where they are made.
>> This is not a mess, but quite practical.
>
>
> You have to watch out here: also Pfunc uses dynamic scope for environment variables.


Right, miSCellaneous' tutorial "Event patterns and Functions" describes this also.
The functionality of 'inEnvir' is integrated with PLx patterns,
which have an 'envir' arg to let you choose the environmental context.

Regards

Daniel

----------------------------------------------------
http://daniel-mayer.at/software_en.htm
----------------------------------------------------




_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/