Questions tagged [enrich-my-library]

Enrich-my-library is a design pattern for Scala that allows to add functionality to existing classes by implicitly converting (typically by wrapping) one class into another that supplies the new capabilities.

Enrich-my-library is a design pattern for Scala that allows to add functionality to existing classes by implicitly converting (typically by wrapping) one class into another that supplies the new capabilities.

When the new method is called on the old class, the conversion happens automatically at compile-time.

30 questions
92
votes
3 answers

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

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…
Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
16
votes
4 answers

Is there a way to "enrich" a Scala class without wrapping the code into another object?

In Scala 2.9 to add custom methods to a library class (enrich or "pimp" it) I had to write something like this: object StringPimper { implicit def pimpString(s: String) = new { def greet: String = "Hello " + s } } As Scala 2.10 was…
Ivan
  • 63,011
  • 101
  • 250
  • 382
15
votes
3 answers

Minimal framework in Scala for collections with inheriting return type

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…
Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
13
votes
1 answer

How do you enrich value classes without overhead?

Scala 2.10 introduces value classes, which you specify by making your class extend AnyVal. There are many restrictions on value classes, but one of their huge advantages is that they allow extension methods without the penalty of creating a new…
Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
10
votes
5 answers

Scala, extending the iterator

Im looking to extended the iterator to create a new method takeWhileInclusive, which will operate like takeWhile but include the last element. My issue is what is best practice to extend the iterator to return a new iterator which I would like to be…
J Pullar
  • 1,915
  • 2
  • 18
  • 30
10
votes
1 answer

How do I enrich a package object?

I recently discovered that one can use the Pimp Enrich My Library pattern to add methods to companion objects using .type: object Whatever { } implicit class WhateverExtensions(val obj: Whatever.type) { def greet =…
Rag
  • 6,405
  • 2
  • 31
  • 38
7
votes
2 answers

Overloading the existing `toInt` method

The toInt method in StringLike doesn't take any arguments, and can only parse in decimal. So to parse binary, hex etc we need to resort to Java's Integer#parseInt(String s, int radix). In an attempt to remedy this state of affairs, I tried the…
Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180
7
votes
3 answers

How can I add new methods to a library object?

I've got a class from a library (specifically, com.twitter.finagle.mdns.MDNSResolver). I'd like to extend the class (I want it to return a Future[Set], rather than a Try[Group]). I know, of course, that I could sub-class it and add my method there.…
Nathaniel Waisbrot
  • 23,261
  • 7
  • 71
  • 99
6
votes
1 answer

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

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…
yura
  • 14,489
  • 21
  • 77
  • 126
6
votes
1 answer

Enrich-My-Library For all Traversables

I was trying to figure out how to write a functional swap function that works on any Traversable[_], given a collection and the indexes to swap. I came up with the following: def swap[A, CC <% Traversable[A]](xs: CC, i: Int, j: Int): Traversable[A]…
KChaloux
  • 3,918
  • 6
  • 37
  • 52
5
votes
3 answers

Type inference on anonymous functions with enrich-my-library

Say I have a method that turns a (function on two elements) into a (function on two sequences): def seqed[T](f: (T,T) => T): (Seq[T], Seq[T]) => Seq[T] = (_,_).zipped map f In words, the resulting function takes two sequences xs and ys, and creates…
Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180
4
votes
3 answers

Enriching Scala collections with a method

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 …
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
4
votes
3 answers

Mixing in generic traits in parameterized classes without duplicating type parameters

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…
4
votes
1 answer

Type mismatch; found : Int(1) required: B

I'm trying to extend the List class to give it some more streamlined way to compare the sizes, however I run into the error in the title... Here's my code: implicit class RichList[A, B](input: List[A]) { def >(that: List[B]): Boolean = input.size…
Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
4
votes
2 answers

How can I add cross product based methods to scala collections?

hopefully this will be a simple question about library pimping (because other questions on that subject tend to generate answers beyond my current skill level). All I want to do is map over the cross product of a collection with itself. val…
teryret
  • 577
  • 1
  • 5
  • 15
1
2