caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Vincent Aravantinos <vincent.aravantinos@yahoo.fr>
To: Dario Teixeira <darioteixeira@yahoo.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Smells like duck-typing
Date: Thu, 18 Oct 2007 17:42:04 +0200	[thread overview]
Message-ID: <189AC25B-86C0-42ED-A2C4-38C7F1015840@yahoo.fr> (raw)
In-Reply-To: <932096.75090.qm@web54602.mail.re2.yahoo.com>


Le 17 oct. 07 à 15:35, Dario Teixeira a écrit :

> Hi,
>
> I have been trying to reach a sane modelling in OCaml for a "story"
> data structure in a CMS.

(...)

> b) Use only full_t and make all fields option types.  However, not  
> only is
>    this cumbersome to use, but is also conceptually wrong, because  
> it does
>    not capture the fact that, for example, all "blurb" stories have  
> three
>    *mandatory* fields.

The following uses polymorphic variants for options so as to make  
those fields mandatory (thus it is more safe).

However it remains very cumbersome :)) but I found it interesting  
(and quite funny).

And to follow the current topic, this solution makes inheritance the  
right way (or, at least, the way you want).

But honestly, I don't think it's usable (?)


--------------
(* TYPES *)

type ('a,'b) polyoption = [<`Some of 'a|`None] as 'b

type 'a string_option = (string,[<`None|`Some of string] as 'a)  
polyoption
type 'a int_option    = (int,[<`None|`Some of int] as 'a) polyoption

type some_string = [`Some of string]
type some_int    = [`Some of int]
type nothing     = [`None]

type ('a,'b,'c,'d) t = {
   id:    'a int_option;
   title: 'b string_option;
   intro: 'c string_option;
   body:  'd string_option
}

(* The types we will finally use *)
type full_t  = (some_int, some_string, some_string, some_string) t
type blurp_t = (some_int, some_string, some_string, nothing    ) t
type fresh_t = (nothing , some_string, some_string, some_string) t


(* SOME TESTS *)
let full : full_t = {id = `Some 1; title = `Some "blablax"; intro =  
`Some "blox" ; body = `Some "pouetx"}

   (* Check with a missing field:
   # let badfull : full_t = {id = `None; title = `Some "blablax";  
intro = `Some "blox" ; body = `Some "pouetx"};;
   This expression has type (...)
   *)

let blurp : blurp_t = {id = `Some 2; title = `Some "blablay"; intro =  
`Some "bloy" ; body = `None}
   (* Check with too much fields:
   # let badblurp : blurp_t = {id = `Some 2; title = `Some "blablay";  
intro = `Some "bloy" ; body = `Some "pouety"};;
   This expression has type (...)
   *)

let fresh : fresh_t = {id = `None; title = `Some "blablaz"; intro =  
`Some "bloz" ; body = `Some "pouetz"}

(* FUNCTIONS *)

let get_body : ('a,'b,'c,'d) t -> string = fun x -> let `Some x =  
x.body in x
let get_title : ('a,'b,'c,'d) t -> string = fun x -> let `Some x =  
x.title in x;;

------------------


TESTS:

# get_title full;;
- : string = "blablax"
# get_title blurp;;
- : string = "blablay"
# get_title fresh;;
- : string = "blablaz"
# get_body full;;
- : string = "pouetx"
# get_body blurp;;
This expression has type (...)
# get_body fresh;;
- : string = "pouetz"


Isn't it funny ?

Cheers
Vincent

  parent reply	other threads:[~2007-10-18 15:42 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-17 13:35 Dario Teixeira
2007-10-17 14:13 ` [Caml-list] " Arnaud Spiwack
2007-10-17 14:47   ` Dario Teixeira
2007-10-17 14:25 ` Daniel Bünzli
2007-10-17 15:03   ` skaller
2007-10-17 15:13     ` Dario Teixeira
2007-10-17 15:25       ` Arnaud Spiwack
2007-10-17 15:32       ` Daniel Bünzli
2007-10-17 16:21         ` Chris King
2007-10-18  7:28           ` Stefano Zacchiroli
2007-10-18  8:33             ` [ANN] pa_oo and pa_polymap for 3.10 (Re: [Caml-list] Smells like duck-typing) Jacques Garrigue
2007-10-17 16:57         ` [Caml-list] Smells like duck-typing skaller
2007-10-17 16:52       ` skaller
2007-10-17 16:59         ` Robert Fischer
2007-10-17 14:33 ` Chris King
2007-10-17 14:59   ` Dario Teixeira
2007-10-17 15:24 ` Vincent Aravantinos
2007-10-17 15:26 ` Zheng Li
2007-10-18 16:13   ` Zheng Li
2007-10-18 16:37     ` [Caml-list] " William D. Neumann
2007-10-19  0:58       ` Jacques Garrigue
2007-10-17 19:59 ` [Caml-list] " Richard Jones
2007-10-17 20:24 ` Dario Teixeira
2007-10-18  7:37   ` Stefano Zacchiroli
2007-10-18 10:31     ` Dario Teixeira
2007-10-18 10:37       ` Stefano Zacchiroli
2007-10-18 13:28       ` Robert Fischer
2007-10-18 14:10         ` Dario Teixeira
2007-10-18 14:18           ` Brian Hurt
2007-10-18 14:29             ` Arnaud Spiwack
2007-10-18 14:45               ` Brian Hurt
2007-10-18 15:02                 ` Arnaud Spiwack
2007-10-18 15:07                   ` Robert Fischer
2007-10-18 15:14                     ` Arnaud Spiwack
2007-10-18 16:39                   ` skaller
2007-10-18 16:49                     ` Arnaud Spiwack
2007-10-18 17:47                       ` skaller
2007-10-18 19:55                         ` Robert Fischer
2007-10-18 16:22                 ` skaller
2007-10-18 16:30                   ` Dario Teixeira
2007-10-18 14:58           ` Robert Fischer
2007-10-18 15:11             ` William D. Neumann
2007-10-18 15:47               ` Loup Vaillant
2007-10-18 16:08                 ` William D. Neumann
2007-10-19 13:08               ` Ed Keith
2007-10-18 16:24           ` Dario Teixeira
2007-10-18 16:35             ` Vincent Aravantinos
2007-10-18 16:43             ` Brian Hurt
2007-10-18 17:04               ` William D. Neumann
2007-10-18 17:05               ` Dario Teixeira
2007-10-18 17:22                 ` Brian Hurt
2007-10-18 17:58                   ` Dario Teixeira
2007-10-18 15:42 ` Vincent Aravantinos [this message]
     [not found] <47161E3B.3060704@tsc.uc3m.es>
2007-10-17 15:01 ` Dario Teixeira
2007-10-17 20:20   ` Alain Frisch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=189AC25B-86C0-42ED-A2C4-38C7F1015840@yahoo.fr \
    --to=vincent.aravantinos@yahoo.fr \
    --cc=caml-list@yquem.inria.fr \
    --cc=darioteixeira@yahoo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).