List of all global variables? (Environments?)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

List of all global variables? (Environments?)

Hannes Hirzel
Hello

The recipe: How to list all global variables [1]

has the code snippet



    Smalltalk keys
    select:
    [:k | ((Smalltalk at: k) isKindOf: Class) not]
       thenCollect:
      [:k | k -> (Smalltalk at: k) class]


to give a list of global variables.


When executing this code the recommendation is to use

    Smalltalk globals

instead of

    Smalltalk keys


A  rewrite because of Environments not having #select:thenCollect:  (I
do not think that is necessary) then is

    (Smalltalk globals select: [:k | ((Smalltalk at: k) isKindOf: Class) not])
       collect:  [:k | k -> (Smalltalk at: k) class]

Then I get 'key not found: ExtendedNumberParser'.

I wonder what is going on here ...

Regards
Hannes


[1] http://wiki.squeak.org/squeak/1824

Reply | Threaded
Open this post in threaded view
|

Re: List of all global variables? (Environments?)

Hannes Hirzel
P.S. A more elegant solution is mentioned at the bottom of page
http://wiki.squeak.org/squeak/1824

Smalltalk globals reject: [:each | each class isMeta]

Result attached.



For that to work Environment needs the addition of

reject: aBlock
        ^ declarations reject: aBlock

in the 'emulating' instance method protocol

On 4/3/17, H. Hirzel <[hidden email]> wrote:

> Hello
>
> The recipe: How to list all global variables [1]
>
> has the code snippet
>
>
>
>     Smalltalk keys
>     select:
>     [:k | ((Smalltalk at: k) isKindOf: Class) not]
>        thenCollect:
>       [:k | k -> (Smalltalk at: k) class]
>
>
> to give a list of global variables.
>
>
> When executing this code the recommendation is to use
>
>     Smalltalk globals
>
> instead of
>
>     Smalltalk keys
>
>
> A  rewrite because of Environments not having #select:thenCollect:  (I
> do not think that is necessary) then is
>
>     (Smalltalk globals select: [:k | ((Smalltalk at: k) isKindOf: Class)
> not])
>        collect:  [:k | k -> (Smalltalk at: k) class]
>
> Then I get 'key not found: ExtendedNumberParser'.
>
> I wonder what is going on here ...
>
> Regards
> Hannes
>
>
> [1] http://wiki.squeak.org/squeak/1824
>



Global_variables_in_Squeak_Smalltalk_2017-04-03.png (56K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: List of all global variables? (Environments?)

Jakob Reschke
In reply to this post by Hannes Hirzel

Am 03.04.2017 19:12 schrieb "H. Hirzel" <[hidden email]>:


    (Smalltalk globals select: [:k | ((Smalltalk at: k) isKindOf: Class) not])
       collect:  [:k | k -> (Smalltalk at: k) class]

Then I get 'key not found: ExtendedNumberParser'.


Cannot verify this atm but I think k is the class (value), not the symbol (key), and at: wants a key. Like a Dictionary.


Reply | Threaded
Open this post in threaded view
|

Re: List of all global variables? (Environments?)

Hannes Hirzel
In reply to this post by Hannes Hirzel
Found the solution. It is in the class comment of SystemDictionary


_____
I represent a special dictionary used as global namespace for class names :

        Smalltalk globals classNames.

and for traits too:

        Smalltalk globals traitNames.
       
and a few other globals:

        (Smalltalk globals keys reject: [:k | (Smalltalk globals at: k) isBehavior])
                        collect: [:k | k -> (Smalltalk globals at: k) class].

As the above example let you guess, the global namespace of Smalltalk
system is accessed through:

        Smalltalk globals.

____
I updated http://wiki.squeak.org/squeak/1824
__
However there is a problem with

    (Smalltalk globals reject: [:each | each class isMeta]) inspect


You get the list but if you click on 'self' then you get a different
list. (printString issue)




On 4/3/17, H. Hirzel <[hidden email]> wrote:

> P.S. A more elegant solution is mentioned at the bottom of page
> http://wiki.squeak.org/squeak/1824
>
> Smalltalk globals reject: [:each | each class isMeta]
>
> Result attached.
>
>
>
> For that to work Environment needs the addition of
>
> reject: aBlock
> ^ declarations reject: aBlock
>
> in the 'emulating' instance method protocol
>
> On 4/3/17, H. Hirzel <[hidden email]> wrote:
>> Hello
>>
>> The recipe: How to list all global variables [1]
>>
>> has the code snippet
>>
>>
>>
>>     Smalltalk keys
>>     select:
>>     [:k | ((Smalltalk at: k) isKindOf: Class) not]
>>        thenCollect:
>>       [:k | k -> (Smalltalk at: k) class]
>>
>>
>> to give a list of global variables.
>>
>>
>> When executing this code the recommendation is to use
>>
>>     Smalltalk globals
>>
>> instead of
>>
>>     Smalltalk keys
>>
>>
>> A  rewrite because of Environments not having #select:thenCollect:  (I
>> do not think that is necessary) then is
>>
>>     (Smalltalk globals select: [:k | ((Smalltalk at: k) isKindOf: Class)
>> not])
>>        collect:  [:k | k -> (Smalltalk at: k) class]
>>
>> Then I get 'key not found: ExtendedNumberParser'.
>>
>> I wonder what is going on here ...
>>
>> Regards
>> Hannes
>>
>>
>> [1] http://wiki.squeak.org/squeak/1824
>>
>

