logo       

The public interface of a program unit in Scala: msg#00448

lang.scala

Subject: The public interface of a program unit in Scala

(In the spirit of designing a language for readability vs writability.)

For the purposes of this note, let us use the term "public interface of a program unit" to mean the information about the program unit that is relevant to its use. (Here I mean "interface" in the colloquial sense, I will use the phrase JavaInterface to refer to Java's intefaces.)

Java has a fairly simple and intuitive answer to this question. A program unit is an abstract or concrete class or interface.
The public interface of a program unit P contains:
(1) Attributes that specify whether this unit is a class or an interface, its accessibility, if it is abstract etc.
(1a) If P is a class, its constructors.
(2) The type associated with P. This is the name of P and the bundle of members associated with P (fields with their name, type and accessibility, and methods with their name, signature and accessibility).
(3) For each method of P, possibly some (informal) behavioral pre/post conditions (e.g. the sort method sorts any input lists, what locks must be held when it is called, what locks it seeks to acquire,
(4) The set of types P implements or extends

(1) is needed in order to know whether P is visible at the point of use, whether P can be instantiated, used in an extends/implements clause etc. (1a) is needed to construct instances of P. (2) is needed to know what can be done with instances of P. (3) to know whether the use of an operation on P is legal, and once I have applied it what can be said about the result. (4) is needed to determine which implicit coercions are legal and to avoid creating a cyclic dependency when defining a new programming unit that references P.)

Of great importance is what the public interface does *not* contain. For these are things that I do not need to carry around in my head. In particular, the public interface does not contain:
-- the structure of the inheritance hierarchy in which P is embedded, (only the projection needed in (4))
-- the types of the members in the classes/interfaces in the inheritance hierarchy above P (note their behavioral invariants may be different from those of the corresponding members at P) ... unless I am extending P to get Q and using super in the body of a method in Q.
-- the code in the body of the methods of P
-- whether the definition of methods in P is inherited or defined at P
-- the private members of P

Has someone worked out the corresponding answer for Scala?

In particular, what do I need to keep in my head about implicits (impllicit parameters, implicit defs) and traits, case classes and pattern-matching?

e.g. it seems the public interface needs to include the inheritance graph G of a trait (and the types of the members of the classes/traits in that graph), because each use of a trait requires the construction of a class linearization (Sec 5.1.2), and this depends on G.

Best,
Vijay

--
X10: Programming parallel machines, productively (http://x10.sf.net)




<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise