[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Martin Jambon <martin.jambon@e...> |
| Subject: | Re: [Caml-list] type error |
On Sun, 23 Mar 2008, Jacques Le Normand wrote:
> Hello caml-list,
> I don't quite understand what the error is here:
>
> type foo = <bar:int;..>
>
> error:
>
> A type variable is unbound in this type declaration.
> In definition < bar : int; .. > the variable 'a is unbound
Basically ".." means "any number of methods of any kind", which needs to
be represented by a type variable.
You can do something like this, which is probably not very useful:
# type 'a foo = < bar: int; .. > as 'a;;
type 'a foo = 'a constraint 'a = < bar : int; .. >
A more idiomatic way is to declare the following type:
type foo = < bar: int >
And when you need an object to behave as having type foo, you use ":>" to
hide the extra methods.
# type foo = < bar: int >;;
type foo = < bar : int >
# let print_bar (x : foo) = print_int x # bar;;
val print_bar : foo -> unit = <fun>
# let x =
(object
method bar = 123
method hello () = print_endline "hello"
end);;
val x : < bar : int; hello : unit -> unit > = <obj>
# print_bar x;;
This expression has type < bar : int; hello : unit -> unit >
but is here used with type foo
Only the first object type has a method hello
# print_bar (x :> foo);;
123- : unit = ()
Martin
--
http://wink.com/profile/mjambon
http://martin.jambon.free.fr