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

import implicit conversions without instance of SparkSession

My Spark-Code is cluttered with code like this object Transformations { def selectI(df:DataFrame) : DataFrame = { // needed to use $ to generate ColumnName import df.sparkSession.implicits._ df.select($"i") } } or…
Raphael Roth
  • 26,751
  • 15
  • 88
  • 145
4
votes
2 answers

Runtime cost of implicit definitions in Scala's typeclass pattern

I am looking at this article, on how Typeclasses are simulated in Scala using implicits. If I am not wrong, a new instance is constructed on runtime every time a typeclass method for a recursive instance declaration is used (Printer a => Printer…
zeronone
  • 2,912
  • 25
  • 28
4
votes
1 answer

Scala implicit conversion for type aliases

Define type TA[T] = T => Int implicit class TAOps[T](a: TA[T]) { def foo(): Unit = {println("TA")} } val ta: TA[Double] = x => x.toInt Now, ta.foo() fails to compile with the message value foo is not a member of…
Kamil Kloch
  • 333
  • 1
  • 9
4
votes
2 answers

Implicit class resolution for parameterized types

In the following example, it seems that the Scala compiler only recognizes an implicit class when it is defined to take the higher-kinded representation of Wrapper. Why is that? scala> case class Nested(n: Int) defined class Nested scala> case…
Sim
  • 13,147
  • 9
  • 66
  • 95
4
votes
1 answer

Composing type-level functions with implicit witnesses

I am experimenting with some rather complex type-level calculations. There, I have some type tags (say, A, B, and C), and functions working on them, which are represented by implicit witnesses with path-dependent result types: class A class B class…
4
votes
2 answers

Scala resolution of multiple implicit parameters

In trying to answer this question, I came up with the following code: case class Monkey(bananas: Int) case class Tree(rings: Int) case class Duck(quacks: Seq[String]) implicit class IntLike(val x : Int) extends AnyVal implicit…
Impredicative
  • 5,039
  • 1
  • 17
  • 43
3
votes
2 answers

Implicit conversion issue in Scala

I'm improving the Scala support in Querydsl and I encountered the following issue. Here is a code snippet that illustrates the problem : // framework types class RelationalPath[T] class RelationalPathAdapter[T, R <: RelationalPath[T]](p: R) { def…
Timo Westkämper
  • 21,824
  • 5
  • 78
  • 111
3
votes
1 answer

Scala: How to get context bound List[T] conversion working here?

This is my first question here so hope I provide enough detail. Feel free to ask for clarification. Taking the following into consideration, which works: implicit def optionBsonReader[T, U](implicit ev: BsonReader[T, U]) = new BsonReader[Option[T],…
dlouwers
  • 83
  • 1
  • 7
3
votes
2 answers

How to express (implicit conv: String => A) as a view bound

I am asking myself what would be the view bound equivalent to (implicit conv: String => A) My first attempt was to simply declare the type parameter A as follows: [String <% A] But the Scala compiler complains with "not found: type A". Any…
Tim Friske
  • 2,012
  • 1
  • 18
  • 28
3
votes
2 answers

Two seemingly identical semantics: one binds implicitly, the other does not

Hello: I've been learning Scala recently (my related background is mostly in C++ templates), and I've run into something I currently don't understand about Scala, and it is driving me insane. :( (Also, this is my first post to StackOverflow, where…
Jay Freeman -saurik-
  • 1,759
  • 1
  • 13
  • 13
3
votes
3 answers

What to do with operations for a specific kind of collection?

In several different places in my application, I need to take a Seq[SalesRow] and return a Map[String,SalesRow], where the string is the name of a country. I need to use this in several places. For example, I take a list of all SalesRows and get a…
Bill
  • 44,502
  • 24
  • 122
  • 213
3
votes
1 answer

Can Scala's implicits compose to convert higher-kinded types?

Let's say I have a type called LongArrayWritable, that is a boxed representation of an Array of Longs. I have implicit definitions that convert between these types: implicit def boxLongArray(array: Array[Long]) : LongArrayWritable {…
mistertim
  • 5,123
  • 4
  • 20
  • 27
3
votes
2 answers

How to implement intermediate types for implicit methods?

Assume I want to offer method foo on existing type A outside of my control. As far as I know, the canonical way to do this in Scala is implementing an implicit conversion from A to some type that implements foo. Now I basically see two…
Raphael
  • 9,779
  • 5
  • 63
  • 94
3
votes
1 answer

Ambiguous implicit solution other than sub typing

If you run the code below you'll get an ambiguous implicit error: class Foo[T,I](val msg: I) object Foo { implicit def provide[T]: Foo[T,String] = new Foo("I came from a place you can't touch so subtyping can't help you") } class User object…
shayan
  • 1,211
  • 9
  • 12
3
votes
4 answers

Scala compiler says my method is recursive in case when implicits and anonymous class is used

I want to be able to write code like 10 times { doSomething } So I thought I could do that with implicits. When i execute the following code in the Scala REPL it gets defined correctly scala> implicit def intToMyRichInt(count: Int) = { | …