enrich-my-library

Why doesn't Scala's implicit class work when one of the type parameters should be Nothing?

蹲街弑〆低调 提交于 2020-07-28 13:54:48
问题 Update: I modified the example so that can be compiled and tested. I have an implicit class that defines an enrichment method: case class Pipe[-I,+O,+R](f: I => (O, R)); object Pipe { // The problematic implicit class: implicit class PipeEnrich[I,O,R](val pipe: Pipe[I,O,R]) extends AnyVal { def >->[X](that: Pipe[O,X,R]): Pipe[I,X,R] = Pipe.fuse(pipe, that); def <-<[X](that: Pipe[X,I,R]): Pipe[X,O,R] = Pipe.fuse(that, pipe); } def fuse[I,O,X,R](i: Pipe[I,O,R], o: Pipe[O,X,R]): Pipe[I,X,R] =

Why doesn't Scala's implicit class work when one of the type parameters should be Nothing?

∥☆過路亽.° 提交于 2020-07-28 13:53:19
问题 Update: I modified the example so that can be compiled and tested. I have an implicit class that defines an enrichment method: case class Pipe[-I,+O,+R](f: I => (O, R)); object Pipe { // The problematic implicit class: implicit class PipeEnrich[I,O,R](val pipe: Pipe[I,O,R]) extends AnyVal { def >->[X](that: Pipe[O,X,R]): Pipe[I,X,R] = Pipe.fuse(pipe, that); def <-<[X](that: Pipe[X,I,R]): Pipe[X,O,R] = Pipe.fuse(that, pipe); } def fuse[I,O,X,R](i: Pipe[I,O,R], o: Pipe[O,X,R]): Pipe[I,X,R] =

Another Scala CanBuildFrom issue: a collection enrichment operator that wraps another of a different type

会有一股神秘感。 提交于 2019-12-11 03:48:43
问题 User Régis Jean-Gilles gracefully answered my previous question where I was struggling with CanBuildFrom and enrichment functions (aka "pimp my library" or "enrich my library"): Creating an implicit function that wraps map() in Scala with the right type: Not for the faint at heart But this time I've got an even more complicated issue. I have a function to implement variations on intersectWith , for intersecting collections by their keys. I've managed to make them work like proper collection

Option-izing Java getters

旧城冷巷雨未停 提交于 2019-12-10 21:19:13
问题 When working with Java from Scala, we have to account for null. HttpServletRequest getters (getAttribute, getHeader, etc.) for example, all potentially return null. I know I can manually do a case/match or map operation on each call to an HttpServletRequest method, but that's a bit tedious. Also, method calls like request.getHeader("Accept-Encoding") are a mouthful. I came up with an enrichment to handle both issues: class Servlet_Request_Provides_NullSafe_Getters (r: HttpServletRequest) {

What is the best way to use enrich-my-library in scala?

大城市里の小女人 提交于 2019-12-10 13:13:52
问题 The are two different way to implement it. One is more short implicit def toR1(s:String) = new { def getLength = s.length)} Second is more long class R2(s:String){def getLength2 = s.length)} implicit def toR2(s:String) = new R2(s) Which one is better? 回答1: The first version uses a structural type. It makes it possible to write short and readable code, but a disadvantage of structural types is that reflection is used at runtime when you call the method in the structural type. Calling a method

Minimal framework in Scala for collections with inheriting return type

好久不见. 提交于 2019-12-05 12:04:28
问题 Suppose one wants to build a novel generic class, Novel[A] . This class will contain lots of useful methods--perhaps it is a type of collection--and therefore you want to subclass it. But you want the methods to return the type of the subclass, not the original type. In Scala 2.8, what is the minimal amount of work one has to do so that methods of that class will return the relevant subclass, not the original? For example, class Novel[A] /* What goes here? */ { /* Must you have stuff here? */

Mixing in generic traits in parameterized classes without duplicating type parameters

♀尐吖头ヾ 提交于 2019-12-05 01:36:06
问题 Let's assume I want to create a trait that I can mix in into any Traversable[T]. In the end, I want to be able to say things like: val m = Map("name" -> "foo") with MoreFilterOperations and have methods on MoreFilterOperations that are expressed in anything Traversable has to offer, such as: def filterFirstTwo(f: (T) => Boolean) = filter(f) take 2 However, the problem is clearly that T is not defined as a type parameter on MoreFilterOperations. Once I do that, it's doable of course, but then

Enriching Scala collections with a method

限于喜欢 提交于 2019-12-05 01:17:07
问题 How do I add a foreachWithIndex method on Scala collections? This is what I could come up with so far: implicit def iforeach[A, CC <: TraversableLike[A, CC]](coll: CC) = new { def foreachWithIndex[B](f: (A, Int) => B): Unit = { var i = 0 for (c <- coll) { f(c, i) i += 1 } } } This doesn't work: Vector(9, 11, 34).foreachWithIndex { (el, i) => println(el, i) } Raises the following error: error: value foreachWithIndex is not a member of scala.collection.immutable.Vector[Int] Vector(9, 11, 34)

Mixing in generic traits in parameterized classes without duplicating type parameters

丶灬走出姿态 提交于 2019-12-03 16:45:34
Let's assume I want to create a trait that I can mix in into any Traversable[T]. In the end, I want to be able to say things like: val m = Map("name" -> "foo") with MoreFilterOperations and have methods on MoreFilterOperations that are expressed in anything Traversable has to offer, such as: def filterFirstTwo(f: (T) => Boolean) = filter(f) take 2 However, the problem is clearly that T is not defined as a type parameter on MoreFilterOperations. Once I do that, it's doable of course, but then my code would read: val m = Map("name" -> "foo") with MoreFilterOperations[(String,String)] or if I

How do I apply the enrich-my-library pattern to Scala collections?

这一生的挚爱 提交于 2019-11-26 05:43:00
One of the most powerful patterns available in Scala is the enrich-my-library* pattern, which uses implicit conversions to appear to add methods to existing classes without requiring dynamic method resolution. For example, if we wished that all strings had the method spaces that counted how many whitespace characters they had, we could: class SpaceCounter(s: String) { def spaces = s.count(_.isWhitespace) } implicit def string_counts_spaces(s: String) = new SpaceCounter(s) scala> "How many spaces do I have?".spaces res1: Int = 5 Unfortunately, this pattern runs into trouble when dealing with