context-bound

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

Understanding Mixed Context Bounds of Seq[AnyVal] and Seq[String]

拟墨画扇 提交于 2020-06-26 14:12:21
问题 Suppose I have some function that should take a sequence of Ints or a sequence of Strings. My attempt: object Example extends App { import scala.util.Random val rand: Random.type = scala.util.Random // raw data val x = Seq(1, 2, 3, 4, 5).map(e => e + rand.nextDouble()) val y = Seq("chc", "asas") def f1[T <: AnyVal](seq: Seq[T]) = { println(seq(0)) } // this works fine as expected f1(x) // how can i combine f1(y) } How can I add this to also work with strings? If I change the method signature

NPE in spray-json because of recursive implicits (context bound issue?)

末鹿安然 提交于 2020-01-04 05:22:09
问题 Perhaps I discovered a bug in spray-json. I get Null Pointer Exception when I'm trying to get json of an object that has field of type of itself. Example is: case class TestItem(subitems: Option[List[TestItem]]) object MyJsonProtocol extends DefaultJsonProtocol { implicit val testItemFormat: RootJsonFormat[TestItem] = jsonFormat(TestItem, "subitems") } import MyJsonProtocol._ object TestNPE { def main(args: Array[String]) { val subitems = List(TestItem(None)) val item: TestItem = TestItem

How can I apply a macro annotation to a case class with a context bound?

时间秒杀一切 提交于 2020-01-03 13:58:06
问题 When I try to add a macro annotation to my case class: @macid case class CC[A: T](val x: A) I get the error: private[this] not allowed for case class parameters @macid is just the identity function, defined as a whitebox StaticAnnotation: import scala.language.experimental.macros import scala.reflect.macros.whitebox.Context import scala.annotation.StaticAnnotation class macid extends StaticAnnotation { def macroTransform(annottees: Any*): Any = macro macidMacro.impl } object macidMacro { def

How can I apply a macro annotation to a case class with a context bound?

拈花ヽ惹草 提交于 2020-01-03 13:57:06
问题 When I try to add a macro annotation to my case class: @macid case class CC[A: T](val x: A) I get the error: private[this] not allowed for case class parameters @macid is just the identity function, defined as a whitebox StaticAnnotation: import scala.language.experimental.macros import scala.reflect.macros.whitebox.Context import scala.annotation.StaticAnnotation class macid extends StaticAnnotation { def macroTransform(annottees: Any*): Any = macro macidMacro.impl } object macidMacro { def