Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

First class Variables in Pharo


Published on

First class Variables in Pharo

First Name: Marcus
Last Name: Denker
Type: Talk

Video 1:
Video 2:

In this talk I will present the first class variables in Pharo:
With Slots, Class, and Global variables, we now model variables
as objects. These mete-objects provide a new reflective API for
variables and in addition, and allow the programmer to introduce
new kinds of variables.

This talk will present the ideas behind and show how to use first class
variables by presenting examples. It will present how variable meta
objects simplify reflecting on variables in combination with Reflectivity.

Marcus Denker is a permanent researcher at INRIA Lille - Nord Europe. Before,
he was a postdoc at the PLEIAD lab/DCC University of Chile and the Software
Composition Group, University of Bern. His research focuses on reflection
and meta-programming for dynamic languages. He is an active participant
in the Squeak and Pharo open source communities for many years and the
co-founder of 2Denker/Cologne/Germany. Marcus received a PhD in Computer
Science from the University of Bern/Switzerland in 2008 and a Dipl.-Inform.
(MSc) from the University of Karlsruhe/Germany in 2004.

Published in: Technology
  • Video 1: Video 2:
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

First class Variables in Pharo

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