Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Zheng Li <li@p...>
Subject: Re: Smells like duck-typing

Yet another.

------------------------------------------------------------------------------
class type blurb = 
object method id: int method title:string method intro:string end

class type fresh =
object method title:string method intro:string method body:string end

class type full = object inherit blurb inherit fresh end

class virtual templt = object
  val virtual id: int
  val virtual title: string
  val virtual intro: string
  val virtual body: string
  method id = id
  method title = title
  method intro = intro
  method body = body
end

let blurb (i, t, it) :> blurb = 
object inherit templt val id=i val title=t val intro=it end 

let fresh (t, it, b) :> fresh = 
object inherit templt val title=t val intro=it val body=b end
------------------------------------------------------------------------------

Strangely though, there seems to be a bug in the OO type system: (The solution
proposed above is safe, as it does coercion)

# let coredump = object inherit templt end;;
val coredump : templt = <obj>
# coredump#title;;

Process caml-toplevel segmentation fault


Zheng Li <li@pps.jussieu.fr> writes:
> Hi,
>
> 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 <darioteixeira@yahoo.com> 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
> http://www.pps.jussieu.fr/~li
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

-- 
Zheng Li
http://www.pps.jussieu.fr/~li