Abstract

Context. An extension method is a method declared in a package other than the package of its host class. Thanks to extension methods, developers can adapt to their needs classes they do not own: adding methods to core classes is a typical use case. This is particularly useful for adapting software and therefore to increase reusability. Inquiry. In most dynamically-typed languages, extension methods are globally visible. Because any developer can define extension methods for any class, naming conflicts occur: if two developers define an extension method with the same signature in the same class, only one extension method is visible and overwrites the other. Similarly, if two developers each define an extension method with the same name in a class hierarchy, one overrides the other. To avoid such "accidental overrides", some dynamically-typed languages limit the visibility of an extension method to a particular scope. However, their semantics have not been fully described and compared. In addition, these solutions typically rely on a dedicated and slow method lookup algorithm to resolve conflicts at runtime. Approach. In this article, we present a formalization of the underlying models of Ruby refinements, Groovy categories, Classboxes, and Method Shelters that are scoping extension method solutions in dynamically-typed languages. Knowledge. Our formal framework allows us to objectively compare and analyze the shortcomings of the studied solutions and other different approaches such as MultiJava. In addition, language designers can use our formal framework to determine which mechanism has less risk of "accidental overrides". Grounding. Our comparison and analysis of existing solutions is grounded because it is based on denotational semantics formalizations. Importance. Extension methods are widely used in programming languages that support them, especially dynamically-typed languages such as Pharo, Ruby or Python. However, without a carefully designed mechanism, this feature can cause insidious hidden bugs or can be voluntarily used to gain access to protected operations, violate encapsulation or break fundamental invariants.

Highlights

  • Extension methods are a popular feature in dynamically-typed object-oriented languages

  • When invoked from within this package, this redefinition is taken into account, even in indirect calls: when invoking the print() method defined in the SimpleEditor package, the redefined version of printIndentation(int) will be executed and not the one defined in the SimpleEditor package

  • To study the different design choices of each model, we present a formal specification of a method lookup algorithm for scoped extension methods

Read more

Summary

Introduction

Extension methods are a popular feature in dynamically-typed object-oriented languages. Variants of extension methods are available in many dynamically-typed languages: it is known as open classes in Ruby [ ], categories in Objective-C [ ] and Groovy [ ], and extension methods in Smalltalk [ ] and Pharo [ ]. Local rebinding is a method-lookup algorithm first defined in the Classbox model [ , ] and refined in the Method Shelters model [ ] and hierarchical layer-based class extensions [ ]. This property permits extension methods to override regular methods in a contextual manner. In figure , the MyEditor package defines an extension method printIndentation(int) that redefines the one in the original package This extension method prints spaces instead of tabs. When invoked from within this package, this redefinition is taken into account, even in indirect calls: when invoking the print() method defined in the SimpleEditor package, the redefined version of printIndentation(int) will be executed and not the one defined in the SimpleEditor package

Objectives
Methods
Discussion
Conclusion
Full Text
Paper version not known

Talk to us

Join us for a 30 min session where you can share your feedback and ask us any queries you have

Schedule a call

Disclaimer: All third-party content on this website/platform is and will remain the property of their respective owners and is provided on "as is" basis without any warranties, express or implied. Use of third-party content does not indicate any affiliation, sponsorship with or endorsement by them. Any references to third-party content is to identify the corresponding services and shall be considered fair use under The CopyrightLaw.