20.03.2015 Views

The Objective Caml system release 3.06 - The Caml language - Inria

The Objective Caml system release 3.06 - The Caml language - Inria

The Objective Caml system release 3.06 - The Caml language - Inria

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

40<br />

3.6 Class interfaces<br />

Class interfaces are inferred from class definitions. <strong>The</strong>y may also be defined directly and used to<br />

restrict the type of a class. Like class declarations, they also define a new type abbreviation.<br />

# class type restricted_point_type =<br />

# object<br />

# method get_x : int<br />

# method bump : unit<br />

# end;;<br />

class type restricted_point_type =<br />

object method bump : unit method get_x : int end<br />

# fun (x : restricted_point_type) -> x;;<br />

- : restricted_point_type -> restricted_point_type = <br />

In addition to program documentation, class interfaces can be used to constrain the type of a class.<br />

Both instance variables and concrete private methods can be hidden by a class type constraint.<br />

Public and virtual methods, however, cannot.<br />

# class restricted_point’ x = (restricted_point x : restricted_point_type);;<br />

class restricted_point’ : int -> restricted_point_type<br />

Or, equivalently:<br />

# class restricted_point’ = (restricted_point : int -> restricted_point_type);;<br />

class restricted_point’ : int -> restricted_point_type<br />

<strong>The</strong> interface of a class can also be specified in a module signature, and used to restrict the inferred<br />

signature of a module.<br />

# module type POINT = sig<br />

# class restricted_point’ : int -><br />

# object<br />

# method get_x : int<br />

# method bump : unit<br />

# end<br />

# end;;<br />

module type POINT =<br />

sig<br />

class restricted_point’ :<br />

int -> object method bump : unit method get_x : int end<br />

end<br />

# module Point : POINT = struct<br />

# class restricted_point’ = restricted_point<br />

# end;;<br />

module Point : POINT

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!