Re: Difference between method and block - with examples
Steve Dekorte <[email protected]>
| Newsgroups | gmane.comp.lang.io |
|---|---|
| Message-ID | <[email protected]> |
You can use them like you would in any language but be aware that like in any other language, blocks retain the stack which they reference - so only use them if that's what you want. Here's an example of implementing a Smalltalk like ifTrue method with blocks: true ifTrue := method(a, a call; self) true ifFalse := method (a, self) false ifTrue := method(a, self) false ifFalse := method (a, a call; self) example use: foo ifTrue(block(x := 1)) Though it's terser to do: if(foo, x := 1) Note that unlike most other languages, we don't need to use blocks for control flow in Io since all arguments are passed as expressions and the receiver can determine if/how to eval them. Likewise, we also don't need conventional macros since we have far more control over evaluation than macros offer. On 2011-06-12 Sun, at 05:50 AM, Shashank Date wrote: > Where can I find examples of how to use "blocks" vs. methods? I have looked in the samples folder but found none. Also trawled the internets without much luck. > > I am looking for something more than what is given here:http://www.iolanguage.com/scm/io/docs/IoGuide.html#Objects-Blocks > > Thanks, > - Shashank