Your SlideShare is downloading. ×
Stoop 303-advanced blocks
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Stoop 303-advanced blocks


Published on

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. S.Ducasse 1 QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture. Stéphane Ducasse Fun with Blocks in VW
  • 2. S.Ducasse 2 License: CC-Attribution-ShareAlike 2.0
  • 3. S.Ducasse 3 • VM represents the state of execution as Context objects • for method MethodContext • for block BlockContext • aContext contains a reference to the context from which it is invoked, the receiver arguments, temporaries in the Context • We call home context the context in which a block is defined Advanced Blocks inVW
  • 4. S.Ducasse 4 • Arguments, temporaries, instance variables are lexically scoped in Smalltalk • These variables are bound in the context in which the block is defined and not in the context in which the block is evaluated Lexical Scope
  • 5. S.Ducasse 5 Test>>testScope "self new testScope" |t| t := 15. self testBlock: [Transcript show: t printString] Test>>testBlock:aBlock |t| t := 50. aBlock value Test new testBlock -> 15 and not 50 Lexical Scope by Example
  • 6. S.Ducasse 6 • ^ should be the last statement of a block body [ Transcript show: 'two'. ^ self. Transcript show: 'not printed'] • ^ return exits the method containing it. • test • Transcript show: 'one'. • 1 isZero • ifFalse: [ 0 isZero ifTrue: [ Transcript show: 'two'. ^ self]]. • Transcript show: ' not printed' • -> one two Returning from a Block
  • 7. S.Ducasse 7 • Simple block [:x :y| x*x. x+y] returns the value of the last statement to the method that sends it the message value • Continuation blocks [:x :y| ^ x + y] returns the value to the method that activated its homeContext • As a block is always evaluated in its homeContext, it is possible to attempt to return from a method which has already returned using other return.This runtime error condition is trapped by theVM. • Object>>returnBlock • ^[^self] • Object new returnBlock • -> Exception Returning From a Block (II)
  • 8. S.Ducasse 8 |b| b:= [:x| Transcript show: x. x]. b value:‘ a’. b value:‘ b’. b:= [:x| Transcript show: x. ^x]. b value:‘ a’. b value:‘ b’. Continuation blocks cannot be executed several times! Continuation Blocks
  • 9. S.Ducasse 9 Test>>testScope |t| t := 15. self testBlock: [Transcript show:‘*’,t printString,‘*’. ^35 ]. ^ 15 Test>>testBlock:aBlock |t| t := 50. aBlock value. self halt. Test new testBlock print: *15* and not halt. return: 35 Example of Block Evaluation
  • 10. S.Ducasse 10 |val| val := [:exit | |goSoon| goSoon := Dialog confirm: 'Exit now?'. goSoon ifTrue: [exit value: 'Bye']. Transcript show: 'Not exiting'. 'last value'] myValueWithExit. Transcript show: val. val yes -> print ‘Bye’ and return ‘Bye’ no -> print ‘Not Exiting’2 and return ‘2’ BlockClosure>>myValueWithExit self value: [:arg| ^arg ]. ^ ‘2’ BlockClosure>>myValueWithExit ^ self value: [:arg | ^ arg] to get no -> print ‘NotExiting’lastValue Creating and escape mechanism
  • 11. S.Ducasse 11 Summary Scary and costly but fun!