SlideShare a Scribd company logo
1 of 11
S.Ducasse 1
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Stéphane Ducasse
Stephane.Ducasse@univ-savoie.fr
http://www.listic.univ-savoie.fr/~ducasse/
Fun with Blocks in VW
S.Ducasse 2
License: CC-Attribution-ShareAlike 2.0
http://creativecommons.org/licenses/by-sa/2.0/
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
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
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
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
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)
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
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
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
S.Ducasse 11
Summary
Scary and costly but fun!

More Related Content

Viewers also liked (20)

Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
Stoop ed-class forreuse
Stoop ed-class forreuseStoop ed-class forreuse
Stoop ed-class forreuse
 
Stoop 423-smalltalk idioms
Stoop 423-smalltalk idiomsStoop 423-smalltalk idioms
Stoop 423-smalltalk idioms
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
 
5 - OOP - Smalltalk in a Nutshell (c)
5 - OOP - Smalltalk in a Nutshell (c)5 - OOP - Smalltalk in a Nutshell (c)
5 - OOP - Smalltalk in a Nutshell (c)
 
08 refactoring
08 refactoring08 refactoring
08 refactoring
 
01 intro
01 intro01 intro
01 intro
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
Stoop 390-instruction stream
Stoop 390-instruction streamStoop 390-instruction stream
Stoop 390-instruction stream
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 
12 - Conditions and Loops
12 - Conditions and Loops12 - Conditions and Loops
12 - Conditions and Loops
 
10 - OOP - Inheritance (a)
10 - OOP - Inheritance (a)10 - OOP - Inheritance (a)
10 - OOP - Inheritance (a)
 
Double Dispatch
Double DispatchDouble Dispatch
Double Dispatch
 
8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model
 
Stoop 301-internal objectstructureinvw
Stoop 301-internal objectstructureinvwStoop 301-internal objectstructureinvw
Stoop 301-internal objectstructureinvw
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
05 seaside
05 seaside05 seaside
05 seaside
 
Stoop 300-block optimizationinvw
Stoop 300-block optimizationinvwStoop 300-block optimizationinvw
Stoop 300-block optimizationinvw
 
Debugging VisualWorks
Debugging VisualWorksDebugging VisualWorks
Debugging VisualWorks
 
Stoop 422-naming idioms
Stoop 422-naming idiomsStoop 422-naming idioms
Stoop 422-naming idioms
 

Similar to Stoop 303-advanced blocks

Similar to Stoop 303-advanced blocks (20)

9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
 
9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)
 
Grand Central Dispatch Design Patterns
Grand Central Dispatch Design PatternsGrand Central Dispatch Design Patterns
Grand Central Dispatch Design Patterns
 
15 - Concurrency in VisualWorks
15 - Concurrency in VisualWorks15 - Concurrency in VisualWorks
15 - Concurrency in VisualWorks
 
Concurrency at the Database Layer
Concurrency at the Database Layer Concurrency at the Database Layer
Concurrency at the Database Layer
 
Java-Module 3-PPT-TPS.pdf.Java-Module 3-PPT-TPS.pdf
Java-Module 3-PPT-TPS.pdf.Java-Module 3-PPT-TPS.pdfJava-Module 3-PPT-TPS.pdf.Java-Module 3-PPT-TPS.pdf
Java-Module 3-PPT-TPS.pdf.Java-Module 3-PPT-TPS.pdf
 
Advanced Java Testing @ POSS 2019
Advanced Java Testing @ POSS 2019Advanced Java Testing @ POSS 2019
Advanced Java Testing @ POSS 2019
 
C++ Boot Camp Part 2
C++ Boot Camp Part 2C++ Boot Camp Part 2
C++ Boot Camp Part 2
 
block
blockblock
block
 
New types of tests for Java projects
New types of tests for Java projectsNew types of tests for Java projects
New types of tests for Java projects
 
New types of tests for Java projects
New types of tests for Java projectsNew types of tests for Java projects
New types of tests for Java projects
 
(Declarative) Jenkins Pipelines
(Declarative) Jenkins Pipelines(Declarative) Jenkins Pipelines
(Declarative) Jenkins Pipelines
 
Jenkins Pipelines
Jenkins PipelinesJenkins Pipelines
Jenkins Pipelines
 
Spock
SpockSpock
Spock
 
Docker by Example - Quiz
Docker by Example - QuizDocker by Example - Quiz
Docker by Example - Quiz
 
Docker by Example - Quiz
Docker by Example - QuizDocker by Example - Quiz
Docker by Example - Quiz
 
Metaprogramming code-that-writes-code
Metaprogramming code-that-writes-codeMetaprogramming code-that-writes-code
Metaprogramming code-that-writes-code
 
Docker and Puppet for Continuous Integration
Docker and Puppet for Continuous IntegrationDocker and Puppet for Continuous Integration
Docker and Puppet for Continuous Integration
 
Testing the waters of iOS
Testing the waters of iOSTesting the waters of iOS
Testing the waters of iOS
 

More from The World of Smalltalk (19)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
99 questions
99 questions99 questions
99 questions
 
13 traits
13 traits13 traits
13 traits
 
11 bytecode
11 bytecode11 bytecode
11 bytecode
 
10 reflection
10 reflection10 reflection
10 reflection
 
07 bestpractice
07 bestpractice07 bestpractice
07 bestpractice
 
06 debugging
06 debugging06 debugging
06 debugging
 
04 idioms
04 idioms04 idioms
04 idioms
 
03 standardclasses
03 standardclasses03 standardclasses
03 standardclasses
 
02 basics
02 basics02 basics
02 basics
 
Stoop sed-sharing ornot
Stoop sed-sharing ornotStoop sed-sharing ornot
Stoop sed-sharing ornot
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
Stoop ed-lod
Stoop ed-lodStoop ed-lod
Stoop ed-lod
 
Stoop ed-inheritance composition
Stoop ed-inheritance compositionStoop ed-inheritance composition
Stoop ed-inheritance composition
 
Stoop ed-frameworks
Stoop ed-frameworksStoop ed-frameworks
Stoop ed-frameworks
 
Stoop ed-dual interface
Stoop ed-dual interfaceStoop ed-dual interface
Stoop ed-dual interface
 
Stoop 440-adaptor
Stoop 440-adaptorStoop 440-adaptor
Stoop 440-adaptor
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 

Stoop 303-advanced blocks

  • 1. S.Ducasse 1 QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture. Stéphane Ducasse Stephane.Ducasse@univ-savoie.fr http://www.listic.univ-savoie.fr/~ducasse/ Fun with Blocks in VW
  • 2. S.Ducasse 2 License: CC-Attribution-ShareAlike 2.0 http://creativecommons.org/licenses/by-sa/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