Movatterモバイル変換


[0]ホーム

URL:


Chapter 3 Objects in OCaml



This chapter gives an overview of the object-oriented features ofOCaml.

Note that the relationship between object, class and type in OCaml isdifferent than in mainstream object-oriented languages such as Java andC++, so you shouldn’t assume that similar keywords mean the same thing.Object-oriented features are used much less frequently in OCaml thanin those languages. OCaml has alternatives that are often more appropriate,such as modules and functors. Indeed, many OCaml programs do not use objectsat all.

1 Classes and objects

The classpoint below defines one instance variablex and two methodsget_x andmove. The initial value of the instance variable is0.The variablex is declared mutable, so the methodmove can changeits value.

#class point =objectvalmutable x = 0method get_x = xmethod move d = x <- x + dend;;
class point :objectvalmutable x : intmethod get_x : intmethod move : int -> unitend

We now create a new pointp, instance of thepoint class.

#let p =new point;;
val p : point = <obj>

Note that the type ofp ispoint. This is an abbreviationautomatically defined by the class definition above. It stands for theobject type<get_x : int; move : int -> unit>, listing the methodsof classpoint along with their types.

We now invoke some methods ofp:

# p#get_x;;
- : int = 0
# p#move 3;;
- : unit = ()
# p#get_x;;
- : int = 3

The evaluation of the body of a class only takes place at objectcreation time. Therefore, in the following example, the instancevariablex is initialized to different values for two differentobjects.

#let x0 =ref 0;;
val x0 : intref = {contents = 0}
#class point =objectvalmutable x = incr x0; !x0method get_x = xmethod move d = x <- x + dend;;
class point :objectvalmutable x : intmethod get_x : intmethod move : int -> unitend
#new point#get_x;;
- : int = 1
#new point#get_x;;
- : int = 2

The classpoint can also be abstracted over the initial values ofthex coordinate.

#class point =fun x_init ->objectvalmutable x = x_initmethod get_x = xmethod move d = x <- x + dend;;
class point : int ->objectvalmutable x : intmethod get_x : intmethod move : int -> unitend

Like in function definitions, the definition above can beabbreviated as:

#class point x_init =objectvalmutable x = x_initmethod get_x = xmethod move d = x <- x + dend;;
class point : int ->objectvalmutable x : intmethod get_x : intmethod move : int -> unitend

An instance of the classpoint is now a function that expects aninitial parameter to create a point object:

#new point;;
- : int -> point = <fun>
#let p =new point 7;;
val p : point = <obj>

The parameterx_init is, of course, visible in the whole body of thedefinition, including methods. For instance, the methodget_offsetin the class below returns the position of the object relative to itsinitial position.

#class point x_init =objectvalmutable x = x_initmethod get_x = xmethod get_offset = x - x_initmethod move d = x <- x + dend;;
class point : int ->objectvalmutable x : intmethod get_offset : intmethod get_x : intmethod move : int -> unitend

Expressions can be evaluated and bound before defining the object bodyof the class. This is useful to enforce invariants. For instance,points can be automatically adjusted to the nearest point on a grid,as follows:

#class adjusted_point x_init =let origin = (x_init / 10) * 10inobjectvalmutable x = originmethod get_x = xmethod get_offset = x - originmethod move d = x <- x + dend;;
class adjusted_point : int ->objectvalmutable x : intmethod get_offset : intmethod get_x : intmethod move : int -> unitend

(One could also raise an exception if thex_init coordinate is noton the grid.) In fact, the same effect could be obtained here bycalling the definition of classpoint with the value of theorigin.

#class adjusted_point x_init = point ((x_init / 10) * 10);;
class adjusted_point : int -> point

An alternate solution would have been to define the adjustment ina special allocation function:

#let new_adjusted_point x_init =new point ((x_init / 10) * 10);;
val new_adjusted_point : int -> point = <fun>

However, the former pattern is generally more appropriate, sincethe code for adjustment is part of the definition of the class and will beinherited.

This ability provides class constructors as can be found in otherlanguages. Several constructors can be defined this way to build objects ofthe same class but with different initialization patterns; analternative is to use initializers, as described below insection 3.4.

2 Immediate objects

There is another, more direct way to create an object: create itwithout going through a class.

The syntax is exactly the same as for class expressions, but theresult is a single object rather than a class. All the constructsdescribed in the rest of this section also apply to immediate objects.

#let p =objectvalmutable x = 0method get_x = xmethod move d = x <- x + dend;;
val p : < get_x : int; move : int -> unit > = <obj>
# p#get_x;;
- : int = 0
# p#move 3;;
- : unit = ()
# p#get_x;;
- : int = 3

Unlike classes, which cannot be defined inside an expression,immediate objects can appear anywhere, using variables from theirenvironment.

#let minmax x y =if x < ythenobjectmethod min = xmethod max = yendelseobjectmethod min = ymethod max = xend;;
val minmax : 'a -> 'a -> < max : 'a; min : 'a > = <fun>

Immediate objects have two weaknesses compared to classes: their typesare not abbreviated, and you cannot inherit from them. But these twoweaknesses can be advantages in some situations, as we will seein sections 3.3 and 3.10.

3 Reference to self

A method or an initializer can invoke methods on self (that is,the current object). For that, self must be explicitly bound, here tothe variables (s could be any identifier, even though we willoften choose the nameself.)

