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

Where does Scala look for implicits?

An implicit question to newcomers to Scala seems to be: where does the compiler look for implicits? I mean implicit because the question never seems to get fully formed, as if there weren't words for it. :-) For example, where do the values for…
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
288
votes
1 answer

What are Scala context and view bounds?

In a simple way, what are context and view bounds and what is the difference between them? Some easy-to-follow examples would be great too!
chrsan
  • 3,390
  • 3
  • 19
  • 14
175
votes
4 answers

What is the Scala identifier "implicitly"?

I have seen a function named implicitly used in Scala examples. What is it, and how is it used? Example here: scala> sealed trait Foo[T] { def apply(list : List[T]) : Unit }; object Foo { | implicit def stringImpl = new…
oluies
  • 17,694
  • 14
  • 74
  • 117
78
votes
3 answers

How can I chain implicits in Scala?

The pimp-my-library pattern allows me to seemingly add a method to a class by making available an implicit conversion from that class to one that implements the method. Scala does not allow two such implicit conversions taking place, however, so I…
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
52
votes
1 answer

Can't prove that singleton types are singleton types while generating type class instance

Suppose I've got a type class that proves that all the types in a Shapeless coproduct are singleton types: import shapeless._ trait AllSingletons[A, C <: Coproduct] { def values: List[A] } object AllSingletons { implicit def cnilSingletons[A]:…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
49
votes
1 answer

What is a diverging implicit expansion error?

While trying to find a solution to another question ([1]) I came across a diverging implicit expansion error. I'm looking for an explanation about what this means Here's the use case: scala> implicit def ordering[T](implicit conv: T => Ordered[T],…
IttayD
  • 28,271
  • 28
  • 124
  • 178
30
votes
1 answer

Conditions under which compiler will not define implicits (constructor, destructor, copy constructor, copy assignment)

This is supposed to be a trivial question but I could not find it explicitly on stackoverflow. The following will be defined implicitly if not provided by the user. default (parameterless) constructor copy constructor copy assignment…
aiao
  • 4,621
  • 3
  • 25
  • 47
27
votes
2 answers

"Cannot find an implicit ExecutionContext" error in scala.js example app.

Here is an example from the Hands-on Scala.js ebook: package webpage import org.scalajs.dom.ext.Ajax import scala.scalajs.js import scala.scalajs.js.annotation.JSExport import scalatags.JsDom.all._ import org.scalajs.dom import…
qed
  • 22,298
  • 21
  • 125
  • 196
21
votes
5 answers

Scala dependency injection: alternatives to implicit parameters

Please pardon the length of this question. I often need to create some contextual information at one layer of my code, and consume that information elsewhere. I generally find myself using implicit parameters: def foo(params)(implicit cx:…
Marcus Downing
  • 10,054
  • 10
  • 63
  • 85
19
votes
6 answers

Other programming languages that support implicits "a la Scala"

Scala implicits are very powerfull. I'm curious if they are a new/unique feature of Scala, or the concept already existed in other programming languages. Thanks. EDIT: To clarify my question, yes, I'm talking about this concrete implementation.…
gerferra
  • 1,519
  • 1
  • 14
  • 26
16
votes
1 answer

Creating instances of a covariant type class from instances of a non-covariant one

Suppose I've got a simple type class whose instances will give me a value of some type: trait GiveMeJustA[X] { def apply(): X } And I've got some instances: case class Foo(s: String) case class Bar(i: Int) implicit object GiveMeJustAFoo extends…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
15
votes
1 answer

False errors when using cats library in IntelliJ

I am using the cats Scala library and the IntelliJ IDE seems to be struggling with the use of implicits: Here is a simple example: import cats.std.all._ import cats.Traverse.ops._ def useSequence[A](ls : List[Option[A]]) : Option[List[A]] = { …
mushroom
  • 6,201
  • 5
  • 36
  • 63
14
votes
3 answers

How to define type lambda properly?

I used =:= as example type lambda for purpose of making simple minimal example. =:= type take two arguments, I'd like to curry one at type level. I take naive implementation type Curry[G] = {type l[L] = L =:= G} but in practical uses it causes…
ayvango
  • 5,867
  • 3
  • 34
  • 73
13
votes
2 answers

Type classes in Scala

Having a background in Haskell I am currently trying to get familiar with Scala. I encountered some problems trying to translate a small, extensible expression language from Haskell into Scala. The underlying issue of writing a data type that is…
FloDo
  • 215
  • 1
  • 9
13
votes
2 answers

"can't existentially abstract over parameterized type..."

I was messing around with Scala 2.8 for fun and trying to define a pimp which adds an "as" method to type constructors, allowing to convert from one functor to another (please overlook the fact that I'm not necessarily dealing with functors here).…
Tom Crockett
  • 30,818
  • 8
  • 72
  • 90
1
2 3
11 12