From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Subtyping
Date: Wed, 08 Apr 2009 02:38:49 +0200 [thread overview]
Message-ID: <87bpr86kti.fsf@frosties.localdomain> (raw)
In-Reply-To: <873aclf1z8.fsf@frosties.localdomain> (Goswin von Brederlow's message of "Tue, 07 Apr 2009 07:48:43 +0200")
Goswin von Brederlow <goswin-v-b@web.de> writes:
> So what other ways are there of doing this? Records. Idealy I would
> like to do this:
>
> type base = { x : int }
> let make_base x = { x = x }
> let print_x r = print_int r.x
> type foo = { base with y : int }
> let make_foo x y = { x = x; y = y }
> let _ =
> print_x (make_base 0); print_newline ();
> print_x (make_foo 0 1); print_newline ();;
>
> Ocaml has no way of making this work like that, right?
Small extra question concerning this. Can I get ocaml to recognise a
type like this?
type base = 'a. {
x : 'a;
fn : 'a -> unit;
}
List.iter
(fun r -> r.fn r)
[{x = 1; fn = (fun r -> print_int r.x); };
{x = 1.2; fn = (fun r -> print_float r.x); }]
The difference to a "'a base" type would be that the 'a is only
infered and fixed inside the record but remains abstract outside of
it.
For that reason this would not be allowed:
let cross_call x y = x.fn y
The 'a would have to escape the record which wouldn't be allowed.
So is there any syntax or trick to get the ocaml typesystem to
construct such a type?
MfG
Goswin
next prev parent reply other threads:[~2009-04-08 0:38 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-04-07 5:48 Subtyping Goswin von Brederlow
2009-04-07 7:41 ` [Caml-list] Subtyping David MENTRE
2009-04-07 13:39 ` Peng Zang
2009-04-07 21:33 ` Goswin von Brederlow
2009-04-07 21:32 ` Goswin von Brederlow
2009-04-08 0:38 ` Goswin von Brederlow [this message]
2009-04-08 1:35 ` Jacques Garrigue
2009-04-08 2:43 ` Jacques Garrigue
2009-04-08 5:16 ` Goswin von Brederlow
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=87bpr86kti.fsf@frosties.localdomain \
--to=goswin-v-b@web.de \
--cc=caml-list@inria.fr \
/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