variables in pharo5
TRANSCRIPT
![Page 2: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/2.jpg)
Everything is an Object
![Page 3: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/3.jpg)
Everything?
![Page 4: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/4.jpg)
Classes, yes.
![Page 5: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/5.jpg)
Methods, yes
![Page 6: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/6.jpg)
But Variables?
![Page 7: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/7.jpg)
Everything is an object?
SmalltalkImage classVarNamed: #CompilerClass ==> returns value
Object binding class ==> Association
![Page 8: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/8.jpg)
Why not an Object?
![Page 9: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/9.jpg)
Globals/ClassVariables
• We are close: bindings are associations
• Add sublass “LiteralVariable”
• Sublasses GlobalVariable, ClassVariable
• Enhance API
![Page 10: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/10.jpg)
Globals/ClassVariables
SmalltalkImage classVariableNamed: #CompilerClass
Object binding class
![Page 11: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/11.jpg)
Globals: Reflective APiglobal := SmalltalkImage classVariableNamed: #CompilerClass
global read global write: someObject
+ helper methods + compatibility methods
![Page 12: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/12.jpg)
Everything is an object?
• Point instanceVariables
• 5@3 instVarNamed: ‘x’
• 5@3 instVarNamed: ‘y’ put: 6
![Page 13: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/13.jpg)
Why not an Object?
![Page 14: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/14.jpg)
Slots
Point slots
(Point slotNamed: #x) read: (3@4)
(Point slotNamed: #x) write: 7 to: (3@4)
![Page 15: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/15.jpg)
Variables+MetaLink
• Helper methods
• But: can’t we annotate variables directly?
Point assignmentNodes
![Page 16: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/16.jpg)
Variables + Links
• Object binding link: myMetaLink
• (Point slotNamed: #x) link: myMetaLink
(not yet in Pharo5)
![Page 17: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/17.jpg)
Opal Compiler
• Uses RB AST
• Based on Visitors
Text AST AST+vars IR CM
Parser Semantic Analysis
AST Translator+ IRBuilder
BytecodeBuilder+ Encoder
![Page 18: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/18.jpg)
Opal Compiler
• Name analysis finds the variables
• Code generator can delegate to them
Text AST AST+vars IR CM
Parser Semantic Analysis
AST Translator+ IRBuilder
BytecodeBuilder+ Encoder
![Page 19: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/19.jpg)
Gobals: code read
• By default compile reflective read
emitValue: aMethodBuilder aMethodBuilder pushLiteralVariable: #slot->self; send: #read
![Page 20: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/20.jpg)
Gobals: code write
• By default compile reflective write
emitStore: aMethodBuilder | tempName | tempName := Object new. aMethodBuilder addTemp: tempName; storeTemp: tempName; popTop; pushLiteralVariable: #global -> self; pushTemp: tempName; send: #write:
![Page 21: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/21.jpg)
Gobals: code write
• ClassVariable and GlobalVariable override
emitStore: methodBuilder
methodBuilder storeIntoLiteralVariable: self.
![Page 22: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/22.jpg)
Same for Slots
• Slot generates reflective read/write
• InstanceVariableSlot overrides for fast instance access via byte code
![Page 23: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/23.jpg)
What does that mean?
• Slots and Globals are instances of a class
• The compiler delegates code generation to the variable meta object
• Which means: We can define our own variables!
![Page 24: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/24.jpg)
Class Template
Object subclass: #Point slots: { #x. #y } classVariables: { } category: 'Kernel-BasicObjects'
![Page 25: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/25.jpg)
Class Template
Object subclass: #MyClass slots: { #x => WeakSlot } classVariables: { } category: 'Example'
![Page 26: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/26.jpg)
Examples: DEMO
• Simple Slot
• WeakSlot
• Property Slot
• Boolean
![Page 27: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/27.jpg)
RoadMap• Pharo3:
• Layout+Slots (hidden), Opal
• Pharo4
• Slots: Monticello support, class template
• Pharo5
• Remove old Compiler/AST
• Slots + Reflectivity: First finished version
![Page 28: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/28.jpg)
RoadMap
• Pharo6:
• Library of useful Slots
• Use e.g. Property Slots in Bloc/Morphic
![Page 29: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/29.jpg)
Future
• Can’t we model bit patterns and bind them to named virtual slots?
• How to model Array-like layouts better?
![Page 30: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/30.jpg)
Thanks!
• Work of many people…
• special thanks to… Toon Verwaest, Camillo Bruni, Martin Dias, Stephane Ducasse, Max Mattone and Camille Teruel
![Page 31: Variables in Pharo5](https://reader031.vdocuments.us/reader031/viewer/2022032118/55cccb29bb61ebca608b461c/html5/thumbnails/31.jpg)
Questions ?