public
Last active

bug with implicits and type inference

  • Download Gist
implicit-zero.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// Making Zero covariant in Z allows compilation
trait Zero[/*+*/Z] {
val zero: Z
}
 
object Zero {
implicit def IntZero: Zero[Int] = error("stub")
// removing UnitZero allows compilation
implicit def UnitZero: Zero[Unit] = error("stub")
}
 
object Scalaz {
def mzero[ZZ](implicit z: Zero[ZZ]): ZZ = z.zero
}
 
object test {
import Scalaz._
// Passing type argument Int explicitly allows compilation: mzero[Int]
mzero: Int
 
}
 
// (fragment of zero.scala):18: error: ambiguous implicit values:
// both method UnitZero in object Zero of type => this.Zero[Unit]
// and method IntZero in object Zero of type => this.Zero[Int]
// match expected type this.Zero[ZZ]
// mzero: Int
// ^
// one error found
typer-error.log
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
typing (mzero: Int), pt = ?, undetparams = List(), implicits-enabled = true, silent = true
typing Int, pt = ?, undetparams = List(), implicits-enabled = true, silent = true
typed scala.Int:Int, undetparams = List(), pt = ?
adapted scala.Int:Int to ?, List()
typing mzero, pt = Int, undetparams = List(), implicits-enabled = true, silent = true
typed Scalaz.mzero:[ZZ](implicit z: Zero[ZZ])ZZ, undetparams = List(), pt = Int
typing implicit with undetermined type params: List(type ZZ)
Scalaz.mzero[ZZ]
typed impl?? UnitZero:=> Zero[Unit] ==> Zero.UnitZero with pt = Zero[ZZ], wildpt = Zero[?]
typing Zero, pt = ?, undetparams = List(), implicits-enabled = false, silent = false
typed Zero:Zero.type with underlying object Zero, undetparams = List(), pt = ?
adapted Zero:object Zero to ?, List()
typed implicit Zero.UnitZero:=> Zero[Unit], pt = Zero[?]
adapted implicit method UnitZero:Zero[Unit] to Zero[?]
tvars = List(?ZZ)/List([ _>:(Unit) | _<:(Unit) ] _= <notype>)
RESULT = SearchResult(Zero.UnitZero, TreeTypeSubstituter(List(type ZZ),List(Unit)))
typed impl?? IntZero:=> Zero[Int] ==> Zero.IntZero with pt = Zero[ZZ], wildpt = Zero[?]
typing Zero, pt = ?, undetparams = List(), implicits-enabled = false, silent = false
typed Zero:Zero.type with underlying object Zero, undetparams = List(), pt = ?
adapted Zero:object Zero to ?, List()
typed implicit Zero.IntZero:=> Zero[Int], pt = Zero[?]
adapted implicit method IntZero:Zero[Int] to Zero[?]
tvars = List(?ZZ)/List([ _>:(Int) | _<:(Int) ] _= <notype>)
RESULT = SearchResult(Zero.IntZero, TreeTypeSubstituter(List(type ZZ),List(Int)))
typing mzero, pt = ?, undetparams = List(type ZZ), implicits-enabled = true, silent = true
typed Scalaz.mzero:[ZZ](implicit z: Zero[ZZ])ZZ, undetparams = List(type ZZ), pt = ?
typing implicit with undetermined type params: List(type ZZ, type ZZ)
Scalaz.mzero[ZZ]
typed impl?? UnitZero:=> Zero[Unit] ==> Zero.UnitZero with pt = Zero[ZZ], wildpt = Zero[?]
typing Zero, pt = ?, undetparams = List(), implicits-enabled = false, silent = false
typed Zero:Zero.type with underlying object Zero, undetparams = List(), pt = ?
adapted Zero:object Zero to ?, List()
typed implicit Zero.UnitZero:=> Zero[Unit], pt = Zero[?]
adapted implicit method UnitZero:Zero[Unit] to Zero[?]
tvars = List(?ZZ, ?ZZ)/List([ _>:() | _<:() ] _= <notype>, [ _>:(Unit) | _<:(Unit) ] _= <notype>)
RESULT = SearchResult(Zero.UnitZero, TreeTypeSubstituter(List(type ZZ),List(Unit)))
typed impl?? IntZero:=> Zero[Int] ==> Zero.IntZero with pt = Zero[ZZ], wildpt = Zero[?]
typing Zero, pt = ?, undetparams = List(), implicits-enabled = false, silent = false
typed Zero:Zero.type with underlying object Zero, undetparams = List(), pt = ?
adapted Zero:object Zero to ?, List()
typed implicit Zero.IntZero:=> Zero[Int], pt = Zero[?]
adapted implicit method IntZero:Zero[Int] to Zero[?]
tvars = List(?ZZ, ?ZZ)/List([ _>:() | _<:() ] _= <notype>, [ _>:(Int) | _<:(Int) ] _= <notype>)
RESULT = SearchResult(Zero.IntZero, TreeTypeSubstituter(List(type ZZ),List(Int)))
caught scala.tools.nsc.symtab.Types$TypeError: ambiguous implicit values:
both method UnitZero in object Zero of type => Zero[Unit]
and method IntZero in object Zero of type => Zero[Int]
match expected type Zero[ZZ] in typed: mzero
zero.scala:18: error: ambiguous implicit values:
both method UnitZero in object Zero of type => Zero[Unit]
and method IntZero in object Zero of type => Zero[Int]
match expected type Zero[ZZ]
mzero: Int
^
adapted Scalaz.mzero:[ZZ](implicit z: Zero[ZZ])ZZ to Int, List()
typed (<error: <none>>: Int):Int, undetparams = List(), pt = ?
typer-success-covariant.log
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
typing (mzero: Int), pt = ?, undetparams = List(), implicits-enabled = true, silent = true
typing Int, pt = ?, undetparams = List(), implicits-enabled = true, silent = true
typed scala.Int:Int, undetparams = List(), pt = ?
adapted scala.Int:Int to ?, List()
typing mzero, pt = Int, undetparams = List(), implicits-enabled = true, silent = true
typed Scalaz.mzero:[ZZ](implicit z: Zero[ZZ])ZZ, undetparams = List(), pt = Int
typed impl?? IntZero:=> Zero[Int] ==> Zero.IntZero with pt = Zero[Int], wildpt = Zero[Int]
typing Zero, pt = ?, undetparams = List(), implicits-enabled = false, silent = false
typed Zero:Zero.type with underlying object Zero, undetparams = List(), pt = ?
adapted Zero:object Zero to ?, List()
typed implicit Zero.IntZero:=> Zero[Int], pt = Zero[Int]
adapted implicit method IntZero:Zero[Int] to Zero[Int]
tvars = List()/List()
RESULT = SearchResult(Zero.IntZero, TreeTypeSubstituter(List(),List()))
typing Scalaz.mzero[Int](Zero.IntZero), pt = Int, undetparams = List(), implicits-enabled = true, silent
= false
typing Scalaz.mzero[Int], pt = ?, undetparams = List(), implicits-enabled = true, silent = false
typed Scalaz.mzero[Int]:(implicit z: Zero[Int])Int, undetparams = List(), pt = ?
adapted Scalaz.mzero[Int]:(implicit z: Zero[Int])Int to ?, List()
typing Zero.IntZero, pt = Zero[Int], undetparams = List(), implicits-enabled = true, silent = false
typed Zero.IntZero:Zero[Int], undetparams = List(), pt = Zero[Int]
adapted Zero.IntZero:Zero[Int] to Zero[Int], List()
typed Scalaz.mzero[Int](Zero.IntZero):Int, undetparams = List(), pt = Int
adapted Scalaz.mzero[Int](Zero.IntZero):Int to Int, List()
adapted Scalaz.mzero:[ZZ](implicit z: Zero[ZZ])ZZ to Int, List()
typed (Scalaz.mzero[Int](Zero.IntZero): Int):Int, undetparams = List(), pt = ?
adapted (Scalaz.mzero[Int](Zero.IntZero): Int):Int to ?, List()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.