Skip to content

Add support for SAM conversion of polymorphic method #9916

Open
@scabug

Description

@scabug

As per spec this is not currently supported.
But javac support them just fine and they are quite useful. The following program return true in Java:

import java.util.function.Function;
import java.util.function.Supplier;

@FunctionalInterface
public interface Maybe<A> {

  <X> X fold(Supplier<X> empty, Function<A, X> just);

  static <A, X> X empty0(Supplier<X> empty, Function<A, X> just) {
    return empty.get();
  }

  static <A> Maybe<A> empty() {
      return Maybe::empty0;
  }

  static void main(String[] args) {
    Maybe<?> emptyString = Maybe.<String>empty();
    Maybe<?> emptyInt = Maybe.<Integer>empty();

    System.out.println(emptyString == emptyInt); // print "true".
  }
}

Fixing this issue would mean that the following code compile and print "true":

trait Maybe[A] {
  def fold[X](empty: => X, just: A => X): X
}

object Maybe {
  def empty0[A, X](empty: => X, just: A => X): X = empty

  def empty[A]: Maybe[A] = empty0(_ , _) // does not compile

  def main(args: Array[String]): Unit = {
    val emptyString: Maybe[String] = Maybe.empty
    val emptyInt: Maybe[Integer] = Maybe.empty

    print(emptyString eq emptyInt) // how to make this print "true"???
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions