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-18 (17:05)
From: Dario Teixeira <darioteixeira@y...>
Subject: Re: [Caml-list] Smells like duck-typing
> So now you've defined full_story, blurb_story, and fresh_story as 
> types.  Now, I write:
> let get_body (story: full_story) = story#get_body;;
> let my_blurb = new blurb_story(id, "A Title", "An Intro");;
> let my_body = get_body my_blurb;;
> what happens?


Well, that's only a problem if you insist in thinking in terms
of inheritance.  Remember that the semantics of marble-carving
are NOT "all that works on full_story should also work on blurb_story",
but the other way around.

Also, imagine you were to add the "get_body" method to the root
"story" class.  The type system in Marble-Caml is smart enough
to tell you that since the blurb_story carved out the "body" field,
then it must also carve out methods that use it.  Aren't imaginary
languages wonderfull?... :-)

(In addition, note that in any real world situation, it wouldn't
make any sense for the user to want to invoke get_body on a blurb
object -- after all, by definition blurbs have no body).


Yahoo! Answers - Got a question? Someone out there knows the answer. Try it