#class printable_point x_init =object (s)valmutable x = x_initmethod get_x = xmethod move d = x <- x + dmethod print = print_int s#get_xend;;
class printable_point : int ->objectvalmutable x : intmethod get_x : intmethod move : int -> unitmethod print : unitend
#let p =new printable_point 7;;
val p : printable_point = <obj>
# p#print;;
7- : unit = ()

Dynamically, the variables is bound at the invocation of a method. Inparticular, when the classprintable_point is inherited, the variables will be correctly bound to the object of the subclass.

A common problem with self is that, as its type may be extended insubclasses, you cannot fix it in advance. Here is a simple example.

#let ints =ref [];;
val ints : '_weak1 listref = {contents = []}
#class my_int =object (self)method n = 1method register = ints :=self :: !intsend ;;
Error: The value self has type < n : int; register : 'a; .. > but an expression was expected of type 'weak1 Self type cannot escape its class

You can ignore the first two lines of the error message. What mattersis the last one: putting self into an external reference would make itimpossible to extend it through inheritance.We will see in section 3.12 a workaround to thisproblem.Note however that, since immediate objects are not extensible, theproblem does not occur with them.

#let my_int =object (self)method n = 1method register = ints := self :: !intsend;;
val my_int : < n : int; register : unit > = <obj>

4 Initializers

Let-bindings within class definitions are evaluated before the objectis constructed. It is also possible to evaluate an expressionimmediately after the object has been built. Such code is written asan anonymous hidden method called an initializer. Therefore, it canaccess self and the instance variables.

#class printable_point x_init =let origin = (x_init / 10) * 10inobject (self)valmutable x = originmethod get_x = xmethod move d = x <- x + dmethod print = print_int self#get_xinitializer print_string"new point at "; self#print; print_newline ()end;;
class printable_point : int ->objectvalmutable x : intmethod get_x : intmethod move : int -> unitmethod print : unitend
#let p =new printable_point 17;;
new point at 10val p : printable_point = <obj>

Initializers cannot be overridden. On the contrary, all initializers areevaluated sequentially.Initializers are particularly useful to enforce invariants.Another example can be seen in section 8.1.

5 Virtual methods

It is possible to declare a method without actually defining it, usingthe keywordvirtual. This method will be provided later insubclasses. A class containing virtual methods must be flaggedvirtual, and cannot be instantiated (that is, no object of this classcan be created). It still defines type abbreviations (treating virtual methodsas other methods.)

#classvirtual abstract_point x_init =object (self)methodvirtual get_x : intmethod get_offset = self#get_x - x_initmethodvirtual move : int -> unitend;;
classvirtual abstract_point : int ->objectmethod get_offset : intmethodvirtual get_x : intmethodvirtual move : int -> unitend
#class point x_init =objectinherit abstract_point x_initvalmutable x = x_initmethod get_x = xmethod move d = x <- x + dend;;
class point : int ->objectvalmutable x : intmethod get_offset : intmethod get_x : intmethod move : int -> unitend

Instance variables can also be declared as virtual, with the same effectas with methods.

#classvirtual abstract_point2 =objectvalmutablevirtual x : intmethod move d = x <- x + dend;;
classvirtual abstract_point2 :objectvalmutablevirtual x : intmethod move : int -> unitend
#class point2 x_init =objectinherit abstract_point2valmutable x = x_initmethod get_offset = x - x_initend;;
class point2 : int ->objectvalmutable x : intmethod get_offset : intmethod move : int -> unitend

6 Private methods

Private methods are methods that do not appear in object interfaces.They can only be invoked from other methods of the same object.

#class restricted_point x_init =object (self)valmutable x = x_initmethod get_x = xmethodprivate move d = x <- x + dmethod bump = self#move 1end;;
class restricted_point : int ->objectvalmutable x : intmethod bump : unitmethod get_x : intmethodprivate move : int -> unitend
#let p =new restricted_point 0;;
val p : restricted_point = <obj>
#p#move 10 ;;
Error: This expression has type restricted_point It has no method move
# p#bump;;
- : unit = ()

Note that this is not the same thing as private and protected methodsin Java or C++, which can be called from other objects of the sameclass. This is a direct consequence of the independence between typesand classes in OCaml: two unrelated classes may produceobjects of the same type, and there is no way at the type level toensure that an object comes from a specific class. However a possibleencoding of friend methods is given in section 3.17.

Private methods are inherited (they are by default visible in subclasses),unless they are hidden by signature matching, as described below.

Private methods can be made public in a subclass.

#class point_again x =object (self)inherit restricted_point xmethodvirtual move : _end;;
class point_again : int ->objectvalmutable x : intmethod bump : unitmethod get_x : intmethod move : int -> unitend

The annotationvirtual here is only used to mention a method withoutproviding its definition. Since we didn’t add theprivateannotation, this makes the method public, keeping the originaldefinition.

An alternative definition is

#class point_again x =object (self : < move : _; ..> )inherit restricted_point xend;;
class point_again : int ->objectvalmutable x : intmethod bump : unitmethod get_x : intmethod move : int -> unitend

The constraint on self’s type is requiring a publicmove method, andthis is sufficient to overrideprivate.

One could think that a private method should remain private in a subclass.However, since the method is visible in a subclass, it is always possibleto pick its code and define a method of the same name that runs thatcode, so yet another (heavier) solution would be:

#class point_again x =objectinherit restricted_point xas supermethod move = super#moveend;;
class point_again : int ->objectvalmutable x : intmethod bump : unitmethod get_x : intmethod move : int -> unitend

