implicit

Setting abstract type based on typeclass

我的梦境 提交于 2021-02-05 08:12:34
问题 I have an example like this: abstract class IsBaseTC[A] { type Self } abstract class JustHoldsTypeMember[A] extends IsBaseTC[A] implicit val doubleHoldsTypeMember = new JustHoldsTypeMember[Double] { type Self = Double } abstract class IsActualTC[A, T](implicit val aIsBaseTc: IsBaseTC[T]) extends IsBaseTC { type Self = A def get(self: A): aIsBaseTc.Self } case class Container[T]( get: T ) implicit val containerOfDoubleIsActual = new IsActualTC[Container[Double], Double] { def get(self: Self) =

Setting abstract type based on typeclass

寵の児 提交于 2021-02-05 08:11:43
问题 I have an example like this: abstract class IsBaseTC[A] { type Self } abstract class JustHoldsTypeMember[A] extends IsBaseTC[A] implicit val doubleHoldsTypeMember = new JustHoldsTypeMember[Double] { type Self = Double } abstract class IsActualTC[A, T](implicit val aIsBaseTc: IsBaseTC[T]) extends IsBaseTC { type Self = A def get(self: A): aIsBaseTc.Self } case class Container[T]( get: T ) implicit val containerOfDoubleIsActual = new IsActualTC[Container[Double], Double] { def get(self: Self) =

Is there anyway, in Scala, to get the Singleton type of something from the more general type?

亡梦爱人 提交于 2021-02-04 08:35:24
问题 I have a situation where I'm trying to use implicit resolution on a singleton type. This works perfectly fine if I know that singleton type at compile time: object Main { type SS = String with Singleton trait Entry[S <: SS] { type out val value: out } implicit val e1 = new Entry["S"] { type out = Int val value = 3 } implicit val e2 = new Entry["T"] { type out = String val value = "ABC" } def resolve[X <: SS](s: X)(implicit x: Entry[X]): x.value.type = { x.value } def main(args: Array[String])

Difference between [A: C] and [A[_]: C] context bounds

眉间皱痕 提交于 2021-01-29 22:00:38
问题 I'm a newbie, according to my lectures : class Test [T: Comparing] means that it requires an implicit value of type Comparing[T] that can be used in the methods of that class. With this Higher kinded type notation Question : What does this expression def notation[F[_]: Sync] : F[Unit] = ??? refer to ? 回答1: Consider the difference between concrete type and type constructor Int // concrete type List[Int] // concrete type List // type constructor We represent the shape of the type constructor

How to resolve implicit lookup by bounded generic?

◇◆丶佛笑我妖孽 提交于 2021-01-29 20:39:34
问题 I have a series of class Foo: trait Foo class Foo1 extends Foo class Foo2 extends Foo //... and I have a type class and instances for all of the Foos: trait CanBar[T] { def bar: Unit } implicit val foo1: CanBar[Foo1] = null implicit val foo2: CanBar[Foo2] = null and I try to get the type class instance from a method: def bar[T <: Foo](foo: T) = { val canBar = implicitly[CanBar[T]] //... } The compiler complains No implicits found for parameter e: CanBar[T] , even though I imported all the

How to overload generic method with different evidence without ambiguity?

浪尽此生 提交于 2021-01-29 17:38:50
问题 I have a customized compare methods that takes two parameters. One of them are expected to be implicitly convertible to another: object Test extends App { def compare[T1, T2](a: T1, b: T2)(implicit ev: T1 => T2) = compareImpl[T2](ev(a), b) def compare[T1, T2](a: T1, b: T2)(implicit ev: T2 => T1) = compareImpl[T1](a, ev(b)) def compareImpl[T](a: T, b: T) = a == b case class Foo(s: String) case class Bar(s: String) implicit def foo2bar(f: Foo): Bar = Bar(f.s) println(compare(Foo("hello"), Bar(

In scala, what is the easiest way to chain functions defined with a type class and of which output type depends on it?

断了今生、忘了曾经 提交于 2021-01-29 16:42:34
问题 Assuming that a class Thing is defined, and an operation + is associated with a type class: trait TypeClass[X, Y] { type Out def v: Out } object TypeClass { implicit def summon[X <: Int, Y <: Int]: TypeClass[X, Y] = new TypeClass[X, Y] { type Out = Int override def v: Out = 2 } } case class Thing[X]() { def +[Y](that: Thing[Y])(implicit typeClass: TypeClass[X, Y]): typeClass.Out = typeClass.v } Now if I want to define a shortcut function +2x , which represents X + Y + Y . My first instinct

Implicit conversion for multiple parameters

青春壹個敷衍的年華 提交于 2021-01-29 15:07:10
问题 Is it possible to implement in Scala an implicit conversion for the group of parameters (without defining them as some class member) like implicit def triple2One (x :Int, s :String, d :Double) = x // just as an example So that I would be able to call it in the code like val x :Int = (1, "test", 2.0) 回答1: It is possible: scala> implicit def iFromISD(isd: (Int, String, Double)): Int = isd._1 iFromISD: (isd: (Int, String, Double))Int scala> val x: Int = (1, "two", 3.0) x: Int = 1 Naturally,

Why can't the compiler select the correct String.contains method when using this lambda shorthand?

牧云@^-^@ 提交于 2021-01-29 14:30:00
问题 Say I want to check if a string contains any of the letters in "cory": def hasCory(input: String): Boolean = { val myName = "cory" input.exists(myName.contains) } The compiler complains with: error: type mismatch; found : CharSequence => Boolean required: Char => Boolean Scala provides the Char -accepting method I want in StringOps: But it appears that the compiler cannot see this method unless I change the code to one of: input.exists(myName.contains(_)) input.exists(c => myName.contains(c))

Scala compiler expand types

时光毁灭记忆、已成空白 提交于 2021-01-29 12:23:07
问题 Consider this code: trait TypeOr[E, F] { type T } implicit def noneq2[E, F](implicit ev: E =!= F): TypeOr[E, F] = new TypeOr[E, F] { type T = (E, F) } sealed trait Error[+E, +A] case class Err[E, A](e: Error[E, A]) { def combine[B, F](f: A => Error[F, B])(implicit ev: TypeOr[E, F]): Error[ev.T, B] = ??? } val result = Err(null.asInstanceOf[Error[Int, Int]]).combine(_ => null.asInstanceOf[Error[String, String]]) So far so good. From the definitions above, I concluded, that the expanded type of