This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

Subtype problem
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: -- (:) From: Hendrik Tews Subject: Subtype problem
```Hi,

could somebody tell me, why

-------------------------Version 1---------------------------
class point x =
val mutable x = (x : int)
method x = x
method move (i : int) =  {< x = x + i >}
end;;

class colored_point x c =
val mutable x = (x : int)
val color = (c : int)
method x = x
method move (i : int) = {< x = x + i >}
method color = color
end;;

let p = ((new colored_point 2 1 :> point) : point)
-------------------------Version 1---------------------------

compiles without problem, while

-------------------------Version 2---------------------------
class point x =
val mutable x = (x : int)
method x = x
method move (i : int) =  Some {< x = x + i >}
end;;

class colored_point x c =
val mutable x = (x : int)
val color = (c : int)
method x = x
method move (i : int) = Some {< x = x + i >}
method color = color
end;;

let p = ((new colored_point 2 1 :> point) : point)
-------------------------Version 2---------------------------

produces an error? (The only difference is the move method, which
delivers an option in version 2). The error message is

-------------------------Error-------------------------------
This expression cannot be coerced to type
point = < x : int; move : int -> point option >;
it has type
colored_point =
< x : int; move : int -> colored_point option; color : int >
but is here used with type
< x : int; move : int -> point option; color : int >
Type
colored_point =
< x : int; move : int -> colored_point option; color : int >
is not compatible with type point = < x : int; move : int -> point option >
-------------------------Error-------------------------------

Bye,

Hendrik

```