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
13
votes
3 answers

Explicit Assignment vs Implicit Assignment

I'm reading a tutorial for C++ but it didn't actually give me a difference (besides syntax) between the two. Here is a quote from the tutorial. You can also assign values to your variables upon declaration. When we assign values to a variable…
Freesnöw
  • 30,619
  • 30
  • 89
  • 138
13
votes
7 answers

Implicit Template Parameters

The following code generates a compile error in Xcode: template struct Foo { Foo(T Value) { } }; int main() { Foo MyFoo(123); return 0; } error: missing template arguments before 'MyFoo' Changing Foo MyFoo(123);…
Maxpm
  • 24,113
  • 33
  • 111
  • 170
13
votes
3 answers

how to find a implicit val or def used at in idea IDE?

Ctrl + Q to find implicit conversion Shift + Cmd + P to find the place of an implicit parameter instance How do I find where an implicit value/def is used?
LoranceChen
  • 2,453
  • 2
  • 22
  • 48
13
votes
1 answer

Explain the `LowPriorityImplicits` pattern used in Scala type-level programming

When looking at the source of some Scala libraries, e.g. shapeless, I often find traits named LowPriorityImplicits. Can you please explain this pattern? What is the problem that is solved, and how does the pattern solve it?
ziggystar
  • 28,410
  • 9
  • 72
  • 124
13
votes
1 answer

C++ : Ternary Operator (Conditional Operator) and its Implicit Type Conversion Rules

Are there rules for implicit type conversion for arguments of the ternary operator? The ternary operator always needs to return the same type. This type is determined solely by the second and third argument (1st ? 2nd : 3rd), therefore both…
dib
  • 133
  • 1
  • 5
13
votes
2 answers

How to use Scala 2.10 implicit classes

I thought this would be correct usage of the new implicit classes of Scala 2.10: implicit case class IntOps(i: Int) extends AnyVal { def twice = i * 2 } 11.twice Apparently not: :13: error: value twice is not a member of Int …
0__
  • 66,707
  • 21
  • 171
  • 266
12
votes
1 answer

Doobie cannot find or construct a Read instance for type T

I'm using doobie to query some data and everything works fine, like this: case class Usuario(var documento: String, var nombre: String, var contrasena: String) def getUsuario(doc: String) = sql"""SELECT documento, nombre, contrasena FROM…
santiromf
  • 123
  • 1
  • 6
12
votes
3 answers

Is it possible to pass "this" as implicit parameter in Scala?

Suppose I want to wrap code that can throw exceptions with a try-catch block that logs the exception and continues. Something like: loggingExceptions { // something dangerous } Ideally, I would like to use for logging the Logger defined on the…
Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
12
votes
5 answers

Why does the compiler choose bool over string for implicit typecast of L""?

Having recently introduced an overload of a method the application started to fail. Finally tracking it down, the new method is being called where I did not expect it to be. We had setValue( const std::wstring& name, const std::wstring& value…
Greg Domjan
  • 13,943
  • 6
  • 43
  • 59
11
votes
1 answer

Why is this call to implicitly ambiguous?

The signature of the sum method on TraversableOnce is as follows: def sum[B >: A](implicit num: Numeric[B]): B = foldLeft(num.zero)(num.plus) I can use it thus: scala> (1 to 10).sum res0: Int = 55 In this case, the compiler is injecting the…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
11
votes
2 answers

create an ambiguous low priority implicit

Consider the default codec as offered in the io package. implicitly[io.Codec].name //res0: String = UTF-8 It's a "low priority" implicit so it's easy to override without ambiguity. implicit val betterCodec: io.Codec =…
jwvh
  • 50,871
  • 7
  • 38
  • 64
11
votes
3 answers

How can I "pimp my library" with Scala in a future-proof way?

I use Scala implicit classes to extend objects I work with frequently. As an example, I have a method similar to this defined on Spark DataFrame: implicit class DataFrameExtensions(df: DataFrame) { def deduplicate: Boolean = …
Sasgorilla
  • 2,403
  • 2
  • 29
  • 56
11
votes
3 answers

Type class pattern in Scala doesn't consider inheritance?

I am designing an API using type classes in some cases however I have encountered a problem with implicit resolution. As shown below, if there is an implicit object for type A but an object of type B extends A is passed to the method, then an…
Jesse Eichar
  • 1,081
  • 1
  • 11
  • 11
11
votes
4 answers

Are implicit operators and TypeConverters equivalent?

It seems to me its very easy to implement an implicit operator versus a TypeConverter, so I'm assuming they aren't equivalent because of the prevalence of TypeConverters in the framework (see anything that extends FrameworkElement). But why? …
user1228
11
votes
3 answers

could not find implicit value for parameter e

case class Cat(name: String) object CuterImplicits { implicit class CatCuteChecker(c: Cat) { def isCute(c: Cat) = true } } trait CuteChecker[A] { def isCute(a: A): Boolean } object CheckingForCuteness { def isItCute[A](a: A) =…
Jas
  • 14,493
  • 27
  • 97
  • 148