Reply | Threaded
Open this post in threaded view
|

Re: List of all global variables? (Environments?)

Levente Uzonyi
In reply to this post by Hannes Hirzel
Both solutions are wrong.
The one you found on the wiki will be fooled by any global pointing to a
class.
The one you came up with will also lose the bindings themselves.
The correct solution is:

Array streamContents: [ :stream |
  Smalltalk globals associationsDo: [ :binding |
  binding class == Global ifTrue: [ stream nextPut: binding ] ] ].

The role of declarations and bindings is still not clear in Environments,
therefore, as I wrote it before, unrelated to tool support, I still
consider Environments to be incomplete.

Levente

On Mon, 3 Apr 2017, H. Hirzel wrote:

> P.S. A more elegant solution is mentioned at the bottom of page
> http://wiki.squeak.org/squeak/1824
>
> Smalltalk globals reject: [:each | each class isMeta]
>
> Result attached.
>
>
>
> For that to work Environment needs the addition of
>
> reject: aBlock
> ^ declarations reject: aBlock
>
> in the 'emulating' instance method protocol
>
> On 4/3/17, H. Hirzel <[hidden email]> wrote:
>> Hello
>>
>> The recipe: How to list all global variables [1]
>>
>> has the code snippet
>>
>>
>>
>>     Smalltalk keys
>>     select:
>>     [:k | ((Smalltalk at: k) isKindOf: Class) not]
>>        thenCollect:
>>       [:k | k -> (Smalltalk at: k) class]
>>
>>
>> to give a list of global variables.
>>
>>
>> When executing this code the recommendation is to use
>>
>>     Smalltalk globals
>>
>> instead of
>>
>>     Smalltalk keys
>>
>>
>> A  rewrite because of Environments not having #select:thenCollect:  (I
>> do not think that is necessary) then is
>>
>>     (Smalltalk globals select: [:k | ((Smalltalk at: k) isKindOf: Class)
>> not])
>>        collect:  [:k | k -> (Smalltalk at: k) class]
>>
>> Then I get 'key not found: ExtendedNumberParser'.
>>
>> I wonder what is going on here ...
>>
>> Regards
>> Hannes
>>
>>
>> [1] http://wiki.squeak.org/squeak/1824
>>
>

Reply | Threaded
Open this post in threaded view
|

Re: List of all global variables? (Environments?)

Eliot Miranda-2
Hi Levente,

> On Apr 3, 2017, at 11:14 AM, Levente Uzonyi <[hidden email]> wrote:
>
> Both solutions are wrong.

+1

> The one you found on the wiki will be fooled by any global pointing to a class.
> The one you came up with will also lose the bindings themselves.
> The correct solution is:
>
> Array streamContents: [ :stream |
>    Smalltalk globals associationsDo: [ :binding |
>        binding class == Global ifTrue: [ stream nextPut: binding ] ] ].

Why not

Array streamContents: [ :stream |
   Smalltalk globals associationsDo: [ :binding |
       (binding class == Global
       and: [ binding value isBehavior
       and: [ binding key == binding value name ] ]) ifTrue:
            [ stream nextPut: binding ] ] ].

or

Array streamContents: [ :stream |
   Smalltalk globals associationsDo: [ :binding |
       (binding value isBehavior
       and: [ binding key == binding value name ]) ifTrue:
            [ stream nextPut: binding ] ] ].

?

> The role of declarations and bindings is still not clear in Environments, therefore, as I wrote it before, unrelated to tool support, I still consider Environments to be incomplete.

What is required to resolve this?

>
> Levente
>
>> On Mon, 3 Apr 2017, H. Hirzel wrote:
>>
>> P.S. A more elegant solution is mentioned at the bottom of page
>> http://wiki.squeak.org/squeak/1824
>>
>> Smalltalk globals reject: [:each | each class isMeta]
>>
>> Result attached.
>>
>>
>>
>> For that to work Environment needs the addition of
>>
>> reject: aBlock
>>    ^ declarations reject: aBlock
>>
>> in the 'emulating' instance method protocol
>>
>>> On 4/3/17, H. Hirzel <[hidden email]> wrote:
>>> Hello
>>>
>>> The recipe: How to list all global variables [1]
>>>
>>> has the code snippet
>>>
>>>
>>>
>>>    Smalltalk keys
>>>    select:
>>>    [:k | ((Smalltalk at: k) isKindOf: Class) not]
>>>       thenCollect:
>>>      [:k | k -> (Smalltalk at: k) class]
>>>
>>>
>>> to give a list of global variables.
>>>
>>>
>>> When executing this code the recommendation is to use
>>>
>>>    Smalltalk globals
>>>
>>> instead of
>>>
>>>    Smalltalk keys
>>>
>>>
>>> A  rewrite because of Environments not having #select:thenCollect:  (I
>>> do not think that is necessary) then is
>>>
>>>    (Smalltalk globals select: [:k | ((Smalltalk at: k) isKindOf: Class)
>>> not])
>>>       collect:  [:k | k -> (Smalltalk at: k) class]
>>>
>>> Then I get 'key not found: ExtendedNumberParser'.
>>>
>>> I wonder what is going on here ...
>>>
>>> Regards
>>> Hannes
>>>
>>>
>>> [1] http://wiki.squeak.org/squeak/1824
>>>
>>
>

Reply | Threaded
Open this post in threaded view
|

Re: List of all global variables? (Environments?)

Bert Freudenberg
On Tue, Apr 4, 2017 at 3:47 PM, Eliot Miranda <[hidden email]> wrote:
Hi Levente,

> On Apr 3, 2017, at 11:14 AM, Levente Uzonyi <[hidden email]> wrote:
>
>  The correct solution is:
>
> Array streamContents: [ :stream |
>    Smalltalk globals associationsDo: [ :binding |
>        binding class == Global ifTrue: [ stream nextPut: binding ] ] ].

Or shorter 

    Smalltalk globals declarations select: #canAssign

maybe? Unlike class bindings, global vars are writable.
 
Why not

Array streamContents: [ :stream |
   Smalltalk globals associationsDo: [ :binding |
       (binding class == Global
       and: [ binding value isBehavior
       and: [ binding key == binding value name ] ]) ifTrue:
            [ stream nextPut: binding ] ] ].

Because we want to know the global *variables*, that is everything *but* the class bindings. So we look for instances of Global, not ClassBinding, no matter the value.

> The role of declarations and bindings is still not clear in Environments, therefore, as I wrote it before, unrelated to tool support, I still consider Environments to be incomplete.

I thought 'declarations' are the things owned by this environment (excluding imports), whereas 'bindings' are the things visible in this environment (including imports).

- Bert -
 


Reply | Threaded
Open this post in threaded view
|

Re: List of all global variables? (Environments?)

Eliot Miranda-2


On Apr 4, 2017, at 7:02 AM, Bert Freudenberg <[hidden email]> wrote:

On Tue, Apr 4, 2017 at 3:47 PM, Eliot Miranda <[hidden email]> wrote:
Hi Levente,

> On Apr 3, 2017, at 11:14 AM, Levente Uzonyi <[hidden email]> wrote:
>
>  The correct solution is:
>
> Array streamContents: [ :stream |
>    Smalltalk globals associationsDo: [ :binding |
>        binding class == Global ifTrue: [ stream nextPut: binding ] ] ].

