SlideShare a Scribd company logo
Variables in Pharo
Marcus Denker, Inria
http://marcusdenker.de
Plan for an interactive
Exploration
• These Slides where done as an outline / plan for an
interactive exploration

• They might therefore be not exactly the same as the
content of the Demo
Variables in ST80
• Temporary Variable

• Instance Variable, Class Instance Variable

• Class Variable (and Pool Variable)

• Globals

• Pseudo Variables: self, super, thisContext
Instance Variables
• De
fi
ned by the Class (or Trait)

• Can be read via the object:

• instVarNamed:(put:), #instVarAt:(put:
)

• Instance Variables have an o
ff
set in the Object

• De
fi
ned by the order of the de
fi
ned vars in the Hierarchy
1@2 instVarNamed: 'x'
Temporary Variable
• De
fi
ned by a method or Block

• Arguments are temps, too

• Can be read via the context

• #tempNamed:, tempNamed:put
:

• With Closures this is more complex than you ever want to
know!
[| temp | temp := 1. thisContext tempNamed: 'temp' ] value
Globals
• Entries in the “Smalltalk globals” Dictionary

• Contain the value

• Can be read via the global Dictionary

• Access via #value / value: on the Association

• Class Vars and Pool Vars are just Associations from other
Dictionaries
Smalltalk globals at: #Object.
Object binding value.
“Everything is an Object”
• For Variables… not really
Globals/Class Vars
• Here we have at least the Association (#binding):

• But there is no “GlobalVariable” class

• No API other than #value:/#value

• Classes de
fi
ne just names of variables
Object binding
Instance Variables
• The class just knows the names

• There is no Object representing instance variables

• Classes de
fi
ne just names of variables

• Bytecode accesses by o
ff
set
Point allInstVarNames
Temporary Variables
• The methods know nothing. Even to know the variable
name we need the compiler (and the source)

• There is no object representing temp Variables

• Re
fl
ective read and write is *hard* -> compiler needs to
create extensive meta-data
Why Not Do Better?
• Of course memory was a concern in 1980, but today we
should be able to do better!

• Why not have objects (and a class Hierarchy) that
describes all Variables in the system?
Variables
• Every de
fi
ned Variable is described a meta object

• Class Hierarchy: Variable

• GlobalVariable

• ClassVariable

• Temporary Variables

• Instance Variables (aka Slots)
The Hierarchy
• Variable

• LiteralVariable

• ClassVariable

• GlobalVariable

• UndeclaredVariable

• WorkspaceVariable
• LocalVariable

• ArgumentVariable

• TemporaryVariable

• ReservedVariable

• SelfVariable

• SuperVariable

• ThisContextVariable

• Slot
Example: vars of a class
• Get all Variables of a class

• Inspect it

• #usingMethods
Point instanceVariables
Instance Variable
• Read x in a Point

• Write

• read/write without sending a message to the object
(Point instanceVariables
fi
rst) read: (5@4)
point := 5@4.
(Point instanceVariables
fi
rst) write: 100 to: point.
Globals
• Object binding class

• Object binding read

• We keep the Association API so the Global Variables can
play the role of associations in the global dictionary.
Object binding usingMethods
Temporary Variables
• There are too many to allocate them all

• They are created on demand (with the AST)
((LinkedList>>#do:) temporaryVariableNamed: 'aLink')
#lookupVar:
• Every variable knows the scope is was de
fi
ned in

• Every scope know the outer scope

• #lookupVar: looks up names along the scope
[ | temp |thisContext lookupVar: 'temp' ] value.
[ | temp |thisContext lookupVar: ‘Object' ] value
(Point slotNamed: #x ) scope outerScope
Debugger: Read Vars
• In the Debugger we to be able to read Variables from a
DoIt.

• lookupVar, then readInContext works for all Variables!

• DoItIn: uses this:
[ | temp | temp :=1 . (thisContext lookupVar: 'temp')
readInContext: thisContext] value
Context>>readVariableNamed: aName
^ (self lookupVar: aName) readInContext: self
Variables as AST
Annotations
• Pharo uses the RB AST

• RBVariableNode instance for every use of a Variable

• Annotated with subclasses of Variables:
(Point>>#x) ast variableNodes
fi
rst variable == (Point slotNamed: #x)
OCASTSemanticAnalyzer
• OCASTSemanticAnalyzer is the visitor that does the name
analysis

• Adds a scope for each block/the method

• Adds de
fi
ned variables to the scope

• Every RBVariableNode use will get annotated with the
variable that #lookUpVar:
fi
nds
Variables and Bytecode
• Compiler just delegates to the Variable

• InstanceVariableSlot>>#emitStore:

• emitStore/emitValue:
emitStore: methodBuilder
"generate store bytecode"
methodBuilder storeInstVar: index
Does that mean…
• If variables are de
fi
ned by a class, could we not make a
subclass?

• And even override the code generation methods ?!
Now let’s create our own
kind of Variable!
Lazy Variables
• Two ways to initialize instance state in ST80

• implement #initialize method (#new calls it)

• use accessors and lazy init pattern

• Can we not do better? Can the Variable not initialize
itself?
Lazy Variables
InstanceVariableSlot subclass: #LazyInitializedInstanceVariabl
e

instanceVariableNames: 'default
'

classVariableNames: '
'

package: ‘CompilerTalk
'

printOn: aStrea
m

aStream
 

store: self name
;

nextPutAll: ' => '
;

nextPutAll: self class name
;

nextPutAll: ' default: '
.

default printOn: aStrea
m
Lazy Variables
read: anObjec
t

"if the value is nil, we write the default value
"

^ (super read: anObject) ifNil: [
 

self write: default to: anObject
]

emitValue: aMethodBuilde
r

"generate bytecode for '<varname> ifNil: [<varname> := default]'
"

aMethodBuilde
r

pushInstVar: index
;

pushDup
;

pushLiteral: nil
;

send: #==
;

jumpAheadTo: #target if: false
;

popTop
;

pushLiteral: default
;

storeInstVar: index
;

jumpAheadTarget: #targe
t
Let’s use it
Object subclass: #MyClas
s

instanceVariableNames: 'var
'

classVariableNames: '
'

package: ‘CompilerTalk'
?
How can we make ‘var’ to be a LazyInitializedInstanceVariable?
Class De
fi
nition
• We need a new way to de
fi
ne classes: Fluid Class
De
fi
nition

• uses cascade for extensibility

• no string, but {} arrays for variables
Fluid Class De
fi
nition
Object << #Poin
t

slots: { #x . #y }
;

tag: 'BasicObjects'
;

package: 'Kernel'
Fluid Class De
fi
nition
• Pharo9: Default is the ST80 style class de
fi
nition

• Fluid can be enabled

• It is used automatically when needed (when using a self
de
fi
ned Variable, for example)

• Goal: Default for Pharo 10
Let’s use it
Object << #MyClas
s

slots: { #var }
;

package: ‘CompilerTalk'
#notation for normal instance Variables
Let’s use it
Object << #MyClas
s

slots: { #var => LazyInitializedInstanceVariable default: 5 }
;

package: ‘CompilerTalk'
For de
fi
ning other variables: use =>
MyClass new var
.

(MyClass new var: 8) var
Inspect method to see bytecode
put halt in read: method of Slot
thisProcess
• To get the current Process we use a message send to a
global variable. But we could use a variable like
thisProces
s

• This avoids a message send (and possible interrupt
check) as we can emit a bytecode
self, thisContext…
• (Object lookupVar: ‘thisContext’) usingMethods
• See classes

• ThisContextVariable

• SelfVariable

• SuperVariable
The Code
ReservedVariable << #ThisProcessVariable
slots: {};
tag: 'Variables';
package: 'Kernel'
emitValue: methodBuilder
methodBuilder pushThisProcess
======
class side:
variableName
^ 'thisProcess'
======
Smalltalk globals resetReservedVariables
Compatibility
• Fully backward compatible: we can load ST80 style class
de
fi
nitions (and Pharo9 just shows this view by default)

• Re
fl
ective API is compatible: “instVarAt:”… still exist

• If you want to restrict yourself to ST80, a checker could
be easily created
Compatibility
• But if you start to use your own kind of Variables, you
code will not be “ST80” compatible anymore

• But you will be able to use the power of the new
abstraction provided

• Was this not the original idea behind Smalltalk? That a
Programming System is a Medium?
Where do we use it?
• We are careful! We use Pharo ourselves… 

• We need a stable system to work with

• We need to learn about how to use Variables best

• Variables are used by the Compiler internally

• Every instance variable is an InstanceVariableSlot (Globals, class vars)

• Variables are used by the Debugging infrastructure to read/write

• replaced the DebuggerMethodMap

• The Spec UI Framework uses ObservableSlot
Next Steps Variables
• DoitVariable for nicer code in #DoItIn: methods 

• Undeclared Variables

• programmer interaction in read/write, not compile!

• better behaviour for test-
fi
rst development

• Implement ThisProcessVariable in Pharo10

• Use WeakSlot to simplify some code
Next Steps Fluid Classes
• Finish the last problems (see issue tracker), make it the
default

• Experiments with Meta Data for Classes

• Tag abstract classes

• Experiment with Pragmas for classes

• Compiler, compiler plugging, compiler options
More..
• Extend the MetaLink Model to allow MetaLinks on
Variables (
fi
rst code is there already)

• More Experiments about Slot Composition 

• Implement Default value once, use it on ClassVariable,
InstanceVariableSlot and WeakSlot

• First prototype, but it turned out to be too complex
Thanks…
• This is the work on *many* contributors from the Pharo
Community

• Thanks for lots of interesting discussions, ideas, and
code!
Help Wanted
• We are always interested in improvements!

• Pharo 10 is under active development

• 30-40 Pull Requests integrated per week

• Your Improvements are Welcome!
https://github.com/pharo-project/pharo
Questions?

More Related Content

What's hot

Solid principles
Solid principlesSolid principles
Solid principles
Monica Rodrigues
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
lalithambiga kamaraj
 
Java 8 date & time api
Java 8 date & time apiJava 8 date & time api
Java 8 date & time api
Rasheed Waraich
 
Token Authentication in ASP.NET Core
Token Authentication in ASP.NET CoreToken Authentication in ASP.NET Core
Token Authentication in ASP.NET Core
Stormpath
 
Strings in Java
Strings in Java Strings in Java
Strings in Java
Hitesh-Java
 
Sql Injection Myths and Fallacies
Sql Injection Myths and FallaciesSql Injection Myths and Fallacies
Sql Injection Myths and Fallacies
Karwin Software Solutions LLC
 
Real Life Clean Architecture
Real Life Clean ArchitectureReal Life Clean Architecture
Real Life Clean Architecture
Mattia Battiston
 
Object Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non StaticObject Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non Static
OXUS 20
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
priya_trivedi
 
TypeScript - An Introduction
TypeScript - An IntroductionTypeScript - An Introduction
TypeScript - An Introduction
NexThoughts Technologies
 
Basic Javascript
Basic JavascriptBasic Javascript
Basic Javascript
Bunlong Van
 
OWASP Top 10 2021 Presentation (Jul 2022)
OWASP Top 10 2021 Presentation (Jul 2022)OWASP Top 10 2021 Presentation (Jul 2022)
OWASP Top 10 2021 Presentation (Jul 2022)
TzahiArabov
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Serhat Can
 
Spring Core
Spring CoreSpring Core
Spring Core
Pushan Bhattacharya
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 
Java Spring Framework
Java Spring FrameworkJava Spring Framework
Java Spring Framework
Mehul Jariwala
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
Sohanur63
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
Math-Circle
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it works
Mindfire Solutions
 

What's hot (20)

Solid principles
Solid principlesSolid principles
Solid principles
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Java 8 date & time api
Java 8 date & time apiJava 8 date & time api
Java 8 date & time api
 
Token Authentication in ASP.NET Core
Token Authentication in ASP.NET CoreToken Authentication in ASP.NET Core
Token Authentication in ASP.NET Core
 
Strings in Java
Strings in Java Strings in Java
Strings in Java
 
Sql Injection Myths and Fallacies
Sql Injection Myths and FallaciesSql Injection Myths and Fallacies
Sql Injection Myths and Fallacies
 
Real Life Clean Architecture
Real Life Clean ArchitectureReal Life Clean Architecture
Real Life Clean Architecture
 
Object Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non StaticObject Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non Static
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
TypeScript - An Introduction
TypeScript - An IntroductionTypeScript - An Introduction
TypeScript - An Introduction
 
Basic Javascript
Basic JavascriptBasic Javascript
Basic Javascript
 
OWASP Top 10 2021 Presentation (Jul 2022)
OWASP Top 10 2021 Presentation (Jul 2022)OWASP Top 10 2021 Presentation (Jul 2022)
OWASP Top 10 2021 Presentation (Jul 2022)
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Spring Core
Spring CoreSpring Core
Spring Core
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
 
Java Spring Framework
Java Spring FrameworkJava Spring Framework
Java Spring Framework
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it works
 

Similar to Variables in Pharo

First Class Variables as AST Annotations
First Class Variables as AST AnnotationsFirst Class Variables as AST Annotations
First Class Variables as AST Annotations
Marcus Denker
 
First Class Variables as AST Annotations
 First Class Variables as AST Annotations First Class Variables as AST Annotations
First Class Variables as AST Annotations
ESUG
 
Java
JavaJava
Learning core java
Learning core javaLearning core java
Learning core java
Abhay Bharti
 
Javascript classes and scoping
Javascript classes and scopingJavascript classes and scoping
Javascript classes and scoping
Patrick Sheridan
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in Pharo
Pharo
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_reviewEdureka!
 
Lecture: "Advanced Reflection: MetaLinks"
Lecture: "Advanced Reflection: MetaLinks"Lecture: "Advanced Reflection: MetaLinks"
Lecture: "Advanced Reflection: MetaLinks"
Marcus Denker
 
Learning from "Effective Scala"
Learning from "Effective Scala"Learning from "Effective Scala"
Learning from "Effective Scala"
Kazuhiro Sera
 
Lecture. Advanced Reflection: MetaLinks
Lecture. Advanced Reflection: MetaLinksLecture. Advanced Reflection: MetaLinks
Lecture. Advanced Reflection: MetaLinks
Marcus Denker
 
33rd degree talk: open and automatic coding conventions with walkmod
33rd degree talk: open and automatic coding conventions with walkmod33rd degree talk: open and automatic coding conventions with walkmod
33rd degree talk: open and automatic coding conventions with walkmod
walkmod
 
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinksVUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
Marcus Denker
 
ppt_on_java.pptx
ppt_on_java.pptxppt_on_java.pptx
ppt_on_java.pptx
MAYANKKUMAR492040
 
Java
JavaJava
Slot Composition
Slot CompositionSlot Composition
Slot Composition
Marcus Denker
 
Slot Composition
Slot CompositionSlot Composition
Slot Composition
ESUG
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud Foundry
Pray Desai
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in Pharo
Marcus Denker
 

Similar to Variables in Pharo (20)

First Class Variables as AST Annotations
First Class Variables as AST AnnotationsFirst Class Variables as AST Annotations
First Class Variables as AST Annotations
 
First Class Variables as AST Annotations
 First Class Variables as AST Annotations First Class Variables as AST Annotations
First Class Variables as AST Annotations
 
Java
JavaJava
Java
 
Learning core java
Learning core javaLearning core java
Learning core java
 
Javascript classes and scoping
Javascript classes and scopingJavascript classes and scoping
Javascript classes and scoping
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in Pharo
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
 
Java
JavaJava
Java
 
Lecture: "Advanced Reflection: MetaLinks"
Lecture: "Advanced Reflection: MetaLinks"Lecture: "Advanced Reflection: MetaLinks"
Lecture: "Advanced Reflection: MetaLinks"
 
Learning from "Effective Scala"
Learning from "Effective Scala"Learning from "Effective Scala"
Learning from "Effective Scala"
 
Javasession6
Javasession6Javasession6
Javasession6
 
Lecture. Advanced Reflection: MetaLinks
Lecture. Advanced Reflection: MetaLinksLecture. Advanced Reflection: MetaLinks
Lecture. Advanced Reflection: MetaLinks
 
33rd degree talk: open and automatic coding conventions with walkmod
33rd degree talk: open and automatic coding conventions with walkmod33rd degree talk: open and automatic coding conventions with walkmod
33rd degree talk: open and automatic coding conventions with walkmod
 
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinksVUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
 
ppt_on_java.pptx
ppt_on_java.pptxppt_on_java.pptx
ppt_on_java.pptx
 
Java
JavaJava
Java
 
Slot Composition
Slot CompositionSlot Composition
Slot Composition
 
Slot Composition
Slot CompositionSlot Composition
Slot Composition
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud Foundry
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in Pharo
 

More from Marcus Denker

Soil And Pharo
Soil And PharoSoil And Pharo
Soil And Pharo
Marcus Denker
 
ConstantBlocks in Pharo11
ConstantBlocks in Pharo11ConstantBlocks in Pharo11
ConstantBlocks in Pharo11
Marcus Denker
 
Demo: Improved DoIt
Demo: Improved DoItDemo: Improved DoIt
Demo: Improved DoIt
Marcus Denker
 
Supporting Pharo / Getting Pharo Support
Supporting Pharo / Getting Pharo SupportSupporting Pharo / Getting Pharo Support
Supporting Pharo / Getting Pharo Support
Marcus Denker
 
thisContext in the Debugger
thisContext in the DebuggerthisContext in the Debugger
thisContext in the Debugger
Marcus Denker
 
Improving code completion for Pharo
Improving code completion for PharoImproving code completion for Pharo
Improving code completion for Pharo
Marcus Denker
 
Lecture: Advanced Reflection. MetaLinks
Lecture: Advanced Reflection. MetaLinksLecture: Advanced Reflection. MetaLinks
Lecture: Advanced Reflection. MetaLinks
Marcus Denker
 
PHARO IOT
PHARO IOTPHARO IOT
PHARO IOT
Marcus Denker
 
Open-Source: An Infinite Game
Open-Source: An Infinite GameOpen-Source: An Infinite Game
Open-Source: An Infinite Game
Marcus Denker
 
Lecture: MetaLinks
Lecture: MetaLinksLecture: MetaLinks
Lecture: MetaLinks
Marcus Denker
 
PharoTechTalk: Contributing to Pharo
PharoTechTalk: Contributing to PharoPharoTechTalk: Contributing to Pharo
PharoTechTalk: Contributing to Pharo
Marcus Denker
 
Feedback Loops in Practice
Feedback Loops in PracticeFeedback Loops in Practice
Feedback Loops in Practice
Marcus Denker
 
Pharo6 - ESUG17
Pharo6 - ESUG17Pharo6 - ESUG17
Pharo6 - ESUG17
Marcus Denker
 
Pharo6
Pharo6Pharo6
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond Smalltak
Marcus Denker
 
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond Smalltak
Marcus Denker
 
Perfection & Feedback Loops or: why worse is better
Perfection & Feedback Loops or: why worse is betterPerfection & Feedback Loops or: why worse is better
Perfection & Feedback Loops or: why worse is better
Marcus Denker
 
Dynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection PromisesDynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection Promises
Marcus Denker
 
Variables in Pharo5
Variables in Pharo5Variables in Pharo5
Variables in Pharo5
Marcus Denker
 
Reflection in Pharo5
Reflection in Pharo5Reflection in Pharo5
Reflection in Pharo5
Marcus Denker
 

More from Marcus Denker (20)

Soil And Pharo
Soil And PharoSoil And Pharo
Soil And Pharo
 
ConstantBlocks in Pharo11
ConstantBlocks in Pharo11ConstantBlocks in Pharo11
ConstantBlocks in Pharo11
 
Demo: Improved DoIt
Demo: Improved DoItDemo: Improved DoIt
Demo: Improved DoIt
 
Supporting Pharo / Getting Pharo Support
Supporting Pharo / Getting Pharo SupportSupporting Pharo / Getting Pharo Support
Supporting Pharo / Getting Pharo Support
 
thisContext in the Debugger
thisContext in the DebuggerthisContext in the Debugger
thisContext in the Debugger
 
Improving code completion for Pharo
Improving code completion for PharoImproving code completion for Pharo
Improving code completion for Pharo
 
Lecture: Advanced Reflection. MetaLinks
Lecture: Advanced Reflection. MetaLinksLecture: Advanced Reflection. MetaLinks
Lecture: Advanced Reflection. MetaLinks
 
PHARO IOT
PHARO IOTPHARO IOT
PHARO IOT
 
Open-Source: An Infinite Game
Open-Source: An Infinite GameOpen-Source: An Infinite Game
Open-Source: An Infinite Game
 
Lecture: MetaLinks
Lecture: MetaLinksLecture: MetaLinks
Lecture: MetaLinks
 
PharoTechTalk: Contributing to Pharo
PharoTechTalk: Contributing to PharoPharoTechTalk: Contributing to Pharo
PharoTechTalk: Contributing to Pharo
 
Feedback Loops in Practice
Feedback Loops in PracticeFeedback Loops in Practice
Feedback Loops in Practice
 
Pharo6 - ESUG17
Pharo6 - ESUG17Pharo6 - ESUG17
Pharo6 - ESUG17
 
Pharo6
Pharo6Pharo6
Pharo6
 
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond Smalltak
 
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond Smalltak
 
Perfection & Feedback Loops or: why worse is better
Perfection & Feedback Loops or: why worse is betterPerfection & Feedback Loops or: why worse is better
Perfection & Feedback Loops or: why worse is better
 
Dynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection PromisesDynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection Promises
 
Variables in Pharo5
Variables in Pharo5Variables in Pharo5
Variables in Pharo5
 
Reflection in Pharo5
Reflection in Pharo5Reflection in Pharo5
Reflection in Pharo5
 

Recently uploaded

Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 

Recently uploaded (20)

Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 

Variables in Pharo

  • 1. Variables in Pharo Marcus Denker, Inria http://marcusdenker.de
  • 2. Plan for an interactive Exploration • These Slides where done as an outline / plan for an interactive exploration • They might therefore be not exactly the same as the content of the Demo
  • 3. Variables in ST80 • Temporary Variable • Instance Variable, Class Instance Variable • Class Variable (and Pool Variable) • Globals • Pseudo Variables: self, super, thisContext
  • 4. Instance Variables • De fi ned by the Class (or Trait) • Can be read via the object: • instVarNamed:(put:), #instVarAt:(put: ) • Instance Variables have an o ff set in the Object • De fi ned by the order of the de fi ned vars in the Hierarchy 1@2 instVarNamed: 'x'
  • 5. Temporary Variable • De fi ned by a method or Block • Arguments are temps, too • Can be read via the context • #tempNamed:, tempNamed:put : • With Closures this is more complex than you ever want to know! [| temp | temp := 1. thisContext tempNamed: 'temp' ] value
  • 6. Globals • Entries in the “Smalltalk globals” Dictionary • Contain the value • Can be read via the global Dictionary • Access via #value / value: on the Association • Class Vars and Pool Vars are just Associations from other Dictionaries Smalltalk globals at: #Object. Object binding value.
  • 7. “Everything is an Object” • For Variables… not really
  • 8. Globals/Class Vars • Here we have at least the Association (#binding): • But there is no “GlobalVariable” class • No API other than #value:/#value • Classes de fi ne just names of variables Object binding
  • 9. Instance Variables • The class just knows the names • There is no Object representing instance variables • Classes de fi ne just names of variables • Bytecode accesses by o ff set Point allInstVarNames
  • 10. Temporary Variables • The methods know nothing. Even to know the variable name we need the compiler (and the source) • There is no object representing temp Variables • Re fl ective read and write is *hard* -> compiler needs to create extensive meta-data
  • 11. Why Not Do Better? • Of course memory was a concern in 1980, but today we should be able to do better! • Why not have objects (and a class Hierarchy) that describes all Variables in the system?
  • 12. Variables • Every de fi ned Variable is described a meta object • Class Hierarchy: Variable • GlobalVariable • ClassVariable • Temporary Variables • Instance Variables (aka Slots)
  • 13. The Hierarchy • Variable • LiteralVariable • ClassVariable • GlobalVariable • UndeclaredVariable • WorkspaceVariable • LocalVariable • ArgumentVariable • TemporaryVariable • ReservedVariable • SelfVariable • SuperVariable • ThisContextVariable • Slot
  • 14. Example: vars of a class • Get all Variables of a class • Inspect it • #usingMethods Point instanceVariables
  • 15. Instance Variable • Read x in a Point • Write • read/write without sending a message to the object (Point instanceVariables fi rst) read: (5@4) point := 5@4. (Point instanceVariables fi rst) write: 100 to: point.
  • 16. Globals • Object binding class • Object binding read • We keep the Association API so the Global Variables can play the role of associations in the global dictionary. Object binding usingMethods
  • 17. Temporary Variables • There are too many to allocate them all • They are created on demand (with the AST) ((LinkedList>>#do:) temporaryVariableNamed: 'aLink')
  • 18. #lookupVar: • Every variable knows the scope is was de fi ned in • Every scope know the outer scope • #lookupVar: looks up names along the scope [ | temp |thisContext lookupVar: 'temp' ] value. [ | temp |thisContext lookupVar: ‘Object' ] value (Point slotNamed: #x ) scope outerScope
  • 19. Debugger: Read Vars • In the Debugger we to be able to read Variables from a DoIt. • lookupVar, then readInContext works for all Variables! • DoItIn: uses this: [ | temp | temp :=1 . (thisContext lookupVar: 'temp') readInContext: thisContext] value Context>>readVariableNamed: aName ^ (self lookupVar: aName) readInContext: self
  • 20. Variables as AST Annotations • Pharo uses the RB AST • RBVariableNode instance for every use of a Variable • Annotated with subclasses of Variables: (Point>>#x) ast variableNodes fi rst variable == (Point slotNamed: #x)
  • 21. OCASTSemanticAnalyzer • OCASTSemanticAnalyzer is the visitor that does the name analysis • Adds a scope for each block/the method • Adds de fi ned variables to the scope • Every RBVariableNode use will get annotated with the variable that #lookUpVar: fi nds
  • 22. Variables and Bytecode • Compiler just delegates to the Variable • InstanceVariableSlot>>#emitStore: • emitStore/emitValue: emitStore: methodBuilder "generate store bytecode" methodBuilder storeInstVar: index
  • 23. Does that mean… • If variables are de fi ned by a class, could we not make a subclass? • And even override the code generation methods ?!
  • 24. Now let’s create our own kind of Variable!
  • 25. Lazy Variables • Two ways to initialize instance state in ST80 • implement #initialize method (#new calls it) • use accessors and lazy init pattern • Can we not do better? Can the Variable not initialize itself?
  • 26. Lazy Variables InstanceVariableSlot subclass: #LazyInitializedInstanceVariabl e instanceVariableNames: 'default ' classVariableNames: ' ' package: ‘CompilerTalk ' printOn: aStrea m aStream store: self name ; nextPutAll: ' => ' ; nextPutAll: self class name ; nextPutAll: ' default: ' . default printOn: aStrea m
  • 27. Lazy Variables read: anObjec t "if the value is nil, we write the default value " ^ (super read: anObject) ifNil: [ self write: default to: anObject ] emitValue: aMethodBuilde r "generate bytecode for '<varname> ifNil: [<varname> := default]' " aMethodBuilde r pushInstVar: index ; pushDup ; pushLiteral: nil ; send: #== ; jumpAheadTo: #target if: false ; popTop ; pushLiteral: default ; storeInstVar: index ; jumpAheadTarget: #targe t
  • 28. Let’s use it Object subclass: #MyClas s instanceVariableNames: 'var ' classVariableNames: ' ' package: ‘CompilerTalk' ? How can we make ‘var’ to be a LazyInitializedInstanceVariable?
  • 29. Class De fi nition • We need a new way to de fi ne classes: Fluid Class De fi nition • uses cascade for extensibility • no string, but {} arrays for variables
  • 30. Fluid Class De fi nition Object << #Poin t slots: { #x . #y } ; tag: 'BasicObjects' ; package: 'Kernel'
  • 31. Fluid Class De fi nition • Pharo9: Default is the ST80 style class de fi nition • Fluid can be enabled • It is used automatically when needed (when using a self de fi ned Variable, for example) • Goal: Default for Pharo 10
  • 32. Let’s use it Object << #MyClas s slots: { #var } ; package: ‘CompilerTalk' #notation for normal instance Variables
  • 33. Let’s use it Object << #MyClas s slots: { #var => LazyInitializedInstanceVariable default: 5 } ; package: ‘CompilerTalk' For de fi ning other variables: use => MyClass new var . (MyClass new var: 8) var Inspect method to see bytecode put halt in read: method of Slot
  • 34. thisProcess • To get the current Process we use a message send to a global variable. But we could use a variable like thisProces s • This avoids a message send (and possible interrupt check) as we can emit a bytecode
  • 35. self, thisContext… • (Object lookupVar: ‘thisContext’) usingMethods • See classes • ThisContextVariable • SelfVariable • SuperVariable
  • 36. The Code ReservedVariable << #ThisProcessVariable slots: {}; tag: 'Variables'; package: 'Kernel' emitValue: methodBuilder methodBuilder pushThisProcess ====== class side: variableName ^ 'thisProcess' ====== Smalltalk globals resetReservedVariables
  • 37. Compatibility • Fully backward compatible: we can load ST80 style class de fi nitions (and Pharo9 just shows this view by default) • Re fl ective API is compatible: “instVarAt:”… still exist • If you want to restrict yourself to ST80, a checker could be easily created
  • 38. Compatibility • But if you start to use your own kind of Variables, you code will not be “ST80” compatible anymore • But you will be able to use the power of the new abstraction provided • Was this not the original idea behind Smalltalk? That a Programming System is a Medium?
  • 39. Where do we use it? • We are careful! We use Pharo ourselves… • We need a stable system to work with • We need to learn about how to use Variables best • Variables are used by the Compiler internally • Every instance variable is an InstanceVariableSlot (Globals, class vars) • Variables are used by the Debugging infrastructure to read/write • replaced the DebuggerMethodMap • The Spec UI Framework uses ObservableSlot
  • 40. Next Steps Variables • DoitVariable for nicer code in #DoItIn: methods • Undeclared Variables • programmer interaction in read/write, not compile! • better behaviour for test- fi rst development • Implement ThisProcessVariable in Pharo10 • Use WeakSlot to simplify some code
  • 41. Next Steps Fluid Classes • Finish the last problems (see issue tracker), make it the default • Experiments with Meta Data for Classes • Tag abstract classes • Experiment with Pragmas for classes • Compiler, compiler plugging, compiler options
  • 42. More.. • Extend the MetaLink Model to allow MetaLinks on Variables ( fi rst code is there already) • More Experiments about Slot Composition • Implement Default value once, use it on ClassVariable, InstanceVariableSlot and WeakSlot • First prototype, but it turned out to be too complex
  • 43. Thanks… • This is the work on *many* contributors from the Pharo Community • Thanks for lots of interesting discussions, ideas, and code!
  • 44. Help Wanted • We are always interested in improvements! • Pharo 10 is under active development • 30-40 Pull Requests integrated per week • Your Improvements are Welcome! https://github.com/pharo-project/pharo