Questions tagged [implicit]

An implicit in Scala is a function applied or a parameter provided without explicitly appearing in the source code.

There are two variants of implicits in Scala: implicit conversions and implicit parameters.

Implicit conversions are applied by the scala compiler when it encounters an expression which it can not compile, but which can be compiled when one of the elements are passed to a function and the return value is used instead of the original element. A typical use case is the enrich-my-library pattern.

For example Scala allows the syntax "some.*regexp".r although the literal between the quotes is a String and String does not have a method r. But there is a method defined in Predef: augmentString(x: String): StringOps and StringOps has an r method. So this implicit conversion is applied.

Implicit parameters are parameters that are added to a method call by the compiler. A somewhat famous use case is in the collection library. Many methods accept a CanBuildFrom parameter which is used to define the type of collection returned by a method. In most cases this parameter is not specified explicitly but an implicit default value is used, which allows the collection library to return the "correct" specialized collection.

Only fields marked as such are considered for use as implicit parameters or conversions.

While this feature makes it possible to create powerful, concise and type safe DSLs and other APIs it should be used with care, since it also allows to create code that is rather hard to understand.

1780 questions
8
votes
5 answers

Templates with implicit parameters, forward declaration, C++

There is a declaration of template class with implicit parameters: List.h template class List: public OList { public: List() : OList () {} .... }; I tried…
Robo
  • 311
  • 5
  • 10
8
votes
2 answers

How can I enable "implicit evaluation" in the JetBrains Rider settings?

JetBrains Rider does not give me the new value for my watches automatically. Instead, I have to click refresh after every step: I went to the settings and made sure this option was enabled: However, the problem still occurs. Any help would be…
sethpblue
  • 81
  • 2
8
votes
1 answer

Scala implicit parameters with defaults defined in the companion object

According to the Scala Spec (2.8), for an implicit to be found it must be defined in local scope, inherited scope, or in a companion object. Given that, it seems to me that the following code should work without an explicit import of the contents…
sellmerfud
  • 340
  • 2
  • 10
8
votes
1 answer

Django inside

Some front-end experts claim that wrapping an with the
kmt
  • 897
  • 2
  • 10
  • 19
8
votes
2 answers

Scala: generic weighted average function

I want to implement a generic weighted average function which relaxes the requirement on the values and the weights being of the same type. ie, I want to support sequences of say: (value:Float,weight:Int) and (value:Int,weight:Float) arguments and…
Maths noob
  • 1,684
  • 20
  • 42
8
votes
1 answer

Scala higher kinded types in implicit def fails with "could not find implicit value"

I'm using implicit def to build a recursive HList type, to match several kind of higher kinded types of HList. I'm heavily inspired by this post. This code is working perfectly : sealed trait HList { type Plus[L <: HList] <: HList } class HNil…
Loic
  • 3,310
  • 4
  • 25
  • 43
8
votes
1 answer

Why high order implicits are ignored in some cases?

I got a strange compiler error about an implicit that is actually present but could not be found for a reason. So I've build a small test case that reproduces mysterious behaviour. trait Hide { type T } object HideString extends Hide { override…
ayvango
  • 5,867
  • 3
  • 34
  • 73
8
votes
1 answer

Scala vs Haskell typeclasses: "catchall" instances

The following Haskell type class and instance: class Able a where able :: a -> Int instance Able Int where able x = x is commonly translated to Scala like so: trait Able[A] { def able(a: A): Int } implicit object AbleInt extends Able[Int]…
scravy
  • 11,904
  • 14
  • 72
  • 127
8
votes
2 answers

Function implicit parameters not any more so after passing it to a higher order function

In Scala you can do things like: def foo(implicit v: Int) = println(v); def h(x: Int) = { implicit val i: Int = x; foo } h(42) > 42 h call gets foo reference as a closure. It wouldn't be strange to try passing foo to h as a parameter: def g(x:…
8
votes
1 answer

How does Scala use explicit types when resolving implicits?

I have the following code which uses spray-json to deserialise some JSON into a case class, via the parseJson method. Depending on where the implicit JsonFormat[MyCaseClass] is defined (in-line or imported from companion object), and whether there…
MrProper
  • 1,023
  • 7
  • 14
8
votes
1 answer

Implicit conversions for defs/lambdas in Scala?

I just ran into a strange disparity between functions and objects (scala 2.10): implicit def conv(c: Int => String) : (PrintStream => Int => Unit) = p => v => p.println(c(v)) def f(h: PrintStream => Int => Unit) : Unit = h(System.out)(1) def a(x:…
8
votes
4 answers

Prevent implicit conversions from float to double in C++

Basically, if I want something like this, double b = sin(2.2); but accidentally write something like this, double b = sin(2.2f); there is no error or even warning message, even though this clearly leads to a different, inaccurate, and therefore…
8
votes
1 answer

Dealing with explicit parameters required by inner implicit parameter lists

I'm currently working with a codebase that requires an explicit parameter to have implicit scope for parts of its implementation: class UsesAkka(system: ActorSystem) { implicit val systemImplicit = system // code using implicit ActorSystem…
Lawrence Wagerfield
  • 6,471
  • 5
  • 42
  • 84
8
votes
3 answers

Preventing implicit cast of numerical types in constructor in C++

I have a constructor of the form: MyClass(int a, int b, int c); and it gets called with code like this: MyClass my_object(4.0, 3.14, 0.002); I would like to prevent this automatic conversion from double to int, or at least get warnings at compile…
Hugo
  • 1,065
  • 1
  • 10
  • 11
8
votes
2 answers

Using string constants in implicit conversion

Consider the following code: public class TextType { public TextType(String text) { underlyingString = text; } public static implicit operator String(TextType text) { return text.underlyingString; } private…
Kornelije Petak
  • 9,412
  • 15
  • 68
  • 96