implicit

implicits in scala Class constructors

為{幸葍}努か 提交于 2020-02-05 03:37:47
问题 I dont understand why only one out of the three examples below are working? What makes the other two faulty? class H(implicit a:String, b: Int) { //Working } class H(a:String, implicit b: Int) { //Not-Working } class H(implicit a:String, implicit b: Int) { //Not-Working } 回答1: In the first case implicit doesn't refer to a but to the entire parameter list. It means " a and b can be provided implicitly when calling the constructor" (and also makes them available as implicits in the class body).

Why does Scala fail to find a secondary implicit value in this one particular case?

假装没事ソ 提交于 2020-02-01 03:05:07
问题 I am having a hard time explaining the difference in behavior between additional implicit values sought by either a primary implicit value or an implicit conversion. Specifically, this works: trait Foo[A] implicit def fooString: Foo[String] = null implicit def value[A](implicit foo: Foo[A]) = 5 > implicitly[Int] 5 But this does not: implicit def conversion[A](x: Int)(implicit foo: Foo[A]) = new { def aMethod = 5 } > 1.aMethod could not find implicit value for parameter foo: test.Foo[A]

Could not find implicit value inside companion object in Scala Worksheet

眉间皱痕 提交于 2020-01-30 09:25:07
问题 I am trying to create a type class inside IntelliJ Scala Worksheet. So I started with trait like this trait Show[A] { def show(a : A) : String } and created a companion object object Show { def show[A](a: A)(implicit sh: Show[A]) = sh.show(a) implicit val intCanShow: Show[Int] = new Show[Int] { def show(int: Int): String = s"int $int" } } When I try println(Show.show(20)) I get this error. Error:(50, 26) could not find implicit value for parameter sh: Show[Int] println(Show.show(20)) But when

Why is this implicit ambiguity behaviour happening?

旧城冷巷雨未停 提交于 2020-01-30 05:46:09
问题 I have a typeclass Search , which has an instance Search[A] if we have a TypeClass1[A] or a TypeClass2[A] instance. With preference given to the 1 instance. The following compiles: trait TypeClass1[A] trait TypeClass2[A] trait Search[A] object Search extends LPSearch { implicit def case1[A](implicit ev: TypeClass1[A]): Search[A] = null } trait LPSearch { implicit def case2[A](implicit ev: TypeClass2[A]): Search[A] = null } object Test { implicit val ev1: TypeClass1[Int] = null implicit val

Why is this implicit ambiguity behaviour happening?

落花浮王杯 提交于 2020-01-30 05:46:05
问题 I have a typeclass Search , which has an instance Search[A] if we have a TypeClass1[A] or a TypeClass2[A] instance. With preference given to the 1 instance. The following compiles: trait TypeClass1[A] trait TypeClass2[A] trait Search[A] object Search extends LPSearch { implicit def case1[A](implicit ev: TypeClass1[A]): Search[A] = null } trait LPSearch { implicit def case2[A](implicit ev: TypeClass2[A]): Search[A] = null } object Test { implicit val ev1: TypeClass1[Int] = null implicit val

Good Practice of Using Implicits in Scala [closed]

扶醉桌前 提交于 2020-01-24 09:07:26
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 months ago . Are there any objective reasons to / not to use implicit parameters everywhere in code just to shorten the number of arguments that should be passed I have a REST API. Each call of an end-point has a unique id that should be passed literally in EVERY function that is being

Implicit parameter and function

谁说胖子不能爱 提交于 2020-01-14 07:27:47
问题 I have a problem considering implicit parameters in Haskell (GHC). I have a function f , that assumes the implicit parameter x , and would like to encapsulate it in a context by applying f to g f :: (?x :: Int) => Int -> Int f n = n + ?x g :: (Int -> Int) -> (Int -> Int) g t = let ?x = 5 in t But when i try to evaluate g f 10 I get an error that x is not bound, e.g.: Unbound implicit parameter (?x::Int) arising from a use of `f' In the first argument of `g', namely `f' In the second argument

How can I locate where an implicit comes from in Scala?

纵饮孤独 提交于 2020-01-13 00:07:01
问题 Short question: Is there a way to ask the scala compiler to tell me where a certain implicit used at a given point in a program was declared ? If not, is there an algorithm that I can follow manually to find out myself where an implicit was declared ? Long question: I am following simple spray crud tutorial. In the code snippet below (coming the this repo for the tutorial): pathEnd { post { entity(as[Question]) { question => completeWithLocationHeader( resourceId = questionService

scala - Confusing “diverging implicit expansion” error when using “sortBy”

一曲冷凌霜 提交于 2020-01-12 14:50:27
问题 I wonder why List(3,2,1).toIndexedSeq.sortBy(x=>x) doesn't work: scala> List(3,2,1).toIndexedSeq.sortBy(x=>x) // Wrong <console>:8: error: missing parameter type List(3,2,1).toIndexedSeq.sortBy(x=>x) ^ <console>:8: error: diverging implicit expansion for type scala.math.Ordering[B] starting with method Tuple9 in object Ordering List(3,2,1).toIndexedSeq.sortBy(x=>x) ^ scala> Vector(3,2,1).sortBy(x=>x) // OK res: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3) scala> Vector(3,2,1)

Using a context bound in a class type parameter

戏子无情 提交于 2020-01-11 09:15:02
问题 I was under the impression that context bounds would work only on methods: trait Target[T] class Post { def pinTo[T : Target](t:T) } apparently context bounds can be used in class (and presumably trait ) too: trait Target[T] class Post[T:Target] { def pintTo[T](t:T) } Now I'm confused as to how the evidence can be provided to Post ? class Business implicit object ev extends Target[Business] // is implicit necessary here ? val p = new Post[Business] // ?? how do I provide ev ? related to