dotty-feature-requests icon indicating copy to clipboard operation
dotty-feature-requests copied to clipboard

TupledFunction should take variance into account

Open robstoll opened this issue 5 years ago • 0 comments

I don't know the reasons for the restriction that givens for TupleFunction are only synthesized if the types are exactly same, i.e have invariant behaviour. I guess the reason is because this is for sure safe and variance can be added later on if proven to be safe as well. I have the feeling it is safe, hence my feature request: Consider the following:

object Main{
  
  def main(args: Array[String]) = {
    val f: Number => String = _ => "a"
    val g: Integer => CharSequence = f           // works as expected
    
    var h: Tuple1[Number] => String = (_) => "b"
    var i: Tuple1[Integer] => String = h         // works as expected
    
    def bar[F,G](f: F, g: G)(using tf: TupledFunction[F,G]) = ???
    def [F, Args <: Tuple, R](f: F) tupled (using tf: TupledFunction[F, Args => R]): Args => R = tf.tupled(f)
  
    h = f.tupled       // works as expected
    i = f.tupled       // works as expected
    
    val b = bar(f, i)  // (Number => String) cannot be tupled as ((Tuple1[Integer]) => String)
    bar(f, f.tupled)   // works as expected, this extra step should not be needed IMO
    
  }
}

robstoll avatar Mar 13 '20 12:03 robstoll