Skip to content

Type inference issue with intersection types #8378

Closed
@adamgfraser

Description

@adamgfraser

minimized code

trait Has[_]

trait A
trait B
trait C

trait ZLayer[-RIn, +E, +ROut]

object ZLayer {
  def fromServices[A0, A1, B](f: (A0, A1) => B): ZLayer[Has[A0] with Has[A1], Nothing, Has[B]] =
    ???
}

val live: ZLayer[Has[A] & Has[B], Nothing, Has[C]] =
  ZLayer.fromServices { (a: A, b: B) =>
    new C {}
  }

Compilation output

// type mismatch
// found:    (A, B) => C
// required: (A, A) => C

expectation

I would have expected this to compile because live expected a ZLayer with the first parameter of Has[A] with Has[B], which corresponds to a function (A, B) => C like I am providing. But the compiler seems to be expecting the first part of the intersection type twice. If I change the type signature of the first parameter in the return type of fromServices to Has[B] with Has[A] then I get an error that the compiler expected (B, B) => C. This issue also appears to exist on Scala 2, reported at scala/bug#11898.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions