implicit

Finding the second matching implicit

☆樱花仙子☆ 提交于 2021-02-10 13:25:40
问题 Consider the following setup: trait Foo[A] object Foo extends Priority2 trait Priority0 { implicit def foo1: Foo[Int] = new Foo[Int] {} } trait Priority1 extends Priority0 { implicit def foo2: Foo[Boolean] = new Foo[Boolean] {} } trait Priority2 extends Priority1 { implicit def foo3: Foo[Double] = new Foo[Double] {} } Now, in a REPL (having loaded the above code up), I can do the following: scala> def implicitlyFoo[A](implicit foo: Foo[A]) = foo implicitlyFoo: [A](implicit foo: Foo[A])Foo[A]

what should a scala implicit macro have to return to tell the compiler “forget my result, continue your search”

不羁的心 提交于 2021-02-10 12:42:39
问题 I have an implicit macro with a greedy signature implicit def materializeHelper[C <: Any]: Helper[C] = macro materializeHelperImpl[C] def materializeHelperImpl[C <: Any: ctx.WeakTypeTag](ctx: blackbox.Context): ctx.Expr[Helper[C]] = ??? According to it's signature it would materialize a Helper[C] for any C. But the body is much more picky. It only accepts C s which are sealed traits. What should the macro return to tell the compiler "forget my result, continue your implicit search as if I

How to avoid calling asInstanceOf in Scala

Deadly 提交于 2021-02-08 14:08:52
问题 Here is a simplified version of my code. How can I avoid to call asInstanceOf (because it is a smell for a poorly design solution) ? sealed trait Location final case class Single(bucket: String) extends Location final case class Multi(buckets: Seq[String]) extends Location @SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf")) class Log[L <: Location](location: L, path: String) { // I prefer composition over inheritance // I don't want to pass location to this method because it's a

Dealing with implicit typeclass conflict

牧云@^-^@ 提交于 2021-02-08 08:58:20
问题 I'm trying to deal with an ambiguous implicits problem, and (relatedly) figure out what best practise should be for parameterizing typeclasses. I have a situation where I am using a typeclass to implement a polymorphic method. I initially tried the approach below: abstract class IsValidTypeForContainer[A] object IsValidTypeForContainer { implicit val IntIsValid = new IsValidTypeForContainer[Int] {} implicit val DoubleIsValid = new IsValidTypeForContainer[Double] {} } abstract class

Scala's Nothing vs partial unification

て烟熏妆下的殇ゞ 提交于 2021-02-08 06:38:34
问题 I would expect the following code to compile just fine: trait Widen[M[_]] { def widen[A, B >: A](ma: M[A]): M[B] } object Widen { implicit class Ops[M[_], A](ma: M[A]) { def widen[B >: A](implicit ev: Widen[M]): M[B] = ev.widen[A, B](ma) } // implicit class OpsNothing[M[_]](ma: M[Nothing]) { // def widen[B](implicit ev: Widen[M]): M[B] = ev.widen(ma) // } implicit val WidenList = new Widen[List] { def widen[A, B >: A](l: List[A]): List[B] = l } } import Widen._ List.empty[Some[Int]].widen

In Scala, why it is impossible to infer TypeTag from type alias or dependent type?

假如想象 提交于 2021-02-08 03:30:39
问题 I have a simple scala program to test the Capability of Scala to infer type classes: import scala.reflect.ClassTag object InferTypeTag { import org.apache.spark.sql.catalyst.ScalaReflection.universe._ def infer(): Unit = { type U = (Int, String) val ttg1 = implicitly[TypeTag[(Int, String)]] val ttg2 = implicitly[TypeTag[U]] val ctg = implicitly[ClassTag[U]] } } ttg1 got inferred without problem. ttg2 triggered the following compilation error: Error:(14, 26) No TypeTag available for U val ttg2

Initialize a type variable with dynamic / concrete type

∥☆過路亽.° 提交于 2021-02-07 20:55:14
问题 I am learning Scala and I was trying to create a type class to solve the "Every animal eats food, but the type of food depends on the animal" problem. I have an Eats type class with context bounds: trait Eats[A <: Animal, B <: Edible] object Eats { def apply[A, B]: Eats[A, B] = new Eats[A, B] {} } with both Animal and Edible being abstract classes. The (reduced) Animal interface looks something like this abstract class Animal { type This // concrete type def eat[A <: Edible](food: A)(implicit

Initialize a type variable with dynamic / concrete type

故事扮演 提交于 2021-02-07 20:54:04
问题 I am learning Scala and I was trying to create a type class to solve the "Every animal eats food, but the type of food depends on the animal" problem. I have an Eats type class with context bounds: trait Eats[A <: Animal, B <: Edible] object Eats { def apply[A, B]: Eats[A, B] = new Eats[A, B] {} } with both Animal and Edible being abstract classes. The (reduced) Animal interface looks something like this abstract class Animal { type This // concrete type def eat[A <: Edible](food: A)(implicit

Why prefer implicit val over implicit object

时光毁灭记忆、已成空白 提交于 2021-02-07 04:14:51
问题 When asking questions about implicits a common suggestion / recommendation / advice that is given together with the answer (or sometimes that is the answer itself) is to use implicit vals with excplicit type signatures instead of using implicit objects . But, what is the reason behind that? 回答1: "TL;DR;" The reason is that an implici val with an explicit type signature has the exact type you want whereas an implicit object has a different type. The best way to show why that could be a problem

Why prefer implicit val over implicit object

回眸只為那壹抹淺笑 提交于 2021-02-07 04:12:30
问题 When asking questions about implicits a common suggestion / recommendation / advice that is given together with the answer (or sometimes that is the answer itself) is to use implicit vals with excplicit type signatures instead of using implicit objects . But, what is the reason behind that? 回答1: "TL;DR;" The reason is that an implici val with an explicit type signature has the exact type you want whereas an implicit object has a different type. The best way to show why that could be a problem