English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Structural subtyping problem
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-03-28 (16:38)
From: Dario Teixeira <darioteixeira@y...>
Subject: Structural subtyping problem

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 =
			method title = title
			method count = 0
	in step3 ~story

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