implicits

Why does Scala implicit resolution fail for overloaded method with type parameter?

送分小仙女□ 提交于 2019-11-30 14:02:53
The first example successfully finds the implicit conversion to the method foo(String) , however as soon as I add a type parameter (see fails ) the compiles doesn't resolve it anymore: object works { class A { def foo(): String = ??? } implicit class PimpedA(a: A) { def foo(i: String): String = ??? } val a = new A() a.foo("test") //compiles } object fails { //same as `works`, but adds type parameter class A { def foo[T](): String = ??? } implicit class PimpedA(a: A) { def foo[T](i: String): String = ??? } val a = new A() PimpedA(a).foo("test") // compiles a.foo("test") // error: too many

Why does Scala implicit resolution fail for overloaded method with type parameter?

青春壹個敷衍的年華 提交于 2019-11-29 19:29:12
问题 The first example successfully finds the implicit conversion to the method foo(String) , however as soon as I add a type parameter (see fails ) the compiles doesn't resolve it anymore: object works { class A { def foo(): String = ??? } implicit class PimpedA(a: A) { def foo(i: String): String = ??? } val a = new A() a.foo("test") //compiles } object fails { //same as `works`, but adds type parameter class A { def foo[T](): String = ??? } implicit class PimpedA(a: A) { def foo[T](i: String):

“can't existentially abstract over parameterized type…”

£可爱£侵袭症+ 提交于 2019-11-29 09:24:12
I was messing around with Scala 2.8 for fun and trying to define a pimp which adds an "as" method to type constructors, allowing to convert from one functor to another (please overlook the fact that I'm not necessarily dealing with functors here). So for instance, you could use it like this: val array:Array[T] val list:List[T] = array.as[List] So here's what I tried to do: object Test { abstract class NatTrans[F[_], G[_]] { def convert[T](f:F[T]):G[T] } implicit def array2List:NatTrans[Array, List] = new NatTrans[Array, List] { def convert[T](a:Array[T]) = a.toList } // this next part gets

What is a diverging implicit expansion error?

放肆的年华 提交于 2019-11-28 21:02:45
While trying to find a solution to another question ( [1] ) I came across a diverging implicit expansion error. I'm looking for an explanation about what this means Here's the use case: scala> implicit def ordering[T](implicit conv: T => Ordered[T], res: Ordering[Ordered[T]]) = Ordering.by(conv) ordering: [T](implicit conv: (T) => Ordered[T],implicit res: Ordering[Ordered[T]])scala.math.Ordering[T] scala> def foo[T <% Ordered[T]](s : Seq[T]) = s.sorted <console>:6: error: diverging implicit expansion for type Ordering[T] starting with method ordering in object $iw def foo[T <% Ordered[T]](s :

“can't existentially abstract over parameterized type…”

岁酱吖の 提交于 2019-11-28 02:46:41
问题 I was messing around with Scala 2.8 for fun and trying to define a pimp which adds an "as" method to type constructors, allowing to convert from one functor to another (please overlook the fact that I'm not necessarily dealing with functors here). So for instance, you could use it like this: val array:Array[T] val list:List[T] = array.as[List] So here's what I tried to do: object Test { abstract class NatTrans[F[_], G[_]] { def convert[T](f:F[T]):G[T] } implicit def array2List:NatTrans[Array,

Inferring type of generic implicit parameter from return type

廉价感情. 提交于 2019-11-28 00:41:13
问题 Say I have a simple class like this abstract class Foo { implicit val impInt: Int = 42 def f[A]()(implicit a: A): A val f2: Int = f() } When declaring val f2 , compiler is able to infer that the type of implicit parameter of function f is Int because that type is the same as the result type, and result type needs to match the type of value f2 , which is Int . However, throwing an Ordering[A] into the mix: def f[A]()(implicit a: A, m: Ordering[A]): A val f2: Int = f() results in this compile

Can't prove that singleton types are singleton types while generating type class instance

冷暖自知 提交于 2019-11-27 07:03:22
Suppose I've got a type class that proves that all the types in a Shapeless coproduct are singleton types: import shapeless._ trait AllSingletons[A, C <: Coproduct] { def values: List[A] } object AllSingletons { implicit def cnilSingletons[A]: AllSingletons[A, CNil] = new AllSingletons[A, CNil] { def values = Nil } implicit def coproductSingletons[A, H <: A, T <: Coproduct](implicit tsc: AllSingletons[A, T], witness: Witness.Aux[H] ): AllSingletons[A, H :+: T] = new AllSingletons[A, H :+: T] { def values = witness.value :: tsc.values } } We can show that it works with a simple ADT: sealed

What is the Scala identifier “implicitly”?

妖精的绣舞 提交于 2019-11-26 19:16:44
I have seen a function named implicitly used in Scala examples. What is it, and how is it used? Example here : scala> sealed trait Foo[T] { def apply(list : List[T]) : Unit }; object Foo { | implicit def stringImpl = new Foo[String] { | def apply(list : List[String]) = println("String") | } | implicit def intImpl = new Foo[Int] { | def apply(list : List[Int]) = println("Int") | } | } ; def foo[A : Foo](x : List[A]) = implicitly[Foo[A]].apply(x) defined trait Foo defined module Foo foo: [A](x: List[A])(implicit evidence$1: Foo[A])Unit scala> foo(1) <console>:8: error: type mismatch; found : Int

What is the Scala identifier “implicitly”?

我怕爱的太早我们不能终老 提交于 2019-11-26 06:09:35
问题 I have seen a function named implicitly used in Scala examples. What is it, and how is it used? Example here: scala> sealed trait Foo[T] { def apply(list : List[T]) : Unit }; object Foo { | implicit def stringImpl = new Foo[String] { | def apply(list : List[String]) = println(\"String\") | } | implicit def intImpl = new Foo[Int] { | def apply(list : List[Int]) = println(\"Int\") | } | } ; def foo[A : Foo](x : List[A]) = implicitly[Foo[A]].apply(x) defined trait Foo defined module Foo foo: [A]

What are Scala context and view bounds?

扶醉桌前 提交于 2019-11-25 22:36:18
问题 In a simple way, what are context and view bounds and what is the difference between them? Some easy-to-follow examples would be great too! 回答1: I thought this was asked already, but, if so, the question isn't apparent in the "related" bar. So, here it is: What is a View Bound? A view bound was a mechanism introduced in Scala to enable the use of some type A as if it were some type B . The typical syntax is this: def f[A <% B](a: A) = a.bMethod In other words, A should have an implicit