Skip to content

Eta-expansion should be triggered before given search for TupledFunction #7851

@eaplatanios

Description

@eaplatanios

As per the discussion on the Gitter channel with @Blaisorblade, I am running in an issue that I have minimized to the following example:

trait Wrappable[T] { }
given Wrappable[Float] { }

case class Wrapped[T: Wrappable](value: T)

trait Wrapper[T] { type WrappedT }
object Wrapper { type Aux[T <: Tuple, WrappedT0 <: Tuple] = Wrapper[T] { type WrappedT = WrappedT0 } }

given Wrapper[Unit] { type WrappedT = Unit }

given [T: Wrappable]: Wrapper[T] { type WrappedT = Wrapped[T] }

given [H: Wrappable, T <: Tuple, WrappedT0 <: Tuple](given Wrapper.Aux[T, WrappedT0]): Wrapper[H *: T] {
  type WrappedT = Wrapped[H] *: WrappedT0
}

def wrappedFunction[F, FArgs <: Tuple, WrapperFArgs <: Tuple, R: Wrappable](
  function: F
)(input: FArgs)(given
  tf: TupledFunction[F, WrapperFArgs => Wrapped[R]],
  vs: Wrapper.Aux[FArgs, WrapperFArgs]
): (R, R => Option[FArgs]) = {
  val variableInput = input.asInstanceOf[WrapperFArgs] // This is not correct but it's ok for the sake of this example.
  val result = tf.tupled(function)(variableInput)
  return (result.value, (_: R) => None)
}

object WrapperTest {
  def test1(x: Wrapped[Float], y: Wrapped[Float], z: Wrapped[Float]): Wrapped[Float] = { x }
  val test2: (Wrapped[Float], Wrapped[Float], Wrapped[Float]) => Wrapped[Float] = { (x, y, z) => x }

  def main(args: Array[String]): Unit = {
    wrappedFunction(test1: (Wrapped[Float], Wrapped[Float], Wrapped[Float]) => Wrapped[Float])(5f, 11f, 3f)
    wrappedFunction(test2)(5f, 11f, 3f)
  }
}

The above code compiles fine, but if I remove the type ascription for test1 in the end and use this instead: wrappedFunction(test1)(5f, 11f, 3f), I get a compile time error:

[error] -- Error: Test.scala:33:39 
[error] 33 |    wrappedFunction(test1)(5f, 11f, 3f)
[error]    |                                       ^
[error]    |      Any cannot be tupled as WrapperFArgs => Wrapped[Float]
[error]    |
[error]    |      where:    WrapperFArgs is a type variable with constraint <: Tuple
[error] one error found

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions