rsc icon indicating copy to clipboard operation
rsc copied to clipboard

Infer return types of `new` constructor calls

Open illicitonion opened this issue 6 years ago • 1 comments

Ideally each of these shouldn't need explicit type ascription:

package examples.pure_scala

object Lib {
  def h1 = new String("hello")

  val h2 = new String("hi")
}

because it's super obvious what the types are, but they currently do:

src/examples/pure_scala/Lib.scala:4: error: No type found at src/examples/pure_scala/Lib.scala@3:2..3:30 for definition: def <examples/pure_scala/Lib.h1().> = new String("hello")
  def h1 = new String("hello")
  ^
src/examples/pure_scala/Lib.scala:6: error: No type found at src/examples/pure_scala/Lib.scala@5:2..5:27 for definition: val def <examples/pure_scala/Lib.h2.> = new String("hi")
  val h2 = new String("hi")
  ^
two errors found

illicitonion avatar Jun 13 '19 14:06 illicitonion

A naive implementation would break for polymorphic constructors:

class C[A](x: A)

val x = new C(1) // should be inferred as x: C[Int] but would need to infer into C's constructor

wiwa avatar Jun 16 '19 21:06 wiwa