pharo icon indicating copy to clipboard operation
pharo copied to clipboard

Get the variable names of objects

Open privat opened this issue 2 years ago • 3 comments

This comes from a question on the Discord. I thought it was a nice exercise for me :)

The context, if I'm correct, is that you are playing on the playground and call complex methods that call complex methods, etc. At some point, you want to know the name of temporary variable that hold some objects.

The proposed method Context>>variableNamesOf: just do that

|a b|
a := 'hello'.
b := 'world'.
(thisContext variableNamesOf: a) >>> #(#a).
b := a.
(thisContext variableNamesOf: a) >>> #(#a #b).

privat avatar Sep 16 '22 12:09 privat

Pay attention you have too much fun :)

Ducasse avatar Sep 16 '22 20:09 Ducasse

Hello, I did not understand the do it context is needed. Is it only for the tests? Context >> searchDoIt

jordanmontt avatar Sep 17 '22 09:09 jordanmontt

Failing tests not related:

  • [CI] Failing test: ProperMethodCategorizationTest>>testNoUncategorizedMethods #11669
  • [CI] two failing test in SpCommandTest #11702

MarcusDenker avatar Sep 21 '22 14:09 MarcusDenker

If you don't declare the temps variables you get an empty array

Capture d’écran 2022-09-21 à 23 31 41

I don't know if that is expected or not. But one normally doesn't declare the names of the temp variable in the Playground

jordanmontt avatar Sep 21 '22 21:09 jordanmontt

Yes, these Variables in the playground are not temps, but WorkspaceVariables, what the compiler does is to add a special "RequestorScope" to the scope hierarchy that has the defintions.

This means that if #variableNamesOf: would ask the scope (and make sure the parent scope is asked, too), we could could find those variables (reading works with #readInContext:, too).

MarcusDenker avatar Sep 22 '22 12:09 MarcusDenker

Hi all, what do we do with this?

guillep avatar Jan 27 '23 09:01 guillep

I will reroll when I have the time.

privat avatar Jan 30 '23 14:01 privat