|
|
|
|
|
by jroesch
5071 days ago
|
|
It only "sucks"(causes problems rarely, and can be quite useful regardless) in dynamic languages. Languages like Go, Rust, and Haskell provide the same flexibility with static guarantees. All you must do is define type specific implementations to satisfy the interface(or typeclass). For example in Haskell, I can so something like: class Stream s where
read :: s -> (a, s)
write :: a -> s -> s
and extend it to any type: instance Stream [Int] where
read s = ...
write a s = ...
instance Stream File where
...
and so on, and I can now pass any type that is a member of Stream, to a function that expects one. |
|
Contrast OCaml, an object type is represented as a set of (method, types) tuples and type-checking is a subset check (if type A has all the methods of type B, then it's a subtype of B regardless of anything else from visibility to semantics):