What’s the closest thing to Haskell’s typeclasses in OCaml?

It really depends what you want to achieve. If you are happy with the OCaml polymorphic comparison function (which will not work on cyclic and functional values), you can simply write: let my_sort l = List.sort Pervasives.compare l The more generic way to mimic type classes is to use functors: module type COMPARABLE = sig … Read more

Can I define the Negatable interface in Java?

Actually, yes. Not directly, but you can do it. Simply include a generic parameter and then derive from the generic type. public interface Negatable<T> { T negate(); } public static <T extends Negatable<T>> T normalize(T a) { return a.negate().negate(); } You would implement this interface like so public static class MyBoolean implements Negatable<MyBoolean> { public … Read more

Why is Haskell missing “obvious” Typeclasses

As other answers have pointed out, Haskell tends to use different vocabulary. However, I don’t think they’ve explained the reason for the difference very well. In a language like Java, functions are not “first class citizens”; it’s true that anonymous functions are available in the latest versions, but this style of interface (Collection, Indexable, Interable, … Read more

What’s the “|” for in a Haskell class definition?

The vertical bar is a syntactic separator with no meaning itself, used to introduce functional dependencies on a multi-parameter type class, so technically | means nothing whatsoever. Presumably | was chosen as a visual analogy to the same symbol’s use for pattern guards on functions. As far as the functional dependencies themselves go, just read … Read more

Difference between OOP interfaces and FP type classes [duplicate]

You can look at this from multiple angles. Other people will disagree, but I think OOP interfaces are a good place to start from to understand type classes (certainly compared to starting from nothing at all). People like to point out that conceptually, type classes classify types, much like sets – “the set of types … Read more

What is Haskell’s Data.Typeable?

Data.Typeable is an encoding of an well known approach (see e.g. Harper) to implementing delayed (dynamic) type checking in a statically typed language — using a universal type. Such a type wraps code for which type checking would not succeed until a later phase. Rather than reject the program as ill-typed, the compiler passes it … Read more

Scala double definition (2 methods have the same type erasure)

I like Michael Krämer’s idea to use implicits, but I think it can be applied more directly: case class IntList(list: List[Int]) case class StringList(list: List[String]) implicit def il(list: List[Int]) = IntList(list) implicit def sl(list: List[String]) = StringList(list) def foo(i: IntList) { println(“Int: ” + i.list)} def foo(s: StringList) { println(“String: ” + s.list)} I think … Read more

Confused by the meaning of the ‘Alternative’ type class and its relationship to other type classes

To begin with, let me offer short answers to each of these questions. I will then expand each into a longer detailed answer, but these short ones will hopefully help in navigating those. No, Alternative and Monoid don’t mean different things; Alternative is for types which have the structure both of Applicative and of Monoid. … Read more

What is “polymorphism a la carte” and how can I benefit from it?

You can take Polymorphism a la carte as Polymorphism on demand. Clojure community are proud of the term Polymorphism a la carte because of the fact that Clojure support multiple polymorphism strategies. Some of them are: Prototype-based polymorphism Inheritance polymorphism This is the polymorphism strategy used by Java. Clojure support this by proxy. Useful when … Read more