Questions tagged [implicits]

Anything related to Scala implicit parameters or conversions

Implicits parameters and conversions are a feature of the Scala language. An implicit parameter is one that can be automatically inferred based on its type and the values in scope, without you needing to pass in the value of the argument explicitly. An implicit conversion function converts one type to another automatically on-demand, without needing to call the function explicitly.

170 questions
2
votes
2 answers

Groovy equivalent for Scala implicit parameters

Is there some Groovy alternative to express something like the following: def doSomethingWith(implicit i:Int) = println ("Got "+i) implicit var x = 5 doSomethingWith(6) // Got 6 doSomethingWith // Got 5 x = 0 doSomethingWith // Got…
Ghiro
  • 500
  • 1
  • 4
  • 11
2
votes
1 answer

Implicit parameter resolution from surrounding scope

I'm not a fan of bringing implicit parameters into my code so where I use them I want to encapsulate their use. So I am trying to define an object that both wraps up calls to spray-json with exception handling and contains default implicit…
Russell
  • 12,261
  • 4
  • 52
  • 75
2
votes
1 answer

Why is trivial implicit not found?

Why isn't the implicit not found, even in something as trivial as: class Wrapper[+A](data: Vector[A]) { def sum[B >: A](implicit num: Numeric[B]) = data.sum } won't compile, without resorting to manually passing in num to data.sum
Heptic
  • 3,076
  • 4
  • 30
  • 51
1
vote
1 answer

Why does DummyImplicit not disambiguate [String](a: A) from (a: String)

Given the following piece of code: final case class Attr[A](name: String)(implicit conv: String To A) { def apply(value: A)(implicit dummy: DummyImplicit) = Attribute(name, value) def apply(value: String) = Attribute[A](name, value) } The Scala…
Tim Friske
  • 2,012
  • 1
  • 18
  • 28
1
vote
2 answers

Implicit lookup for Typeclass of None is not compatible with Contravariant Typeclass of Option

I don't get the following code to compile and I am curious of what I did wrong. I defined a Contravariant Jsonwriter Trait and a function accepting implicit writers: trait JsonWriter[-A] { def write(value: A): Json } object Json { def…
Frederick Roth
  • 2,748
  • 4
  • 28
  • 42
1
vote
2 answers

scala implicit conversion doesn't work

I discover a strange phenomenon: class A { val dual: A = this object T { implicit def doubleDual(t: T): dual.dual.T = t.asInstanceOf[dual.dual.T] implicit def justForTest(t: T): Int = 777 } class T } val a = new A val t = new a.T //…
蘇哲聖
  • 735
  • 9
  • 17
1
vote
1 answer

Generic Avro Serde using shapeless-datatype

I'm struggling creating a generic AvroSerde in Scala. I will be using this serde in combination with Flink therefore this serde should also be serializable itself. Avro doesn't have any native support for Scala, however there are some libraries…
wouterdz
  • 131
  • 1
  • 14
1
vote
1 answer

Folding over provided HList

I am aware that my problem might be "XY problem", so here is a brief recap of what i want to achive. Let's say i have the following type: trait Provider[T] { def provide: T } I want to be able to combine several values provided from that type into…
L.Lampart
  • 755
  • 5
  • 10
1
vote
1 answer

Summoning Scala implicits for subclasses of sealed abstract trait

I'm using two Scala libraries that both rely on implicit parameters to supply codecs/marshallers for case classes (the libraries in question are msgpack4s and op-rabbit). A simplified example follows: sealed abstract trait Event case class…
RoryD
  • 145
  • 6
1
vote
2 answers

Implicit argument in constructor vs method signature

What difference does the compiler see in BroFinder1 and BroFinder2 that causes the first one to fail? I really need BroFinder1 to work as it is without utilizing patterns such as Aux Pattern. trait Brother[I] { type Bro def get: Bro } class…
shayan
  • 1,211
  • 9
  • 12
1
vote
1 answer

Get an implicit instance by class name

What I'm trying to do is: get an implicit instance from the class name. The main problem that I can't get an implicit instance for a class type that created at runtime. What I have: trait Base case class A() extends Base case class B() extends…
1
vote
1 answer

Scala: Implicit resolution, ambigiuty and contravariance

I have the following piece of Scala with ambigiuous implicits, which I would have thought should work, due to lower priority given to inherited implicits. But it does not - it fails with an ambiguous implicit values-error. Can someone explain to me…
holbech
  • 573
  • 3
  • 8
1
vote
1 answer

Mixing Implicit resolution with type lambdas

I know this question is deep type level programming and my failure is due to lack of knowledge. but I'd like to know at least how to get this one to compile. I need the compiler to figure out that my tuple2 can be a Higher-Kinded type in the…
shayan
  • 1,211
  • 9
  • 12
1
vote
2 answers

Implicits in a Spark Scala program not working

I am not able to perform an implicit conversion from an RDD to a Dataframe in a Scala program although I am importing spark.implicits._. Any help would be appreciated. Main Program with the implicits: object spark1 { def main(args: Array[String])…
user1154422
  • 548
  • 8
  • 22
1
vote
1 answer

Shadow any2stringadd when implicitly converting Symbol

I am trying to implicitly add functions to the Symbol class through two levels of implicits (as described here). Consider the following code: case class A(s: Symbol) case class B(s: A) { def +[X](s: X)(implicit xtoa: X=>A) = B(xtoa(s)) } implicit…
DanielM
  • 1,023
  • 8
  • 18