Reputation: 4851
I know in Scala and many other functional languages there are monads that is mostly just an interface realization(for example in Scala with flatMap[T] and unit[T] methods) is there are any Java-style interfaces that could be a monad?
Upvotes: 7
Views: 299
Reputation: 3455
There are two problems with representing monads in Java:
flatMap
needs to operate on a function that returns the (same) specific monad, and Java's type system cannot express that.
unit
is kind of a static method in Java terms, because it creates a monad instance from a generic value. Java 8 has static methods on interfaces, but an abstract monad interface cannot know the implementation, so you have to use a factory method somewhere else (or just use a constructor).
As @AlexeyRomanov suggested, you can implement specific monads (e.g. make a Option
interface), but the abstract concept of a monad is not possible in Java. This of course means you cannot create all those useful functions that work for any monad.
Upvotes: 5
Reputation: 170909
No, because monad interface requires so-called higher-kinded types which don't exist in Java. Of course, you can implement many (not all) specific monads in Java.
Upvotes: 7