Hi,
I'm pretty new to smalltalk. Is there a way to use a String as a code block so I can send it the 'value' message? Thanks, Sparrott |
Hi,
write in the IDE the string inside [ and ] and send it the message value. e.g. [some statements as sting] value If you have a string within an application and you want to evaluate it as code, you must compile it ([...]) first and evaluate the result second. Josef sparrott wrote: Hi, |
In reply to this post by sparrott
One simple way is to use the Compiler:
Compiler evaluate: '[6 + 7] value' Or you can create a block with help of BlockClosure and the method readFromString And then pass the message value to it: block := BlockClosure readFromString: '[6 + 7]'. block value Or if the block is one with arguments: block := BlockClosure readFromString: '[:x :y | x + y]'. block value: 6 value: 7 Björn -----Original Message----- From: sparrott [mailto:[hidden email]] Sent: den 14 november 2006 12:48 To: [hidden email] Subject: [SPAM] - Evaluate string as a code block - Sender is probably forged (SPF Softfail) Hi, I'm pretty new to smalltalk. Is there a way to use a String as a code block so I can send it the 'value' message? Thanks, Sparrott -- View this message in context: http://www.nabble.com/Evaluate-string-as-a-code-block-tf2628952.html#a7336063 Sent from the VisualWorks mailing list archive at Nabble.com. |
Of course you can do it in the following way too:
(Compiler evaluate: '[6 + 7]') value -----Original Message----- From: Björn Eiderbäck [mailto:[hidden email]] Sent: den 14 november 2006 12:56 To: sparrott; [hidden email] Subject: [SPAM] - RE: [SPAM] - Evaluate string as a code block - Sender is probably forged (SPF Softfail) - Sender is probably forged (SPF Softfail) One simple way is to use the Compiler: Compiler evaluate: '[6 + 7] value' Or you can create a block with help of BlockClosure and the method readFromString And then pass the message value to it: block := BlockClosure readFromString: '[6 + 7]'. block value Or if the block is one with arguments: block := BlockClosure readFromString: '[:x :y | x + y]'. block value: 6 value: 7 Björn -----Original Message----- From: sparrott [mailto:[hidden email]] Sent: den 14 november 2006 12:48 To: [hidden email] Subject: [SPAM] - Evaluate string as a code block - Sender is probably forged (SPF Softfail) Hi, I'm pretty new to smalltalk. Is there a way to use a String as a code block so I can send it the 'value' message? Thanks, Sparrott -- View this message in context: http://www.nabble.com/Evaluate-string-as-a-code-block-tf2628952.html#a7336063 Sent from the VisualWorks mailing list archive at Nabble.com. |
In reply to this post by sparrott
You might try:
(Compiler evaluate: '[3 + 4]') value Runar |
In reply to this post by sparrott
On Nov 14, 2006, at 3:47, sparrott wrote:
You can take any expression and place it between []'s. Doing so defers the evaluation of the expression until you send the message 'value' you to the block. block := [Dialog warn: 'Hi!']. No dialog will open right away. It's only at some future time when you send 'block value' that the dialog will pop up. Also, in addition to "compiling" any expression in code, or using doit, you can keep track of a string representation of the source of your program: sourceCode := 'Dialog warn: ''Hi!''' (the double `s are how you embed a string tick inside of another string) Just like the block example, no dialog opens right away. To make it "happen", you use an expression like: Compiler evaluate: sourceCode Now your dialog opens. Compiling and evaluating source code is of course slower than having the code compiled but deferred in a block. With these two tools, you can do interesting combinations: block := Compiler evaluate: '[' , sourceCode , ']' allows you to have an arbitrary source text which you turn into a block for later evaluation by sending 'value' to the block. The arrangement is loosely associative. block := [Compiler evaluate: sourceCode] Of course in this case, if sourceCode changed during the life of the block, the result of sending it 'value' would change too, whereas with the first it would not. And of course both constructs are nestable: block := [[Dialog warn: 'Hi!']] returns a block that when sent 'value' will return a block that when sent 'value' will open the dialog. Compiler evaluate: 'Compiler evaluate: sourceCode' is a nested invocation of the Compiler evaluating strings. HTH -- Travis Griggs Objologist "There are a thousand hacking at the branches of evil to one who is striking at the root" - Henry David Thoreau |
Free forum by Nabble | Edit this page |