Browse thread
Smells like duck-typing
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Chris King <colanderman@g...> |
| Subject: | Re: [Caml-list] Smells like duck-typing |
On 10/17/07, Dario Teixeira <darioteixeira@yahoo.com> wrote:
> c) Actually put the "Objective" part of OCaml to use. This is the solution
> I am using at the moment. This is what it looks like:
>
> class story (id, title, intro, body) =
> object
> val id: int option = id
> val title: string option = title
> val intro: string option = intro
> val body: string option = body
>
> method id =
> match id with
> | Some thing -> thing
> | None -> failwith "oops"
>
> method title =
> match title with
> | Some thing -> thing
> | None -> failwith "oops"
>
> method intro =
> match intro with
> | Some thing -> thing
> | None -> failwith "oops"
>
> method body =
> match body with
> | Some thing -> thing
> | None -> failwith "oops"
> end
>
>
> class full (id, title, intro, body) =
> object
> inherit story (Some id, Some title, Some intro, Some body)
> end
>
> class blurb (id, title, intro) =
> object
> inherit story (Some id, Some title, Some intro, None)
> end
>
> class fresh (title, intro, body) =
> object
> inherit story (None, Some title, Some intro, Some body)
> end
>
>
> let print_metadata s =
> Printf.printf "%d: %s\n" s#id s#title
Why not have different object types for each of the story types? e.g.
type full = < id: int; title: string; intro: string; body: string >
type blurb = < id: int; title: string; intro: string >
type fresh = < title: string; intro: string; body: string >
print_metadata can remain as is in your object example. In order to
allow functions that operate differently depending on which story type
they're given, use a polymorphic variant like in your first example.
If you wanted print_metadata to take such a type, it could be written
as:
let print_metadata s =
let s' =
match s with
| `Full f -> (f :> < id: int; title: string >)
| `Blurb f -> (f :> < id: int; title: string >)
in
Printf.printf "%d: %s\n" s'#id s'#title
(Note the match gook is unfortunately needed to get the typing right...)
If on the other hand you wanted to ditch objects entirely, you could
do a similar thing using modules and functors. E.g.:
module Full = struct
type t = { id: int; title: string; intro: string; body: string }
let id s = s.id
let title s = s.title
let intro s = s.intro
let body s = s.body
end
(* etc. *)
module Print_metadata(S: sig type t val id: t -> string val title: t
-> string) = struct
let f s = Printf.printf "%d: %s\n" (S.id s) (S.title s)
end
Of course, since calls to print_metadata now look like
"Print_metadata(Full).f story", you're essentially forced to write out
the types of everything, which is probably what you wanted to avoid
anyway.
HTH,
Chris