implicit

Why in-scope implicit values typed A and B are not ambiguous when B extends A?

不羁的心 提交于 2019-12-10 19:22:47
问题 Why does the code in Test2 compile even though we clearly have ambiguous implicit values? object Method { def foo(implicit i: A): Unit = println(i.i) } trait A { val i: Int } class B(override val i: Int) extends A object Test1 { implicit val i1: A = new A { val i: Int = 20 } } object Test2 { implicit val i2: B = new B(10) import Test1._ // This compiles fine and prints 10 Method.foo } object Test3 { implicit val i2: A = new B(10) import Test1._ // This does not compile, get `ambiguous

Could not find implicit value inside singleton object

落花浮王杯 提交于 2019-12-10 19:07:28
问题 I have this code: trait Context { implicit val e: Encoder trait Encoder { def write(): Unit = { println("Test") } } } trait AsyncEncoders { this: Context => class AsyncEncoder extends Encoder { } implicit val e = new AsyncEncoder() } class ConcreteContext extends Context with AsyncEncoders { } When I use it like this ( case 1 ): object Main extends App { implicit val c = new ConcreteContext() import c._ implicitly[Encoder].write() } then it compiles and prints Test . But when I try to call

What is implicit evidence in Scala? What is it good for?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 14:57:29
问题 I see the term "implicit evidence" in many SOF and blog posts, related to runtime retention of type information. I searched the net but have not found any simple explanation what "implicit evidence" is. This concept comes up, for example, here. EDIT: A comment to Travis' nice answer: From Predef.scala: sealed abstract class <:<[-From, +To] extends (From => To) with Serializable private[this] final val singleton_<:< = new <:<[Any,Any] { def apply(x: Any): Any = x } implicit def conforms[A]: A

What is the meaning of a type declaration without definition in an object?

蹲街弑〆低调 提交于 2019-12-10 14:55:53
问题 Scala allows to define types using the type keyword, which usually have slightly different meaning and purpose depending on when they are declared. If you use type inside an object or a package object, you'd define a type alias, i.e. a shorter/clearer name for another type: package object whatever { type IntPredicate = Int => Boolean def checkZero(p: IntPredicate): Boolean = p(0) } Types declared in classes/traits are usually intended to be overridden in subclasses/subtraits, and are also

How do I use “implicit” as apply() parameter?

╄→尐↘猪︶ㄣ 提交于 2019-12-10 13:45:18
问题 I want to do this: abstract class Context { def getInt(id: Int): Int } abstract class Dependency[+T] (val name: String, val id: Int) extends Function1[Context,T] class IntDependency(name: String, id: Int) extends Dependency[Int](name, id) { def apply(implicit context: Context): Int = context.getInt(id) } But then I get an error message like this: class IntDependency needs to be abstract, since method apply in trait Function1 of type (v1: Context)Long is not defined (Note that T1 does not

scala implicit method with multiple arguments

落爺英雄遲暮 提交于 2019-12-10 13:10:03
问题 In a comment for SIP-13 Martin Odersky implied that it is possible to create an implicit method with multiple arguments. According to my experiences, implicit methods always have exactly one argument and I cannot imagine how an implicit method with multiple arguments can be used. Can someone give some use case and explanation? 回答1: For example if you need an implicit parameter of a function type: implicit def foo(x: Int, y: Int) = y * x def bar(x: Int, y: Int)(implicit f: (Int, Int) => Int) =

Scala - How can I exclude my function's generic type until use?

血红的双手。 提交于 2019-12-10 11:44:03
问题 I have a map of String to Function s which details all of the valid functions that are in a language. When I add a function to my map, I am required to specify the type (in this case Int ). var functionMap: Map[String, (Nothing) => Any] = Map[String, (Nothing) => Any]() functionMap += ("Neg" -> expr_neg[Int]) def expr_neg[T: Numeric](value: T)(implicit n: Numeric[T]): T = { n.negate(value) } Instead, how can I do something like: functionMap += ("Neg" -> expr_neg) without the [Int] and add it

implicit conversions that add properties to a type, rather than to an instance of a type

时间秒杀一切 提交于 2019-12-10 11:06:30
问题 I was reading through some older Scala posts to better understand type classes, and I ran across this one that seemed quite useful, but the example seems to have gotten stale. Can someone help me figure out the correct way to do what Phillipe intended ? Here is the code trait Default[T] { def value : T } implicit object DefaultInt extends Default[Int] { def value = 42 } implicit def listsHaveDefault[T : Default] = new Default[List[T]] { def value = implicitly[Default[T]].value :: Nil }

transitive implicits - is this possible in Scala?

橙三吉。 提交于 2019-12-10 10:28:53
问题 Let's say I have several functions: func1 : A => B func2: B => C func3: C => D I would like to orchestrate now functions when needed in a generic fashion. let's say if i need a conversion from A to B I'd call func1 . But when I need a conversion from A to D I would like to have a composition of those functions. Is such thing possible in a dynamic notion? 回答1: From the Scala documentation for language features, explaining why implicit conversions have to be explicitly enabled in 2.10: Why

Translate/encode Haskell's `data Obj = forall a. (Show a) => Obj a` in Scala

喜你入骨 提交于 2019-12-09 23:22:09
问题 I've not been able to come up with how to encode Obj in Scala: {-# LANGUAGE ExistentialQuantification #-} data Obj = forall a. (Show a) => Obj a instance Show Obj where show (Obj a) = "Obj " ++ show a main = print $ show [Obj "hello", Obj 3, Obj True] when run, the above produces the following output: [Obj "hello",Obj 3,Obj True] In Scala, however, this does not seem to compile: forSome { type T; implicit val ev: Show[T] } and neither does this: forSome { type T : Show[T] } Is this even