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

Workaround for importing spark implicits everywhere

I'm new to Spark 2.0 and using datasets in our code base. I'm kinda noticing that I need to import spark.implicits._ everywhere in our code. For example: File A class A { def job(spark: SparkSession) = { import spark.implcits._ …
6
votes
1 answer

Does the scala compiler do anything to optimize implicit classes?

Say we have an implicit class like: implicit class RichString(str: String) { def sayHello(): String = s"Hello, ${str}!" } We can use the method sayHello as if it is defined on the String class "World".sayHello Does the scala compiler optimize…
Upio
  • 1,364
  • 1
  • 12
  • 27
6
votes
1 answer

Why can a non-applicable implicit conversion introduce ambiguity?

The setup for this example (Scala 2.10.3): trait S[A] trait T[A] implicit class X[A : S](a: A) { def foo() { } } implicit class Y[A : T](a: A) { def foo() { } } implicit object I extends S[String] This compiles: new X("").foo() This doesn't: new…
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
6
votes
3 answers

How to express this type in Scala? Existential with type class (ie, implicit) restriction?

I'm using the Play framework's JSON library, which uses a type class to implement the Json.toJson function. (I may decide to use another technique with less static typing, like reflection; but for now I want to use this library because it's helping…
Rob N
  • 15,024
  • 17
  • 92
  • 165
6
votes
2 answers

Scala implicit Numeric[T] in companion object

I have the following generic Interval class (kindly formulated for me by user soc): case class Interval[T](from: T, to: T)(implicit num: Numeric[T]) { import num.mkNumericOps // allows us to write from.toDouble and to.toDouble def mid: Double =…
5
votes
2 answers

Deforestation in Scala collections

From the design of Scala's collections I understand that something like: scala> BitSet(1,2,3) map (_ + "a") res7: scala.collection.immutable.Set[String] = Set(1a, 2a, 3a) doesn't build an intermediate datastructure: the new Set is built as the…
Paul Brauner
  • 1,307
  • 1
  • 10
  • 17
5
votes
1 answer

How does Scalaz `F[_] : Applicative` type constraint imply use of implicit parameters?

I am struggling to understand the following function definition in Traverse trait in Scalaz: def traverse[F[_] : Applicative, A, B](f: A => F[B], t: T[A]): F[T[B]] The part I don't understand is F[_] : Applicative. Now, let's see what Applicative…
Rotsor
  • 13,655
  • 6
  • 43
  • 57
5
votes
4 answers

Question about type classes in Scala

Let there are classes Fruit, Orange, and Apple. abstract class Fruit class Orange extends Fruit class Apple extends Fruit Now I want to add write functionality to both types Orange and Apple. Using the type class pattern I can do the…
Michael
  • 10,185
  • 12
  • 59
  • 110
5
votes
0 answers

How can I chain generic implicits in Scala?

There is this post that discusses chaining of implicits but I think it doesn't cover my case because I have generic implicits. Sample project that demonstrates the issue is located here. To reproduce these two lines should be commented out. So I…
expert
  • 29,290
  • 30
  • 110
  • 214
5
votes
1 answer

Evidence that types are not equal in Scala

Is there any way to constraint a method so that it only makes sense if two types are proved not to be equal? trait Something[A, B] { // I can only be called if type A is the same as type B def ifEqual(implicit ev: A =:= B) // Now I cannot be…
Hugo Sereno Ferreira
  • 8,600
  • 7
  • 46
  • 92
5
votes
2 answers

Scala 2.11 Type Variance Changes

In Scala 2.10.4 this compiles: trait Foo[-U,T]{ type Contra = U } but in 2.11.0 the same fails with: contravariant type U occurs in invariant position in type U of type Contra trait Foo[-U,T] {type Contra = U} Is there a workaround available?…
virtualeyes
  • 11,147
  • 6
  • 56
  • 91
5
votes
1 answer

Why do we have to explicitly import implicit conversions having implicit parameters from companion objects? Strange.

Let's consider this code: class A object A{ implicit def A2Int(implicit a:A)=1 implicit def A2String(a:A)="Hello" } object Run extends App{ implicit val a: A =new A import A.A2Int // without this import this code does not compile, why ? …
jhegedus
  • 20,244
  • 16
  • 99
  • 167
5
votes
4 answers

Implicit lifting in scala

I want to implicitly convert functions from A => B to List[A] => List[B]. I wrote the following implicit definition: implicit def lift[A, B](f: A => B): List[A] => List[B] = ... Unfortunately, when I write the following code, implicit aren't…
Konstantin Solomatov
  • 10,252
  • 8
  • 58
  • 88
4
votes
3 answers

In Scala, how do i tell an abstract base class that type parameter T supports implicit conversion from Int (or Float, or...)?

I'm having difficulty transitioning from the world of C++/Templates to scala. I'm used to being able to use any operation on a template parameter T that I want, as long as anything I use to instantiate T with supports those operations (compile-time…
Fooberman
  • 626
  • 5
  • 14
4
votes
1 answer

Why does scala compiler fail to find implicit parameter value/conversion when it is an overload and has generic type param?

Scala 2.8.1 Take the following class hierarchy abstract class A class B extends A class C extends A Why is the scala compiler unable to find the implicit parameter for send when sending an instance of B below implicit def routingKeyFor[T <:…
drstevens
  • 2,903
  • 1
  • 21
  • 30
1 2
3
11 12