caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Structural subtyping problem
@ 2010-03-28 16:38 Dario Teixeira
  2010-03-28 17:03 ` [Caml-list] " Vincent Aravantinos
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Dario Teixeira @ 2010-03-28 16:38 UTC (permalink / raw)
  To: caml-list

Hi,

I'm using the structural subtyping aspects of Ocaml's object system to emulate width
subtyping.  I've come across a problem which does not type-check, though intuitively
it seems correct.  I reckon that the compiler may need some help in the form of type
annotations and/or coercions, though their exact shape elludes me.

A simple nonsensical example that illustrates the problem is listed below; the
type-checking error occurs in function "step1", where the optional parameter "story"
is used as an object of type "< title:string; .. >".  In function "step3", this
parameter "story" is actually instantiated with objects of type "< title:string >"
and "< title:string; count:int >".

Anyway, am I correct in assuming this should be feasible?  And if so, what coercions
are required to make this compile?

Thanks in advance!
Best regards,
Dario Teixeira

=============================================================================

let rec step1 ?story () = match story with
	| Some s -> step2 s#title
	| None   -> step2 "title1"


and step2 title =
	let story =
		object
			method title = title
			method count = 0
		end
	in step3 ~story


and step3 ~story = match story#count with
	| 0 ->
		step1 ~story ()
	| 1 ->
		let story =
			object
				method title = "title2"
			end
		in step1 ~story ()
	| _ ->
		true

=============================================================================







^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-03-28 20:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-03-28 16:38 Structural subtyping problem Dario Teixeira
2010-03-28 17:03 ` [Caml-list] " Vincent Aravantinos
2010-03-28 17:07 ` Andreas Rossberg
2010-03-28 17:32 ` Stéphane Glondu
2010-03-28 19:20   ` Christophe TROESTLER
2010-03-28 20:17 ` Dario Teixeira

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).