implicit

What are implicit objects?

こ雲淡風輕ζ 提交于 2020-07-17 07:23:08
问题 I was reading about type classes where implicit objects were mentioned: object Math { trait NumberLike[T] { def plus(x: T, y: T): T def divide(x: T, y: Int): T def minus(x: T, y: T): T } object NumberLike { implicit object NumberLikeDouble extends NumberLike[Double] { def plus(x: Double, y: Double): Double = x + y def divide(x: Double, y: Int): Double = x / y def minus(x: Double, y: Double): Double = x - y } implicit object NumberLikeInt extends NumberLike[Int] { def plus(x: Int, y: Int): Int

What are implicit objects?

你离开我真会死。 提交于 2020-07-17 07:21:25
问题 I was reading about type classes where implicit objects were mentioned: object Math { trait NumberLike[T] { def plus(x: T, y: T): T def divide(x: T, y: Int): T def minus(x: T, y: T): T } object NumberLike { implicit object NumberLikeDouble extends NumberLike[Double] { def plus(x: Double, y: Double): Double = x + y def divide(x: Double, y: Int): Double = x / y def minus(x: Double, y: Double): Double = x - y } implicit object NumberLikeInt extends NumberLike[Int] { def plus(x: Int, y: Int): Int

How to wrap a method having implicits with another method in Scala?

試著忘記壹切 提交于 2020-07-16 04:45:27
问题 I have a method with implicits: def f(x: String)(implicit dispatcher: ExecutionContextExecutor, mat: ActorMaterializer) = ??? And I want to create a helper method like: def g1(y: String) = f("uri1" + y) def g2(y: String) = f("uri2" + y) Of course, this cannot compile as no implicits found for parameter ex: ExecutionContext for method g . I don't want to repeat the implicits declaration in g . So, what's the idiomatic solution for this case? 回答1: Idiomatic solution is to repeat implicit

Why do both of these implicits match when only one of them would type check?

北城余情 提交于 2020-07-05 12:57:56
问题 I am trying to diagnose a compiler error which is caused by diverging implicit expansion errors. Here is what I am seeing: ❯ scala Welcome to Scala 2.13.1 (OpenJDK 64-Bit Server VM, Java 1.8.0_252). Type in expressions for evaluation. Or try :help. scala> :settings -Xlog-implicits scala> :paste // Entering paste mode (ctrl-D to finish) trait Invariant[A] trait Covariant[+A] trait Foo implicit def invariantTuple1[T1: Invariant]: Invariant[Tuple1[T1]] = ??? implicit def invariantFoo: Invariant

Why do both of these implicits match when only one of them would type check?

扶醉桌前 提交于 2020-07-05 12:57:15
问题 I am trying to diagnose a compiler error which is caused by diverging implicit expansion errors. Here is what I am seeing: ❯ scala Welcome to Scala 2.13.1 (OpenJDK 64-Bit Server VM, Java 1.8.0_252). Type in expressions for evaluation. Or try :help. scala> :settings -Xlog-implicits scala> :paste // Entering paste mode (ctrl-D to finish) trait Invariant[A] trait Covariant[+A] trait Foo implicit def invariantTuple1[T1: Invariant]: Invariant[Tuple1[T1]] = ??? implicit def invariantFoo: Invariant

Implicit resolution fails for a contravariant type with a type bound

核能气质少年 提交于 2020-06-26 14:59:27
问题 The following code compiles: class X[U, T <: U] object X { implicit def genericX[U, T <: U]: X[U, T] = new X[U, T] } implicitly[X[String, String]] // compiles When we make T covariant ( class X[U, +T <: U] ) it compile as well: class X[U, +T <: U] object X { implicit def genericX[U, T <: U]: X[U, T] = new X[U, T] } implicitly[X[String, String]] // compiles When we make T contravariant ( class X[U, -T <: U] ), compiler fails to materilize implicitly[X[String, String]] . Strangely enough, it is

Implicit error when trying to implement the `Absurd` typeclass

╄→尐↘猪︶ㄣ 提交于 2020-06-25 04:12:24
问题 I'm trying to implement the Absurd typeclass (as seen in Haskell's Data.Boring library) in Scala. I'm able to define an Absurd instance for Nothing . Unfortunately, when I try to define an absurd instance for Either , I get a missing implicit error sealed trait Absurd[A] { def absurd[X](a: A): X } object Absurd { def apply[A: Absurd, B](a: A):B = implicitly[Absurd[A]].absurd[B](a) implicit val absurdForNothing: Absurd[Nothing] = new Absurd[Nothing]{ override def absurd[X](a: Nothing): X = a }

Implicit error when trying to implement the `Absurd` typeclass

元气小坏坏 提交于 2020-06-25 04:12:12
问题 I'm trying to implement the Absurd typeclass (as seen in Haskell's Data.Boring library) in Scala. I'm able to define an Absurd instance for Nothing . Unfortunately, when I try to define an absurd instance for Either , I get a missing implicit error sealed trait Absurd[A] { def absurd[X](a: A): X } object Absurd { def apply[A: Absurd, B](a: A):B = implicitly[Absurd[A]].absurd[B](a) implicit val absurdForNothing: Absurd[Nothing] = new Absurd[Nothing]{ override def absurd[X](a: Nothing): X = a }

Failed implicit resolution for Nothing with <:<

早过忘川 提交于 2020-06-17 09:11:27
问题 The following code does not compile on Scala 2.12 / 2.13. Why? class X[U, T] object X { implicit def genericX[U, T](implicit ev: T <:< U): X[U, T] = new X[U, T] } implicitly[X[AnyRef, String]] // compiles implicitly[X[String, Nothing]] // does not compile 回答1: Long story short, compiler doesn't like to infer Nothing in implicits. Why doesn't Scala's implicit class work when one of the type parameters should be Nothing? Implicit error when trying to implement the `Absurd` typeclass https://www

Failed implicit resolution for Nothing with <:<

大憨熊 提交于 2020-06-17 09:11:21
问题 The following code does not compile on Scala 2.12 / 2.13. Why? class X[U, T] object X { implicit def genericX[U, T](implicit ev: T <:< U): X[U, T] = new X[U, T] } implicitly[X[AnyRef, String]] // compiles implicitly[X[String, Nothing]] // does not compile 回答1: Long story short, compiler doesn't like to infer Nothing in implicits. Why doesn't Scala's implicit class work when one of the type parameters should be Nothing? Implicit error when trying to implement the `Absurd` typeclass https://www