Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Monads in Java 8

I am trying to implement a Monad interface in Java 8 following this article however I've got the following compiling errors


2 errors found:

  • File:FunctorsMonads.java [line: 36]

    Error: FOptional is not abstract and does not override abstract method flatMap(java.util.function.Function>) in Monad

  • File:FunctorsMonads.java [line: 50]

    Error: name clash: flatMap(java.util.function.Function>) in FOptional and flatMap(java.util.function.Function) in Monad have the same erasure, yet neither overrides the other


The Functor interface works just fine. Any help is much appreciated.

Here is the code:

import java.util.function.Function;

public class FunctorsMonads {

  public static void main(String[] args) {
    System.out.println(tryParse("47"));
    System.out.println(tryParse("a"));

    FOptional<String> str = FOptional.of("47");
    System.out.println(str);
    FOptional<FOptional<Integer>> num = str.map(FunctorsMonads::tryParse);
    System.out.println(num);
    FOptional<Integer> num2 = str.flatMap(FunctorsMonads::tryParse);
    System.out.println(num2);
  }

  static FOptional<Integer> tryParse(String s){
    try {
      final int i = Integer.parseInt(s);
      return FOptional.of(i);
    } catch (NumberFormatException e) {
      return FOptional.empty();
    }
  }
}

interface Functor<T, F extends Functor<?, ?>> {
  <R> F map(Function<T, R> f);
}

interface Monad<T, M extends Monad<?, ?>> extends Functor<T, M> {
  M flatMap(Function<T, M> f);
}

//class FOptional<T> implements Functor<T, FOptional<?>>
class FOptional<T> implements Monad<T, FOptional<?>> {
  private final T valueOrNull;

  private FOptional(T valueOrNull) {
    this.valueOrNull = valueOrNull;
  }

  public <R> FOptional<R> map(Function<T, R> f) {
    if (valueOrNull == null)
      return empty();
    else
      return of(f.apply(valueOrNull));
  }

  public <R> FOptional<R> flatMap(Function<T, FOptional<R>> f) {
    if (valueOrNull == null)
      return empty();
    else
      return f.apply(valueOrNull);
  }

  public static <T> FOptional<T> of(T a) {
    return new FOptional<T>(a);
  }

  public static <T> FOptional<T> empty() {
    return new FOptional<T>(null);
  }

  @Override
  public String toString() {
    return getClass().getName() + "<" + valueOrNull + ">";
  }
}

Edit:
I have added the following lines in the main method as a litmus test of the implementation's correctness:
FOptional<Integer> num2 = str.flatMap(FunctorsMonads::tryParse); System.out.println(num2);

like image 678
dbaltor Avatar asked Jan 03 '19 16:01

dbaltor


2 Answers

You cannot implement a fully type-safe Monad interface in Java. The correct signature for flatmap would be something like <R> M<R> flatMap(Function<T, M<R>> f), but this is not expressible in Java. This M<R> expression is called a higher-kinded type.

like image 114
MikeFHay Avatar answered Nov 13 '22 00:11

MikeFHay


The implemented method FOptional::flatMap doesn't match the definition in the interface Monad.

All you need is to amend the interface Monad itself:

interface Monad<T, M extends Monad<?, ?>> extends Functor<T, M> {

    <R> M flatMap(Function<T, FOptional<R>> f);
}

Moreover, as far as I understand from the functionality, both interfaces shall be designed with the same spirit. Compare the new interface with the Functor interface:

interface Functor<T, F extends Functor<?, ?>> {

    <R> F map(Function<T, R> f);
}

Both of them define methods that return the new generic type: Monad with M and Functor with F and use a newly introduced generic type R.

like image 23
Nikolas Charalambidis Avatar answered Nov 13 '22 00:11

Nikolas Charalambidis