bug icon indicating copy to clipboard operation
bug copied to clipboard

implicit def x[T, U](value: U)(implicit fn: U => T): Option[T] not usable as implicit U => Option[T]

Open scabug opened this issue 12 years ago • 4 comments

This code does not compile :

object Sample extends App{

import scala.language.implicitConversions

implicit def asOptional[T, U](value: U)(implicit fn: U => T): Option[T] = Some(fn(value))		

case class A(v:Option[Double])	

A(asOptional(1))

A(1)
}

the compiler error is :

<console>:15: error: type mismatch;
 found   : Int(1)
 required: Option[Double]
              A(1)

As a workaround, "splitting" the implicit def will make the compiler happy :


implicit def asOptional[U,T](value: U)(implicit fn: U => T): Option[T] = fn(value)
	
implicit def toOption[T](value: T): Option[T] = Option(value)

Paul Phillips answer me on the google-group that it might be a bug : here

scabug avatar Feb 13 '13 08:02 scabug

Imported From: https://issues.scala-lang.org/browse/SI-7123?orig=1 Reporter: Eric Vantillard (evantill) Affected Versions: 2.9.2, 2.10.0

scabug avatar Feb 13 '13 08:02 scabug

@retronym said: I'm not sure this is supposed to work; demoting the priority for now.

scabug avatar Feb 13 '13 12:02 scabug

@hubertp said: Implicit search finds the asOptional implicit. The problem is during its verification. In typedImplicit (Implicits.scala) we call typed1 which will type the application asOptional(1) and the type of this expression is (implicit fn: Int => T)Option[T]. Since we don't call adapt in typedImplicit we will not trigger search for an implicit arg and this type will fail to conform to Option[Double]. I've seen at least one other bug with a similar problem but I cannot recall the number now.

scabug avatar Feb 13 '13 14:02 scabug

@hubertp said: Actually we call adapt normally but not for views (and the case here involves view search).

scabug avatar Feb 18 '13 17:02 scabug