Version française
Home     About     Download     Resources     Contact us    
Browse thread
type error
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Remi Vanicat <vanicat@d...>
Subject: Re: type error
Martin Jambon <martin.jambon@ens-lyon.org> writes:

> A more idiomatic way is to declare the following type:
>
> type foo = < bar: int >

An even more idiomatic way to do it is by using type class:

class type foo = object method bar : int end;;

and then you can use #foo that will be the type you wanted, Martin's
example become:

# class type foo = object method bar : int end;;
class type foo = object method bar : int end
# 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;;
123- : unit = ()



-- 
Rémi Vanicat