Or shorter 

    Smalltalk globals declarations select: #canAssign

maybe? Unlike class bindings, global vars are writable.

I don't like this.  I see writability as orthogonal.  I'm sure there's good uses for read-only globals that are not classes.  I like the tenseness though.  So

    Smalltalk globals declarations reject: #isClassBinding

with
isClassBinding ^value isBehavior and: [key == value name]

 
Why not

Array streamContents: [ :stream |
   Smalltalk globals associationsDo: [ :binding |
       (binding class == Global
       and: [ binding value isBehavior
       and: [ binding key == binding value name ] ]) ifTrue:
            [ stream nextPut: binding ] ] ].

Because we want to know the global *variables*, that is everything *but* the class bindings. So we look for instances of Global, not ClassBinding, no matter the value.

Forgive me; I missed a not in there.  But my point is that the classes in global are in bindings whose keys are == to their name.  Everything else is a global variable.


> The role of declarations and bindings is still not clear in Environments, therefore, as I wrote it before, unrelated to tool support, I still consider Environments to be incomplete.

I thought 'declarations' are the things owned by this environment (excluding imports), whereas 'bindings' are the things visible in this environment (including imports).

Is there a definition in text somewhere?  Colin?

- Bert -


Reply | Threaded
Open this post in threaded view
|

Re: List of all global variables? (Environments?)

Levente Uzonyi
In reply to this post by Bert Freudenberg
On Tue, 4 Apr 2017, Bert Freudenberg wrote:

> On Tue, Apr 4, 2017 at 3:47 PM, Eliot Miranda <[hidden email]> wrote:
>       Hi Levente,
>
>       > On Apr 3, 2017, at 11:14 AM, Levente Uzonyi <[hidden email]> wrote:
>       >
>       >  The correct solution is:
>       >
>       > Array streamContents: [ :stream |
>       >    Smalltalk globals associationsDo: [ :binding |
>       >        binding class == Global ifTrue: [ stream nextPut: binding ] ] ].
>
>
> Or shorter 
>
>     Smalltalk globals declarations select: #canAssign
>
> maybe? Unlike class bindings, global vars are writable.
I agree with Eliot on this. While its currently true that Globals can be
written to and ClassBindings can't, this should not be relied on.

>  
>       Why not
>
>       Array streamContents: [ :stream |
>          Smalltalk globals associationsDo: [ :binding |
>              (binding class == Global
>              and: [ binding value isBehavior
>              and: [ binding key == binding value name ] ]) ifTrue:
>                   [ stream nextPut: binding ] ] ].
>
>
> Because we want to know the global *variables*, that is everything *but* the class bindings. So we look for instances of Global, not ClassBinding, no matter the value.
>
>       > The role of declarations and bindings is still not clear in Environments, therefore, as I wrote it before, unrelated to tool support, I still consider Environments to be incomplete.
>
>
> I thought 'declarations' are the things owned by this environment (excluding imports), whereas 'bindings' are the things visible in this environment (including imports).
That is correct. I meant that the API is neither clear nor ready.
Currently it's kind of a mess.
For example #associationAt: will return a binding from declarations, while
#associationOrUndeclaredAt: will look up stuff in bindings and undeclared.

I think the backwards compatible dictionary-like API should only use
declarations and undeclared, and should only be used with Smalltalk
globals.
But the new API is still to be done.

Levente

>
> - Bert -
>  
>
>

Reply | Threaded
Open this post in threaded view
|

Re: List of all global variables? (Environments?)

Levente Uzonyi
In reply to this post by Eliot Miranda-2
On Tue, 4 Apr 2017, Eliot Miranda wrote:

>
>
> On Apr 4, 2017, at 7:02 AM, Bert Freudenberg <[hidden email]> wrote:
>
>       On Tue, Apr 4, 2017 at 3:47 PM, Eliot Miranda <[hidden email]> wrote:
>             Hi Levente,
>
>             > On Apr 3, 2017, at 11:14 AM, Levente Uzonyi <[hidden email]> wrote:
>             >
>             >  The correct solution is:
>             >
>             > Array streamContents: [ :stream |
>             >    Smalltalk globals associationsDo: [ :binding |
>             >        binding class == Global ifTrue: [ stream nextPut: binding ] ] ].
>
>
> Or shorter 
>
>     Smalltalk globals declarations select: #canAssign
>
> maybe? Unlike class bindings, global vars are writable.
>
>
> I don't like this.  I see writability as orthogonal.  I'm sure there's good uses for read-only globals that are not classes.  I like the tenseness though.  So
>     Smalltalk globals declarations reject: #isClassBinding
>
> with
> isClassBinding ^value isBehavior and: [key == value name]
Why not just ^true?
It's a ClassBinding even if it's not initialized properly. And that should
be the Environment's responibility.

>
>        
>       Why not
>
>       Array streamContents: [ :stream |
>          Smalltalk globals associationsDo: [ :binding |
>              (binding class == Global
>              and: [ binding value isBehavior
>              and: [ binding key == binding value name ] ]) ifTrue:
>                   [ stream nextPut: binding ] ] ].
>
>
> Because we want to know the global *variables*, that is everything *but* the class bindings. So we look for instances of Global, not ClassBinding, no matter the value.
>
>
> Forgive me; I missed a not in there.  But my point is that the classes in global are in bindings whose keys are == to their name.  Everything else is a global variable.
We have separate subclasses: ClassBinding and Global. We should use those.

Levente

>
>
>       > The role of declarations and bindings is still not clear in Environments, therefore, as I wrote it before, unrelated to tool support, I still consider Environments to be incomplete.
>
>
> I thought 'declarations' are the things owned by this environment (excluding imports), whereas 'bindings' are the things visible in this environment (including imports).
>
>
> Is there a definition in text somewhere?  Colin?
>
>       - Bert -
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: List of all global variables? (Environments?)

Bert Freudenberg

On Tue 4. Apr 2017 at 19:05, Levente Uzonyi <[hidden email]> wrote:
On Tue, 4 Apr 2017, Eliot Miranda wrote:

>
>
> On Apr 4, 2017, at 7:02 AM, Bert Freudenberg <[hidden email]> wrote:
>
>       On Tue, Apr 4, 2017 at 3:47 PM, Eliot Miranda <[hidden email]> wrote:
>             Hi Levente,
>
>             > On Apr 3, 2017, at 11:14 AM, Levente Uzonyi <[hidden email]> wrote:
>             >
>             >  The correct solution is:
>             >
>             > Array streamContents: [ :stream |
>             >    Smalltalk globals associationsDo: [ :binding |
>             >        binding class == Global ifTrue: [ stream nextPut: binding ] ] ].
>
>
> Or shorter 
>
>     Smalltalk globals declarations select: #canAssign
>
> maybe? Unlike class bindings, global vars are writable.
>
>
> I don't like this.  I see writability as orthogonal.  I'm sure there's good uses for read-only globals that are not classes.  I like the tenseness though.  So
>     Smalltalk globals declarations reject: #isClassBinding
>
> with
> isClassBinding ^value isBehavior and: [key == value name]

Why not just ^true?
It's a ClassBinding even if it's not initialized properly. And that should
be the Environment's responibility.

>
>        
>       Why not
>
>       Array streamContents: [ :stream |
>          Smalltalk globals associationsDo: [ :binding |
>              (binding class == Global
>              and: [ binding value isBehavior
>              and: [ binding key == binding value name ] ]) ifTrue:
>                   [ stream nextPut: binding ] ] ].
>
>
> Because we want to know the global *variables*, that is everything *but* the class bindings. So we look for instances of Global, not ClassBinding, no matter the value.
>
>
> Forgive me; I missed a not in there.  But my point is that the classes in global are in bindings whose keys are == to their name.  Everything else is a global variable.

We have separate subclasses: ClassBinding and Global. We should use those.

Levente


Yep. Besides, with renaming on import the name of the class is not necessarily the same as its binding key.

- Bert -


>
>
>       > The role of declarations and bindings is still not clear in Environments, therefore, as I wrote it before, unrelated to tool support, I still consider Environments to be incomplete.
>
>
> I thought 'declarations' are the things owned by this environment (excluding imports), whereas 'bindings' are the things visible in this environment (including imports).
>
>
> Is there a definition in text somewhere?  Colin?
>
>       - Bert -
>
>
>