Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Smells like duck-typing
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-10-17 (15:24)
From: Zheng Li <li@p...>
Subject: Re: Smells like duck-typing


I would vote for the object solution. However, both record and object have
their own pros and cons. The actual solution should depends on your requirement
and taste. Just for an example, the following data structure is a possibility:

# type +'a t = {title:string; intro:string; extra:'a} constraint 'a = < .. >;;
# let fresh (title,intro,(body:string)) = 
  {title=title; intro=intro; extra=object method body=body end};;
# let blurb ((id:int),title,intro) =
  {title=title; intro=intro; extra=object method id=id end};;
# let print_metadata s = Printf.printf "%d: %s\n" s.extra#id s.title;;
val print_metadata : < id : int; .. > t -> unit = <fun>

Dario Teixeira <> writes:
> I have been trying to reach a sane modelling in OCaml for a "story"
> data structure in a CMS.  The problem is that I find myself needing
> a degree of expressiveness that I can't find in the language!  I do
> have a working, tentative solution, but it has a few ugly aspects
> that I would very much like to improve.  Details follow.  (Sorry
> for the long post; at least I hope it's not too dense and hard to
> follow).

Zheng Li