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.

1595 questions
177
votes
9 answers

Implicit type conversion rules in C++ operators

I want to be better about knowing when I should cast. What are the implicit type conversion rules in C++ when adding, multiplying, etc. For example, int + float = ? int * float = ? float * int = ? int / float = ? float / int = ? int / int = ? int ^…
Matt Montag
  • 5,911
  • 7
  • 37
  • 45
133
votes
13 answers

Can we define implicit conversions of enums in c#?

Is it possible to define an implicit conversion of enums in c#? something that could achieve this? public enum MyEnum { one = 1, two = 2 } MyEnum number = MyEnum.one; long i = number; If not, why not?
Adam Naylor
  • 5,745
  • 9
  • 46
  • 65
75
votes
8 answers

Good example of implicit parameter in Scala?

So far implicit parameters in Scala do not look good for me -- it is too close to global variables, however since Scala seems like rather strict language I start doubting in my own opinion :-). Question: could you show a real-life (or close) good…
greenoldman
  • 15,621
  • 22
  • 103
  • 174
74
votes
7 answers

Android, How to read QR code in my application?

In my application I need to read Qr code. I searched the net and found Zing codes however lots of developers had problem with using it and it seems it is buggy! If i assume that my customers has qr reader installed on their device, how can i use…
Hesam
  • 46,136
  • 63
  • 203
  • 340
70
votes
2 answers

TypeError: Can't convert 'int' object to str implicitly

I am trying to write a text game and I have run into an error in the function I am defining that lets you basically spend your skill points after you make your character. At first, the error stated that I was attempting to subtract a string from an…
anon
  • 1,299
  • 3
  • 13
  • 12
70
votes
7 answers

explicit and implicit c#

I'm new to C# and learning new words. I find it difficult to understand what's the meaning of these two words when it comes to programming c#. I looked in the dictionary for the meaning and here's what I got: Implicit "Something that is implicit…
tintincutes
  • 5,098
  • 24
  • 61
  • 84
67
votes
11 answers

What are type classes in Scala useful for?

As I understand from this blog post "type classes" in Scala is just a "pattern" implemented with traits and implicit adapters. As the blog says if I have trait A and an adapter B -> A then I can invoke a function, which requires argument of type A,…
Michael
  • 9,937
  • 10
  • 55
  • 105
53
votes
7 answers

ReSharper and var

Possible Duplicate: Why does ReSharper want to use 'var' for everything? I have ReSharper 4.5 and have found it invaluable so far but I have a concern; It seems to want to make every variable declaration implicit (var). As a relatively new…
Refracted Paladin
  • 11,636
  • 32
  • 115
  • 224
51
votes
1 answer

Unexpected implicit resolution based on inference from return type

Given a typeclass where instance selection should be performed based on the return type: case class Monoid[A](m0: A) // We only care about the zero here implicit def s[T] : Monoid[Set[T]] = Monoid(Set.empty[T]) implicit def l[T] : Monoid[List[T]] =…
Patrick Prémont
  • 948
  • 7
  • 13
41
votes
5 answers

How to declare traits as taking implicit "constructor parameters"?

I'm designing a class hierarchy, which consists of a base class along with several traits. The base class provides default implementations of several methods, and the traits selectively override certain methods via abstract override, so as to acts…
Andrzej Doyle
  • 97,637
  • 30
  • 185
  • 225
34
votes
6 answers

Is it possible to plot implicit equations using Matplotlib?

I would like to plot implicit equations (of the form f(x, y)=g(x, y) eg. X^y=y^x) in Matplotlib. Is this possible?
Geddes
  • 1,073
  • 2
  • 11
  • 22
34
votes
5 answers

Scala: Passing one implicit parameter implicitly and the other explicitly. Is it possible?

Let's consider the function: def foo(implicit a:Int, b:String) = println(a,b). Now, let us assume that there is an implicit String and Int (implicit val i1=1) in scope but we want to pass an other, not implicit Int (val i2=2) explicitly to foo…
jhegedus
  • 18,516
  • 11
  • 84
  • 147
34
votes
4 answers

Is there a way to declare an implicit val inside a for comprehension?

I have some code with nested calls to flatMap like so: foo.flatMap(implicit f => bar(123).flatMap(b => /* and so on... implicit f is still in scope here.*/ )) Normally, one would write that as a for comprehension, which makes the code a lot more…
Kim Stebel
  • 40,545
  • 12
  • 119
  • 139
32
votes
1 answer

Log implicits only for "diverging implicit expansion"s

Other answers suggest using "-Xlog-implicits" option for debugging "diverging implicit expansion" errors. However, it also logs a lot of implicits in places unrelated to these errors. Is there some way to limit it to only explain places which…
Alexey Romanov
  • 154,018
  • 31
  • 276
  • 433
30
votes
4 answers

Get companion object of class by given generic type Scala

What I am trying to do is to make a function that would take a generic class and use a static method in it (sorry for Java language, I mean method of its companion object). trait Worker {def doSth: Unit} class Base object Base extends Worker //…
Wojtek Erbetowski
  • 3,005
  • 4
  • 19
  • 36
1
2 3
99 100