Questions tagged [implicit-class]
11 questions
10
votes
1 answer
Elegant grouping of implicit value classes
I'm writing a set of implicit Scala wrapper classes for an existing Java library (so that I can decorate that library to make it more convenient for Scala developers).
As a trivial example, let's say that the Java library (which I can't modify) has…

Mike Allen
- 8,139
- 2
- 24
- 46
2
votes
1 answer
Creating a type-sensitive function without changing the parent trait or case classes
Suppose I have two classes, Person and Business, that are extended by the trait Entity.
trait Entity
case class Person(name: String) extends Entity
case class Business(id: String) extends Entity
Assuming I cannot change Entity, Person and Business…

bruno
- 23
- 3
1
vote
1 answer
Scala selects wrong implicit conversion from within implicit class method
The compiler is failing to choose the correct implicit conversion method when that conversion occurs within an implicit class declaration. In the example below, I have a Foo[T] class and an implicit Helper class that takes a Foo and provides a print…

alcorn
- 1,268
- 1
- 8
- 17
1
vote
1 answer
Scala - Java interop - is it possible to "choose" the called java method from scala?
I'm having some interop issues calling Jedis (Java) from my Scala code (scala 2.10.4).
I have a trait which I've implemented in an implicit class
I get the following error when calling a method on the underlying class
implicit class Redis(private…

Avba
- 14,822
- 20
- 92
- 192
0
votes
1 answer
How extend a class is diff from implicit class?
I'm trying to understand the difference between child class (class child extend Parent) and implicit class ( implicit class Child (a: Parent))
In both cases, the child instance is able to access both Parent and child methods. Am I missing something…

Lenny
- 83
- 1
- 1
- 11
0
votes
0 answers
Is it possible to implicitly cast a generic type?
I'm working with a function that takes a list of email address records and returns the appropriate one for the use case. This list is passed to the function as an List[Map[String, String]], and the first step of the function is to cast each of the…

James Kelleher
- 1,957
- 3
- 18
- 34
0
votes
1 answer
Implicit class for akka stream SubFlow (generic path dependent types)
I am having a hard time to get an implicit class for an akka.stream.scaladsl.SubFlow to compile.
My test code:
val subFlow = Source(List("1", "2", "3"))
.groupBy(1, f)
val richSubFlow = new SideEffectfulSubFlowOps(subFlow)
val got =…

leozilla
- 1,296
- 2
- 19
- 29
0
votes
0 answers
implicit classes in Scala : performance and good practice
As far as I know, implicit classes are used to provide extension methods to existing types , or let's say enrich types coming from other libraries or APIs.
However, I was playing around and tried this :
case class Person(firstname : String ,…

Helix112
- 305
- 3
- 12
0
votes
1 answer
Implicit class in scala not showing output as expected
I was trying to run a simple program in scala involving implicit class in scala. The expected output of the program is "CZF" i.e. incrementing each character by 1. But, when i am executing it on Eclipse IDE, its not returning any result nor the…

Sri2110
- 335
- 2
- 19
0
votes
1 answer
Implicit classes and "not a member of type parameter" error
I have the following type class definition -
trait ToBigInt[A] {
def toBigInt(n: A): BigInt
}
object ToBigInt {
def apply[A](implicit t: ToBigInt[A]): ToBigInt[A] = t
implicit val toBigIntByte: ToBigInt[Byte] = (x: Byte) => BigInt(x)
…

Michael T
- 1,033
- 9
- 13
0
votes
2 answers
Scala: make an implicit class accessible from classes injecting the class defining it
I have an implicit class defined in a class injected in other classes like this
class A {
implicit class B(s: String) {
def b = ???
}
}
class C(a: A) {}
Is there a way to access implicit class B (and in particular its method b) from the…

Simon
- 6,025
- 7
- 46
- 98