Of course, private methods can also be virtual. Then, the keywords mustappear in this order:method private virtual.

7 Class interfaces

Class interfaces are inferred from class definitions. They may alsobe defined directly and used to restrict the type of a class. Like classdeclarations, they also define a new type abbreviation.

#classtype restricted_point_type =objectmethod get_x : intmethod bump : unitend;;
classtype restricted_point_type =objectmethod bump : unitmethod get_x : intend
#fun (x : restricted_point_type) -> x;;
- : restricted_point_type -> restricted_point_type = <fun>

In addition to program documentation, class interfaces can be used toconstrain the type of a class. Both concrete instance variables and concreteprivate methods can be hidden by a class type constraint. Publicmethods and virtual members, however, cannot.

#class restricted_point' x = (restricted_point x : restricted_point_type);;
class restricted_point' : int -> restricted_point_type

Or, equivalently:

#class restricted_point' = (restricted_point : int -> restricted_point_type);;
class restricted_point' : int -> restricted_point_type

The interface of a class can also be specified in a modulesignature, and used to restrict the inferred signature of a module.

#moduletype POINT =sigclass restricted_point' : int ->objectmethod get_x : intmethod bump : unitendend;;
moduletype POINT =sigclass restricted_point' : int ->objectmethod bump : unitmethod get_x : intendend
#module Point : POINT =structclass restricted_point' = restricted_pointend;;
module Point : POINT

8 Inheritance

We illustrate inheritance by defining a class of colored points thatinherits from the class of points. This class has all instancevariables and all methods of classpoint, plus a new instancevariablec and a new methodcolor.

#class colored_point x (c : string) =objectinherit point xval c = cmethod color = cend;;
class colored_point : int -> string ->objectval c : stringvalmutable x : intmethod color : stringmethod get_offset : intmethod get_x : intmethod move : int -> unitend
#let p' =new colored_point 5"red";;
val p' : colored_point = <obj>
# p'#get_x, p'#color;;
- : int * string = (5,"red")

A point and a colored point have incompatible types, since a point hasno methodcolor. However, the functionget_succ_x below is a genericfunction applying methodget_x to any objectp that has thismethod (and possibly some others, which are represented by an ellipsisin the type). Thus, it applies to both points and colored points.

#let get_succ_x p = p#get_x + 1;;
val get_succ_x : < get_x : int; .. > -> int = <fun>
# get_succ_x p + get_succ_x p';;
- : int = 8

Methods need not be declared previously, as shown by the example:

#let set_x p = p#set_x;;
val set_x : < set_x : 'a; .. > -> 'a = <fun>
#let incr p = set_x p (get_succ_x p);;
val incr : < get_x : int; set_x : int -> 'a; .. > -> 'a = <fun>

9 Multiple inheritance

Multiple inheritance is allowed. Only the last definition of a methodis kept: the redefinition in a subclass of a method that was visible inthe parent class overrides the definition in the parent class.Previous definitions of a method can be reused by binding the relatedancestor. Below,super is bound to the ancestorprintable_point.The namesuper is a pseudo value identifier that can only be used toinvoke a super-class method, as insuper#print.

#class printable_colored_point y c =object (self)val c = cmethod color = cinherit printable_point yas supermethod! print = print_string"("; super#print; print_string", "; print_string (self#color); print_string")"end;;
class printable_colored_point : int -> string ->objectval c : stringvalmutable x : intmethod color : stringmethod get_x : intmethod move : int -> unitmethod print : unitend
#let p' =new printable_colored_point 17"red";;
new point at (10, red)val p' : printable_colored_point = <obj>
# p'#print;;
(10, red)- : unit = ()

A private method that has been hidden in the parent class is no longervisible, and is thus not overridden. Since initializers are treated asprivate methods, all initializers along the class hierarchy are evaluated,in the order they are introduced.

Note that for clarity’s sake, the methodprint is explicitly marked asoverriding another definition by annotating themethod keyword withan exclamation mark!. If the methodprint were not overriding theprint method ofprintable_point, the compiler would raise an error:

#objectmethod! m = ()end;;
Error: The method m has no previous definition

This explicit overriding annotation also worksforval andinherit:

#class another_printable_colored_point y c c' =object (self)inherit printable_point yinherit! printable_colored_point y cval! c = c'end;;
class another_printable_colored_point : int -> string -> string ->objectval c : stringvalmutable x : intmethod color : stringmethod get_x : intmethod move : int -> unitmethod print : unitend

10 Parameterized classes

Reference cells can be implemented as objects.The naive definition fails to typecheck:

#class oref x_init = object val mutable x = x_init method get = x method set y = x <- y end;;
Error: Some type variables are unbound in this type: class oref : 'a -> object val mutable x : 'a method get : 'a method set : 'a -> unit end The method get has type 'a where 'a is unbound

The reason is that at least one of the methods has a polymorphic type(here, the type of the value stored in the reference cell), thuseither the class should be parametric, or the method type should beconstrained to a monomorphic type. A monomorphic instance of the class couldbe defined by:

#class oref (x_init:int) =objectvalmutable x = x_initmethod get = xmethod set y = x <- yend;;
class oref : int ->objectvalmutable x : intmethod get : intmethod set : int -> unitend

Note that since immediate objects do not define a class type, they haveno such restriction.

#let new_oref x_init =objectvalmutable x = x_initmethod get = xmethod set y = x <- yend;;
val new_oref : 'a -> < get : 'a; set : 'a -> unit > = <fun>

