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
3
votes
1 answer

Why can I mark methods as implicit but not the constructor?

The common Enrich-My-Library pattern seems to be something like class Foo(value: Int) implicit def int2Foo(i: Int) = new Foo(i) Why isn't it possible to just add the implicit to the constructor itself like this class Foo implicit (value:…
soc
  • 27,983
  • 20
  • 111
  • 215
3
votes
1 answer

Scala: value class X is added to the return type of its methods as X#

I'd like to enrich a 'graph for scala' graph. For this purpose i've created an implicit value class: import scalax.collection.mutable import scalax.collection.edge.DiEdge ... type Graph = mutable.Graph[Int, DiEdge] implicit class…
gospes
  • 3,819
  • 1
  • 28
  • 31
3
votes
1 answer

Configure scaladoc to include extension methods

Can I configure scaladoc to include methods from implicit conversions by specifying the implicit conversion? E.g. given trait Foo object Operations { implicit class FooOps(val f: Foo) extends AnyVal { def bar = 33 } } Can I make scaladoc…
0__
  • 66,707
  • 21
  • 171
  • 266
3
votes
1 answer

Enrich an inner class

I want to implement the enrich-my-library pattern for the inner class that will work for any instance of the outer class. Something like this: class Outer { class Inner(val x: Option[Inner] = None) { def test(y: Inner) {} } } implicit class…
gleb
  • 143
  • 7
3
votes
1 answer

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

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…
Petr
  • 62,528
  • 13
  • 153
  • 317
3
votes
1 answer

Option-izing Java getters

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…
virtualeyes
  • 11,147
  • 6
  • 56
  • 91
2
votes
1 answer

How to add methods that takes implicit ordering to a scala collection object

I'm trying to utilize the enrich-my-library pattern with scala collections methods with implicit ordering. Given this definition: object ImplicitTest { implicit def RichTraversableOnce[A](t: TraversableOnce[A]): RichTraversableOnce[A] = new…
eirirlar
  • 814
  • 6
  • 24
2
votes
1 answer

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

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…
2
votes
2 answers

How to dynamically dispatch based on enrichment?

The spray-json library extends basic Scala types with a toJson method. I'd like to convert an Any into a JsValue if there is such a pimp for the underlying type. My best attempt works, but is verbose: import cc.spray._ val maybeJson1:…
James Marble
  • 863
  • 1
  • 10
  • 22
1
vote
1 answer

Enrich-my-library by extending TraversableLike with own methods

I tried to extend TraversableLike with my own methods, but I failed. First, see what I wanna achieve: class RichList[A](steps: List[A]) { def step(f: (A, A) => A): List[A] = { def loop(ret: List[A], steps: List[A]): List[A] = steps match { …
kiritsuku
  • 52,967
  • 18
  • 114
  • 136
0
votes
1 answer

How to define an impicit conversion from one to another existing type in Scala?

Let's say we've got a Foo class in a library we use and want (in a code file of ours) Foo instances to be implicitly cast to String instances whenever a Foo instance is met in a place a String instance is required. How to achieve this in Scala?
Ivan
  • 63,011
  • 101
  • 250
  • 382
0
votes
2 answers

How do I enrich a case class with minimal changes to the codebase?

Using scala 2.11.12. Scattered all over my code base I have a case class like this: case class Landscape( north: Sight, east: Sight, south: Sight, west: Sight ) { def toList: List[Sight] = List(north, east, south, west) def isIdyllic:…
0
votes
1 answer

How to implement flatMap for Option

I'm trying to implement map and flatMap as an extension/enrichment for Option, without cheating and looking at how it was implemented in Scalaz. So here's what I got so far before I got stuck: package extensions.monad trait Monad[M[_]] { // >>=…
Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
0
votes
1 answer

How to implicitly extend a Function without losing type inference

For a DSL I need to implicitly extend function values. For instance: trait PimpedFunction[-A, +B] extends Function1[A, B] { def foo = 42 } object PimpedFunction { implicit def pimp[A, B](f: Function1[A, B]): PimpedFunction[A, B] = new…
0
votes
1 answer

Enrich-My-Library optimization

Disclaimer: I came to Scala from C#, where I really appreciated LINQ. Therefore, I immediately felt at home with iterators and sequences. I missed yield "C# style", but I was able to cook my own with continuations... even if it pays a performance…
1
2