implicits

How can I chain implicits in Scala?

落花浮王杯 提交于 2019-12-16 23:37:10
问题 The pimp-my-library pattern allows me to seemingly add a method to a class by making available an implicit conversion from that class to one that implements the method. Scala does not allow two such implicit conversions taking place, however, so I cannot got from A to C using an implicit A to B and another implicit B to C . Is there a way around this restriction? 回答1: Scala has a restriction on automatic conversions to add a method, which is that it won't apply more than one conversion in

Context bounds with two generic parameters

自古美人都是妖i 提交于 2019-12-14 00:48:45
问题 In Scala, I can use context bounds: def sort[T : Ordered](t: Seq[T]) To mean the same thing as: def sort[T](t: Seq[T])(implicit def Ordered[T]) What if I have a class with two generic parameters. I.e. I want to be able to ensure that I have a Writer[T, String] . Is there a syntax where I can use context bounds ( T : ... ) or do I need to have the implicit explicitly (that was fun to write). 回答1: Yes, it's possible! But not really very pretty: trait Writer[T, O] { def write(t: T): O } def

Implicit parameters break type inference or inference does not suffice for their resolution?

百般思念 提交于 2019-12-11 11:36:44
问题 Type inference works fine in this example until I add the implicit ordering evidence. Type inference rules (from left to right & across parameter lists) seem to be satisfied, but there is something in regards to the implicit that breaks it. case class Insert[I, O : Ordering](arg: I) def execute[I,O](req: Insert[I,O]): O = null.asInstanceOf[O] val result: Int = execute(Insert("test")) Error:(5, 39) diverging implicit expansion for type Ordering[O] starting with method Tuple9 in object Ordering

Safely chaining implicit conversions

﹥>﹥吖頭↗ 提交于 2019-12-11 04:22:47
问题 You can do this to get implicit conversions to chain: package language object chainedImplicits { implicit def chainImplicits[A, B, C](a: A)(implicit conv1: A => B, conv2: B => C): C = conv2(conv1(a)) } but this is obviously not safe. I can't see anything wrong with this version, though: package language package chainedImplicits { final class Chained[A, B] private[chainedImplicits] (val f: A => B) trait Low { this: `package`.type => implicit def startChaining(implicit conv: A => B): Chained[A,

Powerset of an HList of Options

落花浮王杯 提交于 2019-12-11 01:39:46
问题 I am playing around with Shapeless and I am trying to compute the (kind-of) powerset of an HList of Option s. Basically, I want to interpret the HList as a set, in which case the Option value of an element tells me it belongs to the set or not. For example, given Some(5) :: Some("a") :: HNil , I want to obtain the following HList s: Some(5) :: Some("a") :: HNil Some(5) :: None :: HNil None :: Some("a") :: HNil None :: None :: HNil I tried the following: object test2 extends Poly2{ implicit

How to implement intermediate types for implicit methods?

ⅰ亾dé卋堺 提交于 2019-12-10 19:30:02
问题 Assume I want to offer method foo on existing type A outside of my control. As far as I know, the canonical way to do this in Scala is implementing an implicit conversion from A to some type that implements foo . Now I basically see two options. Define a separate, maybe even hidden class for the purpose: protected class Fooable(a : A) { def foo(...) = { ... } } implicit def a2fooable(a : A) = new Fooable(a) Define an anonymous class inline: implicit def a2fooable(a : A) = new { def foo(...) =

Test two scala shapeless HList types for equivalence via implicit

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 10:44:22
问题 I'm interested in testing whether two HList heterogeneous records are "equivalent"; that is, they have the same key/val pairs, but not necessarily in the same order. Is there a predefined type predicate that does what EquivHLists does in the code fragment below? // shapeless heterogeneous records with "equivalent" types. // these should compile if given as the arguments to 'f' below. val hrec1 = ("a" ->> 1) :: ("b" ->> 2) :: HNil val hrec2 = ("b" ->> 2) :: ("a" ->> 1) :: HNil // only compiles

What to do with operations for a specific kind of collection?

隐身守侯 提交于 2019-12-10 10:12:55
问题 In several different places in my application, I need to take a Seq[SalesRow] and return a Map[String,SalesRow] , where the string is the name of a country. I need to use this in several places. For example, I take a list of all SalesRows and get a global breakdown of sales by country. But in other places, I want to break down my sales by month and then by country (so Map[Month,Seq[SalesRow]] becomes Map[Month,Map[String,Seq[SalesRow]]] ) - in still other places, I want to break down by day

False errors when using cats library in IntelliJ

微笑、不失礼 提交于 2019-12-09 08:46:19
问题 I am using the cats Scala library and the IntelliJ IDE seems to be struggling with the use of implicits: Here is a simple example: import cats.std.all._ import cats.Traverse.ops._ def useSequence[A](ls : List[Option[A]]) : Option[List[A]] = { ls.sequence } In IntelliJ, this code is highlighted red. But I can build just fine using Make Project or the command line. Right now the error is: Expression of type Nothing[List[Nothing]] doesn't conform to expected type Option[List[A]] Other times the

How to express this type in Scala? Existential with type class (ie, implicit) restriction?

别说谁变了你拦得住时间么 提交于 2019-12-08 20:24:09
问题 I'm using the Play framework's JSON library, which uses a type class to implement the Json.toJson function. (I may decide to use another technique with less static typing, like reflection; but for now I want to use this library because it's helping me learn the Scala type system.) I have a bunch of simple case classes that need to be passed to toJson , so I have to implement an implicit Writes[T] object for each of them. A first cut might look like this, for each of the classes. // An example