implicit

Implementing a typeclass using type parameters versus abstract types

假如想象 提交于 2021-01-29 10:51:22
问题 Following on from Witness that an abstract type implements a typeclass I've tried to compare these two approaches side-by-side in the code snippet below: // We want both ParamaterizedTC and WithAbstractTC (below) to check that // their B parameter implements AddQuotes abstract class AddQuotes[A] { def inQuotes(self: A): String = s"${self.toString}" } implicit val intAddQuotes = new AddQuotes[Int] {} abstract class ParamaterizedTC[A, _B](implicit ev: AddQuotes[_B]) { type B = _B def getB(self:

How to take advantage of mkNumericOps in Scala?

不打扰是莪最后的温柔 提交于 2021-01-29 09:51:50
问题 I'm trying to define a new type that can behave essentially like a number (for concreteness, let's say a Double ). I'd like to overload operators on this type and I could do this explicitly, but to avoid repetition, I would like to advantage of the methods in NumericOps, which are defined in terms of the abstract methods in Numeric. My understanding is I should be able to just override the methods in Numeric and get the others for free. Here's the simplest attempt I can think of: class

Extending an object with a trait which needs implicit member

拥有回忆 提交于 2021-01-29 06:17:51
问题 I'm trying to have a code like below: object MetaData extends CacheParams{} So, since CacheParams needs implicit val p:Parameters , I tried: object MetaData (implicit val p: Parameters) extends CacheParams But it seems that I can't pass arguments to an object. ( because it gives error: traits or objects may not have parameters ) And if I don't pass any arguments it will give compile error that: [error]: object creation impossible, since value p in trait CacheParams of type Parameters is not

Why is this implicit resolution failing?

无人久伴 提交于 2021-01-28 11:26:38
问题 I have an implicit conversion - below - which feels like it should definitely be working but is definitely not. Can anyone shed any light? I know implicitly can sometimes fail when type refinements are used - is that the issue here? trait GetItem[A[_], T, R] { type Out def ret(a: A[T], ref: R): Out } object GetItem { implicit def ifRefIsInt[A[_], T]: GetItem[A, T, Int] { type Out = A[T] } = new GetItem[A, T, Int] { type Out = A[T] def ret(a: A[T], ref: Int): Out = a } } import GetItem._ /

generic collection generation with a generic type

别来无恙 提交于 2021-01-28 04:24:45
问题 Sometimes, I find myself wishing scala collections to include some missing functionality, and it's rather easy "extending" a collection, and provide a custom method. This is a bit more difficult when it comes to building the collection from scratch. Consider useful methods such as .iterate . I'll demonstrate the usecase with a similar, familiar function: unfold . unfold is a method to construct a collection from an initial state z: S , and a function to generate an optional tuple of the next

How to make a typeclass works with an heterogenous List in scala

久未见 提交于 2021-01-27 19:00:40
问题 Given the following typeclass and some instances for common types trait Encoder[A] { def encode(a: A): String } object Encoder { implicit val stringEncoder = new Encoder[String] { override def encode(a: String): String = a } implicit val intEncoder = new Encoder[Int] { override def encode(a: Int): String = String.valueOf(a) } implicit def listEncoder[A: Encoder] = new Encoder[List[A]] { override def encode(a: List[A]): String = { val encoder = implicitly[Encoder[A]] a.map(encoder.encode)

Reverse HList and convert to class?

喜你入骨 提交于 2021-01-27 11:33:12
问题 I'm using Shapeless to accumulate materialized values in Akka as an HList and convert that to a case class. (You don't have to know Akka much for this question, but the default approach accumulates materialized values as recursively nested 2-tuples, which isn't much fun, so Shapeless HLists seemed a more sensible approach -- and works pretty well. But I don't know how to properly re-use that approach. Here, I'll simplify the kinds of values Akka produces.) For example, let's say we've got two

Scala implicit def do not work if the def name is toString

[亡魂溺海] 提交于 2021-01-27 06:34:56
问题 This code fails to compile: object Foo { implicit def toString(i: Int): String = i.toString def foo(x: String) = println(x) foo(23) } Above code fails to compile with following error: error: type mismatch; found : scala.this.Int(23) required: String foo(23) But, this code compiles object Foo { implicit def asString(i: Int): String = i.toString def foo(x: String) = println(x) foo(23) } Why does the name of an implicit def should matter? Note: If the method is named equals , it also does not

Scala implicit def do not work if the def name is toString

让人想犯罪 __ 提交于 2021-01-27 06:34:14
问题 This code fails to compile: object Foo { implicit def toString(i: Int): String = i.toString def foo(x: String) = println(x) foo(23) } Above code fails to compile with following error: error: type mismatch; found : scala.this.Int(23) required: String foo(23) But, this code compiles object Foo { implicit def asString(i: Int): String = i.toString def foo(x: String) = println(x) foo(23) } Why does the name of an implicit def should matter? Note: If the method is named equals , it also does not

starting android service using explicit vs implicit intent

流过昼夜 提交于 2021-01-21 04:01:37
问题 According to the standard Android documentation, the prefered way to start a service (started service that is) is to use an explicit intent like this: // Using explicit intent: Intent serviceIntent = new Intent(getApplicationContext(), MyService.class); // or: Intent serviceIntent = new Intent(this, MyService.class); startService(serviceIntent); You can also start/stop a service using an implicit intent with an action string specified in the manifest, like this: // Using implicit intent: