What is an analog analog in Java?

I know that in Scala and many other functional languages ​​there are monads that basically represent an implementation of an interface (for example, in Scala with the flatMap [T] and unit [T] methods) are there any Java-style interfaces that can be a monad?

+6
source share
2 answers

There are two problems with representing monads in Java:

flatMap needs to use a function that returns (the same) specific monad, and a system like Java cannot express this.

unit is a kind of static method in terms of Java, since it creates an instance of a monad from a common meaning. Java 8 has static methods on interfaces, but the abstract monad interface cannot know the implementation, so you need to use the factory method somewhere else (or just use the constructor).

As @AlexeyRomanov suggested, you can implement certain monads (for example, make the Option interface), but the abstract concept of a monad is not possible in Java. This, of course, means that you cannot create all those useful functions that work for any monad.

+5
source

No, because the monad interface requires the so-called higher-grade types that are not in Java. Of course, you can implement many (not all) specific monads in Java.

+6
source

Source: https://habr.com/ru/post/987247/


All Articles