Side Effect Free Functions in Object-Oriented Languages

TitleSide Effect Free Functions in Object-Oriented Languages
Publication TypeTechnical Report
Year of Publication1994
AuthorsRodriguez, N.., & Ierusalimschy R.
Other Numbers936

Mathematical functions have always been considered an important abstraction to be incorporated in programming languages. However, in most imperative languages this abstraction is not really supported, since any kind of side effect is allowed in a function, with at most a warning in the manual that such effects are not good programming practice.Several levels of control over side effects may be identified, ranging from this total lack of control up to functions that use only the functional subset of the language. In this paper we study the class of functions (called side effect free, or sef) which may not change old values in memory, but may create new values. A method is described for statically ensuring that a function is sef in the programming language School, an imperative object oriented language whose main design goal is to achieve good flexibility with a secure static type system. The proposed algorithm is completely done in compile time, integrated with the type checking. It ensures that any function accepted as sef cannot modify pre-existent objects, that is, objects created priorly to the function activation.A formal memory model for the execution of School is presented in the paper; this allows a precise definition of sef methods to be given.The method for checking that a function is sef relies on the concept of old objects. An object is old, from the point of view of a function invocation, if it was created before this invocation. Such objects are seen through a special filter, called the old transformation, during checking of a sef function, disallowing the invocation of methods which may cause any modification to them. One important point is that, since types in School are used solely at compile time, the use of this filter does not imply in any runtime conversions. After applying the old transformation, checking of sef functions reduces to the normal type checking in School, with only one extra rule: assignments to instance variables are forbidden. This means that the introduction of side effect free methods does not imply in much extra implementation effort or complexity of understanding.Guarantee of "side effect free-ness" is in general associated with lack of flexibility. We believe that the proposed method achieves good results in this direction, since sef functions can do most tasks which are in fact side-effect free. This is discussed in the paper with the use of some examples.

Bibliographic Notes

ICSI Technical Report TR-94-066

Abbreviated Authors

N. Rodriguez and R. Jerusalimschy

ICSI Publication Type

Technical Report