From: Martin Jambon <martin.jambon@ens-lyon.org>
To: Jacques Le Normand <rathereasy@gmail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] type error
Date: Sun, 23 Mar 2008 23:19:45 +0100 (CET) [thread overview]
Message-ID: <Pine.LNX.4.64.0803232304450.15117@martin.ec.wink.com> (raw)
In-Reply-To: <f74178430803231501x31a22497w89271b3d2f8a5c4@mail.gmail.com>
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
next prev parent reply other threads:[~2008-03-23 22:20 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-03-23 22:01 Jacques Le Normand
2008-03-23 22:19 ` Martin Jambon [this message]
2008-03-24 8:53 ` Remi Vanicat
-- strict thread matches above, loose matches on Subject: below --
2008-04-01 2:27 Jacques Le Normand
2008-04-01 2:42 ` [Caml-list] " Jacques Garrigue
2006-10-15 3:29 Type error Denis Bueno
2006-10-15 3:40 ` [Caml-list] " Jonathan Roewen
2006-10-15 3:48 ` skaller
2006-10-15 3:54 ` Jonathan Roewen
2006-10-15 11:26 ` Etienne Miret
2006-10-15 23:37 ` Jacques Garrigue
2005-04-02 1:29 Jon Harrop
2005-04-02 7:50 ` [Caml-list] " Jacques Garrigue
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Pine.LNX.4.64.0803232304450.15117@martin.ec.wink.com \
--to=martin.jambon@ens-lyon.org \
--cc=caml-list@yquem.inria.fr \
--cc=rathereasy@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox