Browse thread
[Caml-list] does class polymorphism need to be so complicated?
[
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: | Issac Trotts <ijtrotts@u...> |
| Subject: | Re: [Caml-list] does class polymorphism need to be so complicated? |
Brian Hurt wrote:
>On Wed, 20 Aug 2003, Benjamin Geer wrote:
>
>
>
>>class printer =
>> object
>> method print (obj : printable) = obj#print()
>> end ;;
>>
>>
>
>Instead of declaring obj to be printable, why not just declare that it has
>a function print? Like:
>
>class printer =
> object
> method print (obj: <print: unit->unit>) = obj#print ();
> end;;
>
>
You still have to up-cast objects having more methods than just 'print'.
# class printer =
object
method print (o:<print:unit>) = o#print
end;;
# class foo =
object
method print = print_string "[foo]"
end;;
# class bar =
object
method print = print_string "[bar]"
method frob = ()
end;;
# let f = new foo;;
val f : foo = <obj>
# let p = new printer;;
val p : printer = <obj>
# let b = new bar;;
val b : bar = <obj>
# p#print f;;
[foo]- : unit = ()
# p#print b;;
Characters 8-9:
p#print b;;
^
This expression has type bar = < frob : unit; print : unit >
but is here used with type foo = < print : unit >
Only the first object type has a method frob
# p#print (b :> <print:unit>);;
[bar]- : unit = ()
Issac
>This removes the need for a coercion, as it gets around the need to
>upcast.
>
>Brian
>
>
>-------------------
>To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
>Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
>Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>
>
>
>
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners