Chris Muller uploaded a new version of Environments to project The Inbox:
http://source.squeak.org/inbox/Environments-cmm.57.mcz ==================== Summary ==================== Name: Environments-cmm.57 Author: cmm Time: 23 March 2015, 1:58:37.126 pm UUID: 6e10d99c-b424-4f94-9152-18ccd72ca623 Ancestors: Environments-topa.56 Fix access to globals which were defined by: Smalltalk at: #MyGlobal ifAbsentPut: [myValue]. =============== Diff against Environments-topa.56 =============== Item was changed: ----- Method: Environment>>at:ifAbsentPut: (in category 'emulating') ----- + at: aSymbol ifAbsentPut: aBlock - at: aSymbol ifAbsentPut: aBlock ^ declarations at: aSymbol + ifAbsentPut: + [ bindings + at: aSymbol + ifAbsentPut: aBlock ]! - ifAbsentPut: aBlock! |
To define a global variable in code, while taking care not to
over-write it if it already is defined, I wrote: Smalltalk at: #MyGlobal ifAbsentPut: [myValue]. But this only put it in the Smalltalk environments 'declarations', not its 'bindings', which renders MyGlobal inaccessible from any code. I don't really like this nesting of at:ifAbsentPut: into 'bindings', but it does solve the bug for the basic case. Is it the right way to fix it? On Mon, Mar 23, 2015 at 1:58 PM, <[hidden email]> wrote: > Chris Muller uploaded a new version of Environments to project The Inbox: > http://source.squeak.org/inbox/Environments-cmm.57.mcz > > ==================== Summary ==================== > > Name: Environments-cmm.57 > Author: cmm > Time: 23 March 2015, 1:58:37.126 pm > UUID: 6e10d99c-b424-4f94-9152-18ccd72ca623 > Ancestors: Environments-topa.56 > > Fix access to globals which were defined by: Smalltalk at: #MyGlobal ifAbsentPut: [myValue]. > > =============== Diff against Environments-topa.56 =============== > > Item was changed: > ----- Method: Environment>>at:ifAbsentPut: (in category 'emulating') ----- > + at: aSymbol ifAbsentPut: aBlock > - at: aSymbol ifAbsentPut: aBlock > ^ declarations > at: aSymbol > + ifAbsentPut: > + [ bindings > + at: aSymbol > + ifAbsentPut: aBlock ]! > - ifAbsentPut: aBlock! > > |
On Mon, 23 Mar 2015, Chris Muller wrote:
> To define a global variable in code, while taking care not to > over-write it if it already is defined, I wrote: > > Smalltalk at: #MyGlobal ifAbsentPut: [myValue]. > > But this only put it in the Smalltalk environments 'declarations', not > its 'bindings', which renders MyGlobal inaccessible from any code. > > I don't really like this nesting of at:ifAbsentPut: into 'bindings', > but it does solve the bug for the basic case. Is it the right way to > fix it? No, it's a hack. The right way to do it is to use #bind:to: to create a new binding. Something like this should work: at: aSymbol ifAbsentPut: aBlock ^self at: aSymbol ifAbsent: [ self at: aSymbol put: aBlock value ] Levente > > On Mon, Mar 23, 2015 at 1:58 PM, <[hidden email]> wrote: >> Chris Muller uploaded a new version of Environments to project The Inbox: >> http://source.squeak.org/inbox/Environments-cmm.57.mcz >> >> ==================== Summary ==================== >> >> Name: Environments-cmm.57 >> Author: cmm >> Time: 23 March 2015, 1:58:37.126 pm >> UUID: 6e10d99c-b424-4f94-9152-18ccd72ca623 >> Ancestors: Environments-topa.56 >> >> Fix access to globals which were defined by: Smalltalk at: #MyGlobal ifAbsentPut: [myValue]. >> >> =============== Diff against Environments-topa.56 =============== >> >> Item was changed: >> ----- Method: Environment>>at:ifAbsentPut: (in category 'emulating') ----- >> + at: aSymbol ifAbsentPut: aBlock >> - at: aSymbol ifAbsentPut: aBlock >> ^ declarations >> at: aSymbol >> + ifAbsentPut: >> + [ bindings >> + at: aSymbol >> + ifAbsentPut: aBlock ]! >> - ifAbsentPut: aBlock! >> >> > > |
My mental model of "Smalltalk" is that there is all global stuff stored: classes and other global variables such as ActiveWorld and ActiveHand. As Smalltalk >> #at: returns the object behind that global name and Smalltalk >> #at:put: overwrites such a global binding with a new object, this looks like a regular dictionary to me. Having this, #at:ifAbsentPut: should just combine #at: and #at:put: -- like Levente proposed.
What is the current state of our discussion about Smalltalk offering the full Dictionary interface? Best, Marcel |
In reply to this post by Levente Uzonyi-2
> On 24.03.2015, at 04:22, Levente Uzonyi <[hidden email]> wrote: > > On Mon, 23 Mar 2015, Chris Muller wrote: > >> To define a global variable in code, while taking care not to >> over-write it if it already is defined, I wrote: >> >> Smalltalk at: #MyGlobal ifAbsentPut: [myValue]. >> >> But this only put it in the Smalltalk environments 'declarations', not >> its 'bindings', which renders MyGlobal inaccessible from any code. >> >> I don't really like this nesting of at:ifAbsentPut: into 'bindings', >> but it does solve the bug for the basic case. Is it the right way to >> fix it? > > No, it's a hack. The right way to do it is to use #bind:to: to create a new binding. Something like this should work: > > at: aSymbol ifAbsentPut: aBlock > > ^self at: aSymbol ifAbsent: [ > self at: aSymbol put: aBlock value ] > > Levente I just ran into this too ... - Bert - smime.p7s (5K) Download Attachment |
In reply to this post by Levente Uzonyi-2
Oh that is so much better; use the existing API's which we know work
rather than another path through the code which modifies the state of the object. I just tried it and it worked, thanks Levente! I'll commit it to trunk. On Mon, Mar 23, 2015 at 10:22 PM, Levente Uzonyi <[hidden email]> wrote: > On Mon, 23 Mar 2015, Chris Muller wrote: > >> To define a global variable in code, while taking care not to >> over-write it if it already is defined, I wrote: >> >> Smalltalk at: #MyGlobal ifAbsentPut: [myValue]. >> >> But this only put it in the Smalltalk environments 'declarations', not >> its 'bindings', which renders MyGlobal inaccessible from any code. >> >> I don't really like this nesting of at:ifAbsentPut: into 'bindings', >> but it does solve the bug for the basic case. Is it the right way to >> fix it? > > > No, it's a hack. The right way to do it is to use #bind:to: to create a new > binding. Something like this should work: > > at: aSymbol ifAbsentPut: aBlock > > ^self at: aSymbol ifAbsent: [ > self at: aSymbol put: aBlock value ] > > Levente > >> >> On Mon, Mar 23, 2015 at 1:58 PM, <[hidden email]> wrote: >>> >>> Chris Muller uploaded a new version of Environments to project The Inbox: >>> http://source.squeak.org/inbox/Environments-cmm.57.mcz >>> >>> ==================== Summary ==================== >>> >>> Name: Environments-cmm.57 >>> Author: cmm >>> Time: 23 March 2015, 1:58:37.126 pm >>> UUID: 6e10d99c-b424-4f94-9152-18ccd72ca623 >>> Ancestors: Environments-topa.56 >>> >>> Fix access to globals which were defined by: Smalltalk at: #MyGlobal >>> ifAbsentPut: [myValue]. >>> >>> =============== Diff against Environments-topa.56 =============== >>> >>> Item was changed: >>> ----- Method: Environment>>at:ifAbsentPut: (in category 'emulating') >>> ----- >>> + at: aSymbol ifAbsentPut: aBlock >>> - at: aSymbol ifAbsentPut: aBlock >>> ^ declarations >>> at: aSymbol >>> + ifAbsentPut: >>> + [ bindings >>> + at: aSymbol >>> + ifAbsentPut: aBlock ]! >>> - ifAbsentPut: aBlock! >>> >>> >> >> > |
Free forum by Nabble | Edit this page |