The nearest equivalent to Haskell type classes in Clojure are
multimethods, but as Clojure is a dynamically typed language, the
compiler does not enforce the 'interface' defined by the multimethods.

In Clojure, I can't think of any way you could enforce such a contract
at compile time. Clojure is not statically typed, and does not place
any restriction on the data you can pass to a function. You could
write a predicate to check for invalid data at runtime, of course, but
this would have an obvious performance impact (which may or may not be
worth the trouble).

You could just always state the interface in the documentation, and
leave it up to the programmer to implement it correctly.

- James

On Mar 7, 11:14 pm, mikel <mev...@mac.com> wrote:
> Most conventional object-oriented languages conflate data layout with
> protocol, by gluing those two orthogonal concepts together in classes.
> If you want the behavior provided by a class, you have to take the
> data layout with it, and vice versa, even though the two things are
> logically orthogonal, and even though you may well want one but not
> the other.
>
> Java took a baby step toward separating the two concerns with
> interfaces, though I gather that's not the purpose the designers had
> in mind. If I recall correctly, they were trying to provide some of
> the benefits of multiple inheritance without all of the complexity it
> adds.
>
> Clojure takes us farther down the road toward separating structure
> from behavior, in that you can use proxy to glue together arbitrary
> interfaces and obtain live, working objects that implement them. Your
> newly-created object need not use any class at all, but can provide
> any data layout that is suited to supporting your implementation of
> the methods from the interfaces you've glued together.
>
> In my mind, the desirable end of that particular road is represented
> by the way that Haskell handles this particular issue: in Haskell,
> data layout is handled by algebraic datatypes; protocol is handled by
> typeclasses. Maybe the roles of the two concepts in Haskell would be
> clearer if they were called "structures" and "protocols". In Haskell,
> you can define a new structure without saying anything about any
> protocols, and you can define a new protocol without saying anything
> about any structures. That seems just right to me.
>
> You can do a lot of that in Clojure, too, but, unless I'm mistaken,
> there are some arbitrary limits as things stand right now. I don't
> know of a way in Clojure to define an interface; as far as I know, if
> you decide you need a new interface, you have to step outside Clojure
> to define it. And you can have any kind of structure you want, as long
> as it's a map, set, or sequence.
>
> Konrad Hinsen seems to be successfully building algebraic datatypes,
> so that maybe proves that the tools are there in Clojure to build
> arbitrary data layouts. That's half the picture. What about the other
> half? How would one go about defining a new interface using only
> Clojure code?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to