[Pharo-project] Instantiating from a Symbol
Hello everybody, I am fairly new to ST and have been following this list since a few weeks. A very brief introduction of myself: I am a 32 year old student of informatics with a degree in political sciences. Currently i am located in Regensburg, Germany. Most of my previous programming was in python, along with some php and javascript. The issue I could not solve on my own is this: I would like to instantiate from symbols. My use-case is a collection of class names which I want to ask to return one instance each. Such that: #Foo asClass new -> a Foo whereas asClass is the method I am looking for. I am using Pharo 1.3. I did not find any description of what I want to achieve on the internet. Many thanks in advance, Markus
On 07.11.2011 15:29, Markus Rother wrote:
The issue I could not solve on my own is this: I would like to instantiate from symbols. My use-case is a collection of class names which I want to ask to return one instance each.
Such that: #Foo asClass new -> a Foo whereas asClass is the method I am looking for.
(self environment at: #Foo) new -> a Foo Cheers, Henry
On 11/07/2011 03:33 PM, Henrik Sperre Johansen wrote:
On 07.11.2011 15:29, Markus Rother wrote:
The issue I could not solve on my own is this: I would like to instantiate from symbols. My use-case is a collection of class names which I want to ask to return one instance each.
Such that: #Foo asClass new -> a Foo whereas asClass is the method I am looking for.
(self environment at: #Foo) new -> a Foo
Cheers, Henry
Grand! Thanks a lot Henry! M.
On 7 November 2011 15:33, Henrik Sperre Johansen <henrik.s.johansen@veloxit.no> wrote:
On 07.11.2011 15:29, Markus Rother wrote:
The issue I could not solve on my own is this: I would like to instantiate from symbols. My use-case is a collection of class names which I want to ask to return one instance each.
Such that: #Foo asClass new -> a Foo whereas asClass is the method I am looking for.
(self environment at: #Foo) new -> a Foo
or (Smalltalk globals classNamed: #Foo) just to make sure that you only interested in getting classes, not funny globals.
Cheers, Henry
-- Best regards, Igor Stasenko.
Hi Marcus, On 07 Nov 2011, at 15:29, Markus Rother wrote:
Hello everybody,
I am fairly new to ST and have been following this list since a few weeks.
A very brief introduction of myself: I am a 32 year old student of informatics with a degree in political sciences. Currently i am located in Regensburg, Germany. Most of my previous programming was in python, along with some php and javascript.
The issue I could not solve on my own is this: I would like to instantiate from symbols. My use-case is a collection of class names which I want to ask to return one instance each.
Such that: #Foo asClass new -> a Foo whereas asClass is the method I am looking for.
I am using Pharo 1.3.
I did not find any description of what I want to achieve on the internet.
Many thanks in advance, Markus
Welcome! Traditionally, one would do this: (Smalltalk at: #String) new Smalltalk here is not a class, but a global variable whose value is a SystemDictionary mapping class names to class objects. Pharo is working towards less dependency on globals like these, so the new way would be like this: (self environment at: #String) new (Where environment essentially does 'Smalltalk globals'), HTH, Sven
Hi,
Pharo is working towards less dependency on globals like these, so the new way would be like this:
(self environment at: #String) new
(Where environment essentially does 'Smalltalk globals'),
In my image (Pharo 1.3) #environment is defined in Behavior so I need to call: (self class environment at: #String) new have I missed something... Nick
2011/11/7 Nick Ager <nick.ager@gmail.com>:
Hi,
Pharo is working towards less dependency on globals like these, so the new way would be like this: Â Â Â Â (self environment at: #String) new
(Where environment essentially does 'Smalltalk globals'),
In my image (Pharo 1.3) #environment is defined in Behavior so I need to call: (self class environment at: #String) new have I missed something... Nick
It depends where you evaluate the snippet... Try to evaluate self in workspace, you'll get nil, but ina browser, you'll get the selected class... Nicolas
On 07 Nov 2011, at 16:12, Nick Ager wrote:
In my image (Pharo 1.3) #environment is defined in Behavior so I need to call:
(self class environment at: #String) new
have I missed somethingâ¦
Stéph, Nick is right. I guess the question is: how should normal client or user code that used to do something like Smalltalk at: #Point be written ? Should we just do Smalltalk globals or self environment. In the last case, there is the question whether an instance method would be needed. Sven
Stéph,
Nick is right. I guess the question is: how should normal client or user code that used to do something like
Smalltalk at: #Point
be written ?
Should we just do
Smalltalk globals
or
self environment.
environment should be used in a method while Smalltalk globals more for scripts. Stef
Stef, On 12 Nov 2011, at 23:04, Stéphane Ducasse wrote:
environment should be used in a method while Smalltalk globals more for scripts.
Yeah, but Nick's point is that #environment is a class method, not an instance method, and maybe it would be handy on the instance side. Sven
Am 13.11.2011 um 10:17 schrieb Sven Van Caekenberghe:
Stef,
On 12 Nov 2011, at 23:04, Stéphane Ducasse wrote:
environment should be used in a method while Smalltalk globals more for scripts.
Yeah, but Nick's point is that #environment is a class method, not an instance method, and maybe it would be handy on the instance side.
Absolutely. I want to have environments for code _and_ data. Not sure if it is applyable in a clean way into the same concept. But I like to be able to load a new version of my code into the same image. And I like to be able to run tests of my code without having side effects to my existing production data. Norbert
But environment is a business of classes and not instances. So environment is a class method and this is normal. And if you need to access from an instance method self class environment⦠makes it. Stef
Stef,
On 12 Nov 2011, at 23:04, Stéphane Ducasse wrote:
environment should be used in a method while Smalltalk globals more for scripts.
Yeah, but Nick's point is that #environment is a class method, not an instance method, and maybe it would be handy on the instance side.
Sven
2011/11/7 Sven Van Caekenberghe <sven@beta9.be>:
Hi Marcus,
On 07 Nov 2011, at 15:29, Markus Rother wrote:
Hello everybody,
I am fairly new to ST and have been following this list since a few weeks.
A very brief introduction of myself: I am a 32 year old student of informatics with a degree in political sciences. Currently i am located in Regensburg, Germany. Most of my previous programming was in python, along with some php and javascript.
The issue I could not solve on my own is this: I would like to instantiate from symbols. My use-case is a collection of class names which I want to ask to return one instance each.
Such that: #Foo asClass new -> a Foo whereas asClass is the method I am looking for.
I am using Pharo 1.3.
I did not find any description of what I want to achieve on the internet.
Many thanks in advance, Markus
Welcome!
Traditionally, one would do this:
    (Smalltalk at: #String) new
Smalltalk here is not a class, but a global variable whose value is a SystemDictionary mapping class names to class objects.
Pharo is working towards less dependency on globals like these, so the new way would be like this:
    (self environment at: #String) new
(Where environment essentially does 'Smalltalk globals'),
HTH,
Sven
For a few months, Smalltalk class -> SmalltalkImage (which in fact represents the Smalltalk system) And Smalltalk globals class -> SystemDictionary (the global namespace) Nicolas
Warm welcome Markus!
I am fairly new to ST and have been following this list since a few weeks.
A very brief introduction of myself: I am a 32 year old student of informatics with a degree in political sciences. Currently i am located in Regensburg, Germany. Most of my previous programming was in python, along with some php and javascript.
The issue I could not solve on my own is this: I would like to instantiate from symbols. My use-case is a collection of class names which I want to ask to return one instance each.
why are you trying to do? because symbols are not done to be instantiated like that. aString asSymbol is instantiating symbols for you. Stef
Such that: #Foo asClass new -> a Foo whereas asClass is the method I am looking for.
I am using Pharo 1.3.
I did not find any description of what I want to achieve on the internet.
Many thanks in advance, Markus
On 11/07/2011 11:08 PM, Stéphane Ducasse wrote:
The issue I could not solve on my own is this: I would like to instantiate from symbols. My use-case is a collection of class names which I want to ask to return one instance each. why are you trying to do? because symbols are not done to be instantiated like that.
aString asSymbol is instantiating symbols for you.
Stef
Thanks everybody for the kind, plenty and swift replies. Here is what I was up to: ChessBoard class>>newGame | newBoard | ... firstRow := #(Rook Knight Bishop Queen King Bishop Knight Rook). 1 to: 8 do: [ :col | (newBoard at: 1 at: col) putFigure: (firstRow at: col) asClass newWhite ]. ... Symbol>>asClass ^(Smalltalk classNamed: self) I don't see how a string representation would have helped me here...? Although, I am a noob, my intuition tells me that symbols are absolutely fine, here - especially considering they are used precisely for that purpose internally. Greets, Markus
On Mon, Nov 7, 2011 at 3:21 PM, Markus Rother <smalltalk@markusrother.de>wrote:
On 11/07/2011 11:08 PM, Stéphane Ducasse wrote:
The issue I could not solve on my own is this: I would like to instantiate from symbols. My use-case is a collection of class names which I want to ask to return one instance each.
why are you trying to do? because symbols are not done to be instantiated like that.
aString asSymbol is instantiating symbols for you.
Stef
Thanks everybody for the kind, plenty and swift replies.
Here is what I was up to:
ChessBoard class>>newGame | newBoard | ... firstRow := #(Rook Knight Bishop Queen King Bishop Knight Rook). 1 to: 8 do: [ :col | (newBoard at: 1 at: col) putFigure: (firstRow at: col) asClass newWhite ]. ...
Symbol>>asClass ^(Smalltalk classNamed: self)
I don't see how a string representation would have helped me here...? Although, I am a noob, my intuition tells me that symbols are absolutely fine, here - especially considering they are used precisely for that purpose internally.
But so is ChessBoard class>>newGame | newBoard | ... firstRow := {Rook. Knight. Bishop. Queen. King. Bishop. Knight. Rook}. 1 to: 8 do: [ :col | (newBoard at: 1 at: col) putFigure: (firstRow at: col) newWhite ]. ... as could be newBoard at: 1 put: ({Rook. Knight. Bishop. Queen. King. Bishop. Knight. Rook} collect: [:class| class newWhite]; at: 2 put: (1 to: 8) collect: [:ign| Pawn newWhite]; atAll: (3 to: 6) put: (Array new: 8); at: 7 put: (1 to: 8) collect: [:ign| Pawn newBlack]; at: 8 put: ({Rook. Knight. Bishop. Queen. King. Bishop. Knight. Rook} collect: [:class| class newBlack]
Greets, Markus
-- best, Eliot
2011/11/8 Eliot Miranda <eliot.miranda@gmail.com>:
On Mon, Nov 7, 2011 at 3:21 PM, Markus Rother <smalltalk@markusrother.de> wrote:
On 11/07/2011 11:08 PM, Stéphane Ducasse wrote:
The issue I could not solve on my own is this: I would like to instantiate from symbols. My use-case is a collection of class names which I want to ask to return one instance each.
why are you trying to do? because symbols are not done to be instantiated like that.
aString asSymbol is instantiating symbols for you.
Stef
Thanks everybody for the kind, plenty and swift replies.
Here is what I was up to:
ChessBoard class>>newGame | newBoard | ... firstRow := #(Rook Knight Bishop Queen King Bishop Knight Rook).    1 to: 8 do: [ :col |       (newBoard at: 1 at: col) putFigure: (firstRow at: col) asClass newWhite       ]. ...
Symbol>>asClass    ^(Smalltalk classNamed: self)
I don't see how a string representation would have helped me here...? Although, I am a noob, my intuition tells me that symbols are absolutely fine, here - especially considering they are used precisely for that purpose internally.
But so is
ChessBoard class>>newGame    | newBoard |    ...    firstRow := {Rook. Knight. Bishop. Queen. King. Bishop. Knight. Rook}.    1 to: 8 do: [ :col |       (newBoard at: 1 at: col) putFigure: (firstRow at: col) newWhite       ]. ... as could be    newBoard     at: 1 put: ({Rook. Knight. Bishop. Queen. King. Bishop. Knight. Rook} collect: [:class| class newWhite];     at: 2 put: (1 to: 8) collect: [:ign| Pawn newWhite];     atAll: (3 to: 6) put: (Array new: 8);
Then avoid statefull operations like ((board at; i) at: j) put: move. because you just shared some rows. ;) Nicolas
    at: 7 put: (1 to: 8) collect: [:ign| Pawn newBlack];     at: 8 put: ({Rook. Knight. Bishop. Queen. King. Bishop. Knight. Rook} collect: [:class| class newBlack]
Greets, Markus
-- best, Eliot
On Mon, Nov 7, 2011 at 3:46 PM, Nicolas Cellier < nicolas.cellier.aka.nice@gmail.com> wrote:
2011/11/8 Eliot Miranda <eliot.miranda@gmail.com>:
On Mon, Nov 7, 2011 at 3:21 PM, Markus Rother <smalltalk@markusrother.de
wrote:
On 11/07/2011 11:08 PM, Stéphane Ducasse wrote:
The issue I could not solve on my own is this: I would like to instantiate from symbols. My use-case is a collection of class names which I want to ask to return one instance each.
why are you trying to do? because symbols are not done to be instantiated like that.
aString asSymbol is instantiating symbols for you.
Stef
Thanks everybody for the kind, plenty and swift replies.
Here is what I was up to:
ChessBoard class>>newGame | newBoard | ... firstRow := #(Rook Knight Bishop Queen King Bishop Knight Rook). 1 to: 8 do: [ :col | (newBoard at: 1 at: col) putFigure: (firstRow at: col) asClass newWhite ]. ...
Symbol>>asClass ^(Smalltalk classNamed: self)
I don't see how a string representation would have helped me here...? Although, I am a noob, my intuition tells me that symbols are absolutely fine, here - especially considering they are used precisely for that
purpose
internally.
But so is
ChessBoard class>>newGame | newBoard | ... firstRow := {Rook. Knight. Bishop. Queen. King. Bishop. Knight. Rook}. 1 to: 8 do: [ :col | (newBoard at: 1 at: col) putFigure: (firstRow at: col) newWhite ]. ... as could be newBoard at: 1 put: ({Rook. Knight. Bishop. Queen. King. Bishop. Knight. Rook} collect: [:class| class newWhite]; at: 2 put: (1 to: 8) collect: [:ign| Pawn newWhite]; atAll: (3 to: 6) put: (Array new: 8);
Then avoid statefull operations like ((board at; i) at: j) put: move. because you just shared some rows.
;)
<blush>oops</blush>. Quite so. So... newBoard at: 1 put: ({Rook. Knight. Bishop. Queen. King. Bishop. Knight. Rook} collect: [:class| class newWhite]; at: 2 put: (1 to: 8) collect: [:ign| Pawn newWhite]; atAll: (3 to: 6) putAll: ((3 to: 6) collect: [:ign| Array new: 8]); at: 7 put: (1 to: 8) collect: [:ign| Pawn newBlack]; at: 8 put: ({Rook. Knight. Bishop. Queen. King. Bishop. Knight. Rook} collect: [:class| class newBlack]
Nicolas
at: 7 put: (1 to: 8) collect: [:ign| Pawn newBlack]; at: 8 put: ({Rook. Knight. Bishop. Queen. King. Bishop. Knight. Rook} collect: [:class| class newBlack]
Greets, Markus
-- best, Eliot
-- best, Eliot
ok you wanted to create a class not instantiate a symbol. I got it. Stef On Nov 8, 2011, at 12:21 AM, Markus Rother wrote:
On 11/07/2011 11:08 PM, Stéphane Ducasse wrote:
The issue I could not solve on my own is this: I would like to instantiate from symbols. My use-case is a collection of class names which I want to ask to return one instance each.
why are you trying to do? because symbols are not done to be instantiated like that.
aString asSymbol is instantiating symbols for you.
Stef
Thanks everybody for the kind, plenty and swift replies.
Here is what I was up to:
ChessBoard class>>newGame | newBoard | ... firstRow := #(Rook Knight Bishop Queen King Bishop Knight Rook). 1 to: 8 do: [ :col | (newBoard at: 1 at: col) putFigure: (firstRow at: col) asClass newWhite ]. ...
Symbol>>asClass ^(Smalltalk classNamed: self)
I don't see how a string representation would have helped me here...? Although, I am a noob, my intuition tells me that symbols are absolutely fine, here - especially considering they are used precisely for that purpose internally.
Greets, Markus
participants (9)
-
Eliot Miranda -
Henrik Sperre Johansen -
Igor Stasenko -
Markus Rother -
Nick Ager -
Nicolas Cellier -
Norbert Hartl -
Stéphane Ducasse -
Sven Van Caekenberghe