Skip to content

SAM and Subtyping causes AbstractMethodError #10512

Closed
@ikempf

Description

@ikempf

The following fails on 2.12.3
This seems to be related to SAM, subtyping, implicit resolution and ad-hoc polymorphism.

trait JsonEncoder[A] {
  def encode(value: A): JsonValue
}

trait JsonObjectEncoder[A] extends JsonEncoder[A] {
  def encode(value: A): JsonObject
}

object JsonEncoderInstances {

  implicit val stringEncoder: JsonEncoder[String] =
    JsonString.apply

  implicit val intEncoder: JsonEncoder[Int] =
    i => JsonNumber(i)

  implicit def listEncoder[A](implicit encoder: JsonEncoder[A]): JsonObjectEncoder[List[A]] =
    l => JsonObject(l.zipWithIndex.map {
      case (a, index) => index.toString -> encoder.encode(a)
    })

}
// Following fails with java.lang.AbstractMethodError: com.ikempf.sandbox.sam_subtyping_bug.JsonEncoderInstances$$$Lambda$142/1556995360.encode(Ljava/lang/Object;)Lcom/ikempf/sandbox/sam_subtyping_bug/JsonValue;
implicitly[JsonEncoder[List[String]]].encode(List("a","b"))

The bug dissapears under following conditions

  • I do not use the SAM syntax for the listEncoder[A] (non parameterized types do not cause seem to cause the exception)
  • I return a JsonEncoder[List[A]] instead of a JsonObjectEncoder[List[A]]
  • I resolve a implicit[JsonObjectEncoder[List[String]]] instead of a implicit[JsonEncoder[List[String]]]
  • I do not use implicit resolution and directly use listEncoder[String].encode(List("a","b"))

This is an edge case that fails at runtime but it seems to me that it should compile and run just fine.
Here is a sample repo that reproduces the bug.

Environnment

Scala: 2.12.3
Java: 1.8.0_102
JVM: HotSpot 25.102-b14

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions