Rethinking the Default Class Hierarchy: An Object's Tale

Rethinking the Default Class Hierarchy: An Object's Tale
Slide Note
Embed
Share

In this insightful presentation, delve into the concept of every class in the D language having 'Object' as the root ancestor. Explore the significance of allowing users to choose methods and the search for good design in languages like Java, C#, and Rust. The proposed design suggests defining an empty root object and utilizing interfaces to expose desired behaviors.

  • Class Hierarchy
  • Object-Oriented Programming
  • D Language
  • Java
  • Design Principles

Uploaded on Feb 16, 2025 | 2 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Rethinking the Default Class Hierarchy: an Object s Tale Constantin Eduard St niloiu University POLITEHNICA of Bucharest eduard.c.staniloiu@gmail.com DConf 2019 London, May 8-11, 2019

  2. object.Object Every class defined in the D language has `Object` as the root ancestor 2

  3. object.Object Every class defined in the D language has `Object` as the root ancestor 3

  4. object.Object Every class defined in the D language has `Object` as the root ancestor 4

  5. object.Object Every class defined in the D language has `Object` as the root ancestor 5

  6. object.Object The user must be allowed to chose what methods he or she desires to implement Root objects must work in attributed code without issues. 6

  7. The Search of a Good Design A peek at Java s java.lang.Object The root of all classes Defines equals(Object), hashCode() and toString() Defines getClass() typeid() Defines a clone() method Has an object monitor 8

  8. The Search of a Good Design A peek at C# s System.Object The root of all classes Defines GetHashCode(), GetType() and ToString() Defines System.Threading.Monitor 9

  9. The Search of a Good Design A peek at Rust Data aggregates are all unrelated types No subtyping and no inheritance of data Relationships between various data types in Rust are established using traits Rust Traits are like interfaces in OOP languages 10

  10. Proposed Design Define an empty root object Define Interfaces that expose the desired behavior 11

  11. object.ProtoObject 12

  12. object.ProtoObject Object has two roles: taken by ProtoObject The root of all classes The default supertype of class definitions Recommended way going forward: Inherit from ProtoObject Implement interfaces that expose the desired behaviour 13

  13. Interfaces Introduced Interface Interface Method name Method name const @nogc nothrow pure @safe scope int opCmp(scope const ProtoObject); const @nogc nothrow pure @safe scope bool opEquals(scope const ProtoObject); const @nogc nothrow pure @safe scope size_t toHash(); const @nogc nothrow pure @safe scope void toString(scope void delegate(scope const(char)[])); Ordered Equals Hash Stringify 14

  14. Ordering 15

  15. Ordering 16

  16. Ordering Ordered implies that implementing types form a total order total and antisymmetric: exactly one of a < b, a == b or a > b is true transitive, a < b and b < c implies a < c 17

  17. Ordering 18

  18. ImplementOrdered(M) 19

  19. ImplementOrdered(M) 20

  20. ImplementOrdered(M) 21

  21. ImplementOrderedExcept(M) 22

  22. Improving the Ordered interface 23

  23. Equality 24

  24. Equality Equality must respect the following contract: Reflexivity: a == a Symmetry: a == b && b == a Transitivity: a == b && b == c => a == c 25

  25. ImplementEquals(M) 26

  26. ImplementEqualsExcept(M) 27

  27. Hashing ImplementHash(M ) ImplementHashExcept(M ) 28

  28. Stringifying 29

  29. Conclusion Working objects in attributed code Increased flexibility Less code bloat 30

  30. Check it out DIP https://github.com/edi33416/DIPs/blob/ProtoObjectI/DIP s/DIPxxxx.md Code https://github.com/edi33416/dmd/tree/dip_ProtoObject https://github.com/edi33416/druntime/tree/dip_ProtoObj ect 31

More Related Content