Uniform and Safe Metaclass Composition


Published on

Uniform and Safe Metaclass
Composition. Stéphane Ducasse, Nathanael Schaerli and Roel Wuyts (ESUG 2004, Koethen)

Published in: Technology
  • 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

No notes for slide

Uniform and Safe Metaclass Composition

  1. 1. S. Ducasse 1 Uniform and Safe Metaclass Composition Stéphane Ducasse, Nathanael Schaerli and Roel Wuyts ducasse@iam.unibe.ch http://www.iam.unibe.ch/~ducasse/
  2. 2. S. Ducasse 2 Why Metaclasses? • Classes are objects too • A class is an instance of a class called a metaclass • Uniformity: Classes are just objects • Control: Instance creation behavior • Reuse of class behavior
  3. 3. S. Ducasse 3 Implicit vs. Explicit • Implicit: programmer cannot specify the class • Safe but • Limited or no reuse • Explicit: • Composition metaclass is not safe • Ad-hoc composition mechanisms (for example: strings manipulation of neoclasstalk and dynamic class change) • Safe?: upward and downward compatible
  4. 4. S. Ducasse 4 Upward Compatible • aB i-foo => no error Meta A c-bar A i-foo A>>i-foo ^ self class c-bar Meta B B inherits from instance of ?
  5. 5. S. Ducasse 5 Downward Compatible • B c-bar => no error Meta A c-bar A i-bar Meta A>>c-bar ^ self new i-bar Meta B B inherits from instance of ?
  6. 6. S. Ducasse 6 Smalltalk-80 • Implicit • Safe • Limited reuse Meta A>>c-foo ^ self new i-bar Meta A c-bar c-foo A i-foo i-bar Meta B B A>>i-foo ^ self class c-bar inherits from instance of
  7. 7. S. Ducasse 7 Existing Solutions • Smalltalk-80 • Implicit, safe • But limited reuse • Clos • Explicit but help yourself! • NeoClasstalk • Dynamic class changes • Metaclass behavior specified as strings • MetaClasstalk • Double meta-layer with mixin composition
  8. 8. S. Ducasse 8 Our approach: traits • Implicit metaclasses • But traits to compose classes • Reuse of traits A B inherits from instance of Meta A Class Property2 Meta B Class Property1
  9. 9. S. Ducasse 9 What are Traits? • Traits are parameterized behaviors • Traits provide a set of methods • Traits require a set of methods • Traits are purely behavioral • Traits do not specify any state radius radius: center center: TCircle area bounds diameter hash
  10. 10. S. Ducasse 10 How are Traits Used? • Traits are the behavioral building blocks of classes • Class = Superclass + state + traits + glue methods • Class methods take precedence over trait methods Object ColoredCircle TColor TCircle
  11. 11. S. Ducasse 11 Conflicts Explicitly Resolved I) Override the conflict with a glue method  Aliases provide access to the conflicting methods II) Avoid the conflict  Exclude the conflicting method from one trait ColoredCircle TColor hash TCircle hash hash hash -> circleHash hash -> colorHash
  12. 12. S. Ducasse 12 Hierarchies Revisited... Boolean False True False class True class TSingleton Boolean class TAbstract TSingleton Behavior TCreator new basicNew Boolean class TAbstract new False class TSingleton new default reset basicNew uniqueInstance uniqueInstance:
  13. 13. S. Ducasse 13 Metalevel Reengineering TRememberInstances new rememberInstance instances reset rememberedInstances rememberedInstances: TInstantiator new TAllocate basicNew TInitInstantiator new initialize TAbstract new TFinal subclass: TSharedInstance reset sharedInstance directSharedInstance directSharedInstance: TDefault TSingleton new sharedNew ! new default ! sharedInstance uniqueInstance ! sharedInstance nonRememberingNew ! new is composed from TSingleton new Trait m1 ! m2 creates an alias m2 referring to m1 Legend
  14. 14. S. Ducasse 14 Traits Implementation • Available in Squeak 3.7 • Bootstrapping the kernel PureBehavior BehaviorTraitBehavior Class Description Class Metaclass Trait Description Trait
  15. 15. S. Ducasse 15 Conclusion • Uniform • Traits are instance and class level! • Explicit Composition • Reuse but Safe