On the other hand, a class for polymorphic references must explicitlylist the type parameters in its declaration. Class type parameters arelisted between[ and]. The type parameters must also bebound somewhere in the class body by a type constraint.

#class ['a] oref x_init =objectvalmutable x = (x_init : 'a)method get = xmethod set y = x <- yend;;
class ['a] oref : 'a ->objectvalmutable x : 'amethod get : 'amethod set : 'a -> unitend
#let r =new oref 1in r#set 2; (r#get);;
- : int = 2

The type parameter in the declaration may actually be constrained in thebody of the class definition. In the class type, the actual value ofthe type parameter is displayed in theconstraint clause.

#class ['a] oref_succ (x_init:'a) =objectvalmutable x = x_init + 1method get = xmethod set y = x <- yend;;
class ['a] oref_succ : 'a ->objectconstraint 'a = intvalmutable x : intmethod get : intmethod set : int -> unitend

Let us consider a more complex example: define a circle, whose centermay be any kind of point. We put an additional typeconstraint in methodmove, since no free variables must remainunaccounted for by the class type parameters.

#class ['a] circle (c : 'a) =objectvalmutable center = cmethod center = centermethod set_center c = center <- cmethod move = (center#move : int -> unit)end;;
class ['a] circle : 'a ->objectconstraint 'a = < move : int -> unit; .. >valmutable center : 'amethod center : 'amethod move : int -> unitmethod set_center : 'a -> unitend

An alternate definition ofcircle, using aconstraint clause inthe class definition, is shown below. The type#point used below intheconstraint clause is an abbreviation produced by the definitionof classpoint. This abbreviation unifies with the type of anyobject belonging to a subclass of classpoint. It actually expands to< get_x : int; move : int -> unit; .. >. This leads to the followingalternate definition ofcircle, which has slightly strongerconstraints on its argument, as we now expectcenter to have amethodget_x.

#class ['a] circle (c : 'a) =objectconstraint 'a = #pointvalmutable center = cmethod center = centermethod set_center c = center <- cmethod move = center#moveend;;
class ['a] circle : 'a ->objectconstraint 'a = #pointvalmutable center : 'amethod center : 'amethod move : int -> unitmethod set_center : 'a -> unitend

The classcolored_circle is a specialized version of classcircle that requires the type of the center to unify with#colored_point, and adds a methodcolor. Note that when specializing aparameterized class, the instance of type parameter must always beexplicitly given. It is again written between[ and].

#class ['a] colored_circle c =objectconstraint 'a = #colored_pointinherit ['a] circle cmethod color = center#colorend;;
class ['a] colored_circle : 'a ->objectconstraint 'a = #colored_pointvalmutable center : 'amethod center : 'amethod color : stringmethod move : int -> unitmethod set_center : 'a -> unitend

11 Polymorphic methods

While parameterized classes may be polymorphic in their contents, theyare not enough to allow polymorphism of method use.

A classical example is defining an iterator.

# List.fold_left;;
- : ('acc -> 'a -> 'acc) -> 'acc -> 'a list -> 'acc = <fun>
#class ['a] intlist (l : int list) =objectmethod empty = (l = [])method fold f (accu : 'a) = List.fold_left f accu lend;;
class ['a] intlist : int list ->objectmethod empty : boolmethod fold : ('a -> int -> 'a) -> 'a -> 'aend

At first look, we seem to have a polymorphic iterator, however thisdoes not work in practice.

#let l =new intlist [1; 2; 3];;
val l : '_weak2 intlist = <obj>
# l#fold (fun x y -> x+y) 0;;
- : int = 6
# l;;
- : int intlist = <obj>
# l#fold (fun s x ->s ^ Int.to_string x ^" ")"" ;;
Error: The value s has type int but an expression was expected of type string

Our iterator works, as shows its first use for summation. However,since objects themselves are not polymorphic (only their constructorsare), using thefold method fixes its type for this individual object.Our next attempt to use it as a string iterator fails.

The problem here is that quantification was wrongly located: it isnot the class we want to be polymorphic, but thefold method.This can be achieved by giving an explicitly polymorphic type in themethod definition.

#class intlist (l : int list) =objectmethod empty = (l = [])method fold : 'a. ('a -> int -> 'a) -> 'a -> 'a =fun f accu -> List.fold_left f accu lend;;
class intlist : int list ->objectmethod empty : boolmethod fold : ('a -> int -> 'a) -> 'a -> 'aend
#let l =new intlist [1; 2; 3];;
val l : intlist = <obj>
# l#fold (fun x y -> x+y) 0;;
- : int = 6
# l#fold (fun s x -> s ^ Int.to_string x ^" ")"";;
- : string ="1 2 3 "

As you can see in the class type shown by the compiler, whilepolymorphic method types must be fully explicit in class definitions(appearing immediately after the method name), quantified typevariables can be left implicit in class descriptions. Why require typesto be explicit? The problem is that(int -> int -> int) -> int -> int would also be a valid type forfold, and it happens to beincompatible with the polymorphic type we gave (automaticinstantiation only works for toplevel types variables, not for innerquantifiers, where it becomes an undecidable problem.) So the compilercannot choose between those two types, and must be helped.

However, the type can be completely omitted in the class definition ifit is already known, through inheritance or type constraints on self.Here is an example of method overriding.

#class intlist_rev l =objectinherit intlist lmethod! fold f accu = List.fold_left f accu (List.rev l)end;;

The following idiom separates description and definition.

#classtype ['a] iterator =objectmethod fold : ('b -> 'a -> 'b) -> 'b -> 'bend;;
#class intlist' l =object (self : int #iterator)method empty = (l = [])method fold f accu = List.fold_left f accu lend;;

Note here the(self : int #iterator) idiom, which ensures that thisobject implements the interfaceiterator.

Polymorphic methods are called in exactly the same way as normalmethods, but you should be aware of some limitations of typeinference. Namely, a polymorphic method can only be called if itstype is known at the call site. Otherwise, the method will be assumedto be monomorphic, and given an incompatible type.

#let sum lst = lst#fold (fun x y -> x+y) 0;;
val sum : < fold : (int -> int -> int) -> int -> 'a; .. > -> 'a = <fun>
# suml ;;
Error: The value l has type intlist = < empty : bool; fold : 'a. ('a -> int -> 'a) -> 'a -> 'a > but an expression was expected of type < fold : (int -> int -> int) -> int -> 'b; .. > The method fold has type 'a. ('a -> int -> 'a) -> 'a -> 'a, but the expected method type was (int -> int -> int) -> int -> 'b

The workaround is easy: you should put a type constraint on theparameter.

#let sum (lst : _ #iterator) = lst#fold (fun x y -> x+y) 0;;
val sum : int #iterator -> int = <fun>

Of course the constraint may also be an explicit method type.Only occurrences of quantified variables are required.

#let sum lst = (lst : < fold : 'a. ('a -> _ -> 'a) -> 'a -> 'a; .. >)#fold (+) 0;;
val sum : < fold : 'a. ('a -> int -> 'a) -> 'a -> 'a; .. > -> int = <fun>

Another use of polymorphic methods is to allow some form of implicitsubtyping in method arguments. We have already seen insection 3.8 how some functions may be polymorphic in theclass of their argument. This can be extended to methods.

#classtype point0 =objectmethod get_x : intend;;
classtype point0 =objectmethod get_x : intend
#class distance_point x =objectinherit point xmethod distance : 'a. (#point0as 'a) -> int =fun other -> abs (other#get_x - x)end;;
class distance_point : int ->objectvalmutable x : intmethod distance : #point0 -> intmethod get_offset : intmethod get_x : intmethod move : int -> unitend
#let p =new distance_point 3in (p#distance (new point 8), p#distance (new colored_point 1"blue"));;
- : int * int = (5, 2)

Note here the special syntax(#point0 as 'a) we have to use toquantify the extensible part of#point0. As for the variable binder,it can be omitted in class specifications. If you want polymorphisminside object field it must be quantified independently.

#class multi_poly =objectmethod m1 : 'a. (< n1 : 'b. 'b -> 'b; .. >as 'a) -> _ =fun o -> o#n1true, o#n1"hello"method m2 : 'a 'b. (< n2 : 'b -> bool; .. >as 'a) -> 'b -> _ =fun o x -> o#n2 xend;;
class multi_poly :objectmethod m1 : < n1 : 'b. 'b -> 'b; .. > -> bool * stringmethod m2 : < n2 : 'b -> bool; .. > -> 'b -> boolend

In methodm1,o must be an object with at least a methodn1,itself polymorphic. In methodm2, the argument ofn2 andx musthave the same type, which is quantified at the same level as'a.

12 Using coercions

Subtyping is never implicit. There are, however, two ways to performsubtyping. The most general construction is fully explicit: both thedomain and the codomain of the type coercion must be given.

We have seen that points and colored points have incompatible types.For instance, they cannot be mixed in the same list. However, acolored point can be coerced to a point, hiding itscolor method:

#let colored_point_to_point cp = (cp : colored_point :> point);;
val colored_point_to_point : colored_point -> point = <fun>
#let p =new point 3and q =new colored_point 4"blue";;
val p : point = <obj>val q : colored_point = <obj>
#let l = [p; (colored_point_to_point q)];;
val l : point list = [<obj>; <obj>]

An object of typet can be seen as an object of typet'only ift is a subtype oft'. For instance, a point cannot beseen as a colored point.

#(p : point :> colored_point);;
Error: Type point = < get_offset : int; get_x : int; move : int -> unit > is not a subtype of colored_point = < color : string; get_offset : int; get_x : int; move : int -> unit > The first object type has no method color

Indeed, narrowing coercions without runtime checks would be unsafe.Runtime type checks might raise exceptions, and they would requirethe presence of type information at runtime, which is not the case inthe OCaml system.For these reasons, there is no such operation available in the language.

Be aware that subtyping and inheritance are not related. Inheritance is asyntactic relation between classes while subtyping is a semantic relationbetween types. For instance, the class of colored points could have beendefined directly, without inheriting from the class of points; the type ofcolored points would remain unchanged and thus still be a subtype ofpoints.

The domain of a coercion can often be omitted. For instance, one candefine:

#let to_point cp = (cp :> point);;
val to_point : #point -> point = <fun>

In this case, the functioncolored_point_to_point is an instance of thefunctionto_point. This is not always true, however. The fullyexplicit coercion is more precise and is sometimes unavoidable.Consider, for example, the following class:

#class c0 =objectmethod m = {< >}method n = 0end;;
class c0 :object ('a)method m : 'amethod n : intend

The object typec0 is an abbreviation for<m : 'a; n : int> as 'a.Consider now the type declaration:

#classtype c1 =objectmethod m : c1end;;
classtype c1 =objectmethod m : c1end

The object typec1 is an abbreviation for the type<m : 'a> as 'a.The coercion from an object of typec0 to an object of typec1 iscorrect:

#fun (x:c0) -> (x : c0 :> c1);;
- : c0 -> c1 = <fun>

However, the domain of the coercion cannot always be omitted.In that case, the solution is to use the explicit form.Sometimes, a change in the class-type definition can also solve the problem

#classtype c2 =object ('a)method m : 'aend;;
classtype c2 =object ('a)method m : 'aend
#fun (x:c0) -> (x :> c2);;
- : c0 -> c2 = <fun>

While class typesc1 andc2 are different, both object typesc1 andc2 expand to the same object type (same method names and types).Yet, when the domain of a coercion is left implicit and its co-domainis an abbreviation of a known class type, then the class type, ratherthan the object type, is used to derive the coercion function. Thisallows leaving the domain implicit in most cases when coercing from asubclass to its superclass.The type of a coercion can always be seen as below:

#let to_c1 x = (x :> c1);;
val to_c1 : < m : #c1; .. > -> c1 = <fun>
#let to_c2 x = (x :> c2);;
val to_c2 : #c2 -> c2 = <fun>

Note the difference between these two coercions: in the case ofto_c2,the type#c2 = < m : 'a; .. > as 'a is polymorphically recursive (accordingto the explicit recursion in the class type ofc2); hence thesuccess of applying this coercion to an object of classc0.On the other hand, in the first case,c1 was only expanded andunrolled twice to obtain< m : < m : c1; .. >; .. > (remember#c1 = < m : c1; .. >), without introducing recursion.You may also note that the type ofto_c2 is#c2 -> c2 whilethe type ofto_c1 is more general than#c1 -> c1. This is not always true,since there are class types for which some instances of#c are not subtypesofc, as explained in section 3.16. Yet, forparameterless classes the coercion(_ :> c) is always more general than(_ : #c :> c).

A common problem may occur when one tries to define a coercion to aclassc while defining classc. The problem is due to the typeabbreviation not being completely defined yet, and so its subtypes are notclearly known. Then, a coercion(_ :> c) or(_ : #c :> c) is taken to bethe identity function, as in

#fun x -> (x :> 'a);;
- : 'a -> 'a = <fun>

As a consequence, if the coercion is applied toself, as in thefollowing example, the type ofself is unified with the closed typec (a closed object type is an object type without ellipsis). Thiswould constrain the type of self be closed and is thus rejected.Indeed, the type of self cannot be closed: this would prevent anyfurther extension of the class. Therefore, a type error is generatedwhen the unification of this type with another type would result in aclosed object type.

#class c =objectmethod m = 1endand d =object (self)inherit cmethod n = 2method as_c = (self :> c)end;;
Error: This expression cannot be coerced to type c = < m : int >; it has type < as_c : c; m : int; n : int; .. > but is here used with type c Self type cannot escape its class

However, the most common instance of this problem, coercing self toits current class, is detected as a special case by the type checker,and properly typed.

#class c =object (self)method m = (self :> c)end;;
class c :objectmethod m : cend

This allows the following idiom, keeping a list of all objectsbelonging to a class or its subclasses:

#let all_c =ref [];;
val all_c : '_weak3 listref = {contents = []}
#class c (m : int) =object (self)method m = minitializer all_c := (self :> c) :: !all_cend;;
class c : int ->objectmethod m : intend

This idiom can in turn be used to retrieve an object whose type hasbeen weakened:

#letrec lookup_obj obj =function [] -> raise Not_found | obj' :: l ->if (obj :> < >) = (obj' :> < >)then obj'else lookup_obj obj l ;;
val lookup_obj : < .. > -> (< .. >as 'a) list -> 'a = <fun>
#let lookup_c obj = lookup_obj obj !all_c;;
val lookup_c : < .. > -> < m : int > = <fun>

The type< m : int > we see here is just the expansion ofc, dueto the use of a reference; we have succeeded in getting back an objectof typec.


The previous coercion problem can often be avoided by firstdefining the abbreviation, using a class type:

#classtype c' =objectmethod m : intend;;
classtype c' =objectmethod m : intend
#class c : c' =objectmethod m = 1endand d =object (self)inherit cmethod n = 2method as_c = (self :> c')end;;
class c : c'and d :objectmethod as_c : c'method m : intmethod n : intend

It is also possible to use a virtual class. Inheriting from this classsimultaneously forces all methods ofc to have the sametype as the methods ofc'.

#classvirtual c' =objectmethodvirtual m : intend;;
classvirtual c' :objectmethodvirtual m : intend
#class c =object (self)inherit c'method m = 1end;;
class c :objectmethod m : intend

One could think of defining the type abbreviation directly:

#type c' = <m : int>;;

However, the abbreviation#c' cannot be defined directly in a similar way.It can only be defined by a class or a class-type definition.This is because a#-abbreviation carries an implicit anonymousvariable.. that cannot be explicitly named.The closer you get to it is:

#type 'a c'_class = 'aconstraint 'a = < m : int; .. >;;

with an extra type variable capturing the open object type.

13 Functional objects

It is possible to write a version of classpoint without assignmentson the instance variables. The override construct{< ... >} returns a copy of“self” (that is, the current object), possibly changing the value ofsome instance variables.

#class functional_point y =objectval x = ymethod get_x = xmethod move d = {< x = x + d >}method move_to x = {< x >}end;;
class functional_point : int ->object ('a)val x : intmethod get_x : intmethod move : int -> 'amethod move_to : int -> 'aend
#let p =new functional_point 7;;
val p : functional_point = <obj>
# p#get_x;;
- : int = 7
# (p#move 3)#get_x;;
- : int = 10
# (p#move_to 15)#get_x;;
- : int = 15
# p#get_x;;
- : int = 7

As with records, the form{< x >} is an elided version of{< x = x >} which avoids the repetition of the instance variable name.Note that the type abbreviationfunctional_point is recursive, which canbe seen in the class type offunctional_point: the type of self is'aand'a appears inside the type of the methodmove.

The above definition offunctional_point is not equivalentto the following:

#class bad_functional_point y =objectval x = ymethod get_x = xmethod move d =new bad_functional_point (x+d)method move_to x =new bad_functional_point xend;;
class bad_functional_point : int ->objectval x : intmethod get_x : intmethod move : int -> bad_functional_pointmethod move_to : int -> bad_functional_pointend

While objects of either class will behave the same, objects of theirsubclasses will be different. In a subclass ofbad_functional_point,the methodmove willkeep returning an object of the parent class. On the contrary, in asubclass offunctional_point, the methodmove will return anobject of the subclass.

Functional update is often used in conjunction with binary methodsas illustrated in section 8.2.1.

14 Cloning objects

Objects can also be cloned, whether they are functional or imperative.The library functionOo.copy makes a shallow copy of an object. That is,it returns a new object that has the same methods and instancevariables as its argument. Theinstance variables are copied but their contents are shared.Assigning a new value to an instance variable of the copy (using a methodcall) will not affect instance variables of the original, and conversely.A deeper assignment (for example if the instance variable is a reference cell)will of course affect both the original and the copy.

The type ofOo.copy is the following:

# Oo.copy;;
- : (< .. >as 'a) -> 'a = <fun>

The keywordas in that type binds the type variable'a tothe object type< .. >. Therefore,Oo.copy takes an object withany methods (represented by the ellipsis), and returns an object ofthe same type. The type ofOo.copy is different from type< .. > -> < .. > as each ellipsis represents a different set of methods.Ellipsis actually behaves as a type variable.

#let p =new point 5;;
val p : point = <obj>
#let q = Oo.copy p;;
val q : point = <obj>
# q#move 7; (p#get_x, q#get_x);;
- : int * int = (5, 12)

In fact,Oo.copy p will behave asp#copy assuming that a publicmethodcopy with body{< >} has been defined in the class ofp.

Objects can be compared using the generic comparison functions= and<>.Two objects are equal if and only if they are physically equal. Inparticular, an object and its copy are not equal.

#let q = Oo.copy p;;
val q : point = <obj>
# p = q, p = p;;
- : bool * bool = (false,true)

Other generic comparisons such as (<,<=, ...) can also be used onobjects. Therelation< defines an unspecified but strict ordering on objects. Theordering relationship between two objects is fixed permanently once thetwo objects have been created, and it is not affected by mutation of fields.

Cloning and override have a non empty intersection.They are interchangeable when used within an object and withoutoverriding any field:

#class copy =objectmethod copy = {< >}end;;
class copy :object ('a)method copy : 'aend
#class copy =object (self)method copy = Oo.copy selfend;;
class copy :object ('a)method copy : 'aend

Only the override can be used to actually override fields, andonly theOo.copy primitive can be used externally.

Cloning can also be used to provide facilities for saving andrestoring the state of objects.

#class backup =object (self : 'mytype)valmutable copy = Nonemethod save = copy <- Some {< copy = None >}method restore =match copywith Some x -> x | None -> selfend;;
class backup :object ('a)valmutable copy : 'a optionmethod restore : 'amethod save : unitend

The above definition will only backup one level.The backup facility can be added to any class by using multiple inheritance.

#class ['a] backup_ref x =objectinherit ['a] oref xinherit backupend;;
class ['a] backup_ref : 'a ->object ('b)valmutable copy : 'b optionvalmutable x : 'amethod get : 'amethod restore : 'bmethod save : unitmethod set : 'a -> unitend
#letrec get p n =if n = 0then p # getelse get (p # restore) (n-1);;
val get : (< get : 'b; restore : 'a; .. >as 'a) -> int -> 'b = <fun>
#let p =new backup_ref 0in p # save; p # set 1; p # save; p # set 2; [get p 0; get p 1; get p 2; get p 3; get p 4];;
- : int list = [2; 1; 1; 1; 1]

We can define a variant of backup that retains all copies. (We alsoadd a methodclear to manually erase all copies.)

#class backup =object (self : 'mytype)valmutable copy = Nonemethod save = copy <- Some {< >}method restore =match copywith Some x -> x | None -> selfmethod clear = copy <- Noneend;;
class backup :object ('a)valmutable copy : 'a optionmethod clear : unitmethod restore : 'amethod save : unitend
#class ['a] backup_ref x =objectinherit ['a] oref xinherit backupend;;
class ['a] backup_ref : 'a ->object ('b)valmutable copy : 'b optionvalmutable x : 'amethod clear : unitmethod get : 'amethod restore : 'bmethod save : unitmethod set : 'a -> unitend
#let p =new backup_ref 0in p # save; p # set 1; p # save; p # set 2; [get p 0; get p 1; get p 2; get p 3; get p 4];;
- : int list = [2; 1; 0; 0; 0]

15 Recursive classes

Recursive classes can be used to define objects whose types aremutually recursive.

#class window =objectvalmutable top_widget = (None : widget option)method top_widget = top_widgetendand widget (w : window) =objectval window = wmethod window = windowend;;
class window :objectvalmutable top_widget : widget optionmethod top_widget : widget optionendand widget : window ->objectval window : windowmethod window : windowend

Although their types are mutually recursive, the classeswidget andwindow are themselves independent.

16 Binary methods

A binary method is a method which takes an argument of the same typeas self. The classcomparable below is a template for classes with abinary methodleq of type'a -> bool where the type variable'ais bound to the type of self. Therefore,#comparable expands to< leq : 'a -> bool; .. > as 'a. We see here that the binderas alsoallows writing recursive types.

#classvirtual comparable =object (_ : 'a)methodvirtual leq : 'a -> boolend;;
classvirtual comparable :object ('a)methodvirtual leq : 'a -> boolend

We then define a subclassmoney ofcomparable. The classmoneysimply wraps floats as comparable objects.1 We will extendmoney below with more operations. We have to use a type constraint onthe class parameterx because the primitive<= is a polymorphicfunction in OCaml. Theinherit clause ensures that the type ofobjects of this class is an instance of#comparable.

#class money (x : float) =objectinherit comparableval repr = xmethod value = reprmethod leq p = repr <= p#valueend;;
class money : float ->object ('a)val repr : floatmethod leq : 'a -> boolmethod value : floatend

Note that the typemoney is not a subtype of typecomparable, as the self type appears in contravariant positionin the type of methodleq.Indeed, an objectm of classmoney has a methodleqthat expects an argument of typemoney since it accessesitsvalue method. Consideringm of typecomparable would allow acall to methodleq onm with an argument that does not have a methodvalue, which would be an error.

Similarly, the typemoney2 below is not a subtype of typemoney.

#class money2 x =objectinherit money xmethod times k = {< repr = k *. repr >}end;;
class money2 : float ->object ('a)val repr : floatmethod leq : 'a -> boolmethod times : float -> 'amethod value : floatend

It is however possible to define functions that manipulate objects oftype eithermoney ormoney2: the functionminwill return the minimum of any two objects whose type unifies with#comparable. The type ofmin is not the same as#comparable -> #comparable -> #comparable, as the abbreviation#comparable hides atype variable (an ellipsis). Each occurrence of this abbreviationgenerates a new variable.

#let min (x : #comparable) y =if x#leq ythen xelse y;;
val min : (#comparableas 'a) -> 'a -> 'a = <fun>

This function can be applied to objects of typemoneyormoney2.

# (min (new money 1.3) (new money 3.1))#value;;
- : float = 1.3
# (min (new money2 5.0) (new money2 3.14))#value;;
- : float = 3.14

More examples of binary methods can be found insections 8.2.1 and 8.2.3.

Note the use of override for methodtimes.Writingnew money2 (k *. repr) instead of{< repr = k *. repr >}would not behave well with inheritance: in a subclassmoney3 ofmoney2thetimes method would return an object of classmoney2 but not of classmoney3 as would be expected.

The classmoney could naturally carry another binary method. Here is adirect definition:

#class money x =object (self : 'a)val repr = xmethod value = reprmethod print = print_float reprmethod times k = {< repr = k *. x >}method leq (p : 'a) = repr <= p#valuemethod plus (p : 'a) = {< repr = x +. p#value >}end;;
class money : float ->object ('a)val repr : floatmethod leq : 'a -> boolmethod plus : 'a -> 'amethod print : unitmethod times : float -> 'amethod value : floatend

17 Friends

The above classmoney reveals a problem that often occurs with binarymethods. In order to interact with other objects of the same class, therepresentation ofmoney objects must be revealed, using a method such asvalue. If we remove all binary methods (hereplus andleq),the representation can easily be hidden inside objects by removing the methodvalue as well. However, this is not possible as soon as some binarymethod requires access to the representation of objects of the sameclass (other than self).

#class safe_money x =object (self : 'a)val repr = xmethod print = print_float reprmethod times k = {< repr = k *. x >}end;;
class safe_money : float ->object ('a)val repr : floatmethod print : unitmethod times : float -> 'aend

Here, the representation of the object is known only to a particular object.To make it available to other objects of the same class, we are forced tomake it available to the whole world. However we can easily restrict thevisibility of the representation using the module system.

#moduletype MONEY =sigtype tclass c : float ->object ('a)val repr : tmethod value : tmethod print : unitmethod times : float -> 'amethod leq : 'a -> boolmethod plus : 'a -> 'aendend;;
#module Euro : MONEY =structtype t = floatclass c x =object (self : 'a)val repr = xmethod value = reprmethod print = print_float reprmethod times k = {< repr = k *. x >}method leq (p : 'a) = repr <= p#valuemethod plus (p : 'a) = {< repr = x +. p#value >}endend;;

Another example of friend functions may be found in section 8.2.3.These examples occur when a group of objects (hereobjects of the same class) and functions should see each others internalrepresentation, while their representation should be hidden from theoutside. The solution is always to define all friends in the same module,give access to the representation and use a signature constraint to make therepresentation abstract outside the module.


1
floats are anapproximation of decimal numbers, they are unsuitable for use in mostmonetary calculations as they may introduce errors.

« The module systemLabeled arguments »
(Chapter written by Jérôme Vouillon, Didier Rémy and Jacques Garrigue)
Copyright © 2025 Institut National deRecherche en Informatique et en Automatique

[8]ページ先頭

©2009-2026 Movatter.jp