Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* Emulating width subtyping with 1st-class modules
@ 2010-08-05 15:02 Dario Teixeira
  2010-08-05 16:10 ` [Caml-list] " Philippe Veber
  2010-08-05 16:35 ` Alain Frisch
  0 siblings, 2 replies; 5+ messages in thread
From: Dario Teixeira @ 2010-08-05 15:02 UTC (permalink / raw)
  To: caml-list

Hi,

I have a problem where some form of width subtyping for records would be
useful.  At the present I'm taking advantage of the structural subtyping nature
of Ocaml's object system to emulate the width subtyping.  This works and is
reasonably compact, but I'm still open to other approaches.  It has occurred
to me that 3.12's modules-as-first-class-values provide yet another solution:


module type BRIEF =
sig
	val a: int
	val b: string
end


module type FULL =
sig
	include BRIEF
	val c: float
end


let print_brief m =
	let module M = (val m: BRIEF) in
	Printf.printf "A: %d, B: %s\n" M.a M.b


let print_full m =
	let module M = (val m: FULL) in
	Printf.printf "A: %d, B: %s, C: %f\n" M.a M.b M.c


module Full =
struct
	let a = 1
	let b = "full"
	let c = 0.5
end


module Brief =
struct
	let a = 0
	let b = "short"
end


let () =
	print_brief (module Brief : BRIEF);
	print_brief (module Full : BRIEF);
	print_full (module Full : FULL)


While this approach seems awfully verbose, I reckon it could be made much
more palatable via some Camlp4 sugaring.  Nevertheless, I have a question:
just how heavy would this approach be when compared to the object one?
And how would it fare in comparison to regular records?

Thanks for your attention!
Best regards,
Dario Teixeira






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

end of thread, other threads:[~2010-08-05 19:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-05 15:02 Emulating width subtyping with 1st-class modules Dario Teixeira
2010-08-05 16:10 ` [Caml-list] " Philippe Veber
2010-08-05 16:17   ` Alain Frisch
2010-08-05 16:35 ` Alain Frisch
2010-08-05 19:02   ` Dario Teixeira

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox