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
11
votes
1 answer

Type class and dependent types

First off, I don't know how to properly label my problem. This might also be the reason why I didn't find helpful resources. Any hints are highly appreciated. trait Context[T] { self => trait Rule { def apply( value: T ):…
Taig
  • 6,718
  • 4
  • 44
  • 65
11
votes
2 answers

How can implicits with multiple inputs be used in Scala?

For example, how can I write an expression where the following is implicitly applied: implicit def intsToString(x: Int, y: Int) = "test" val s: String = ... //? Thanks
Dimitris Andreou
  • 8,806
  • 2
  • 33
  • 36
11
votes
2 answers

Scala: How can I explicitly compare two Options?

If I have two Options such as val a = Option(2) val b = Option(1) I can write List(a,b).sorted and it sorts correctly by inserting an implicit Ordering. How can I get a reference to this Ordering so I can call compare(a,b) and get the result? I'd…
Sam
  • 1,260
  • 2
  • 11
  • 32
11
votes
3 answers

C# : Custom implicit cast operator failing

Alright, I've been trying to find any information on this for a while. I built a small class to see how hard type-safe-enums are to implement for strings, because I want to use them for database field-names and such. I've never liked the fact that…
FireSBurnsmuP
  • 923
  • 10
  • 28
11
votes
2 answers

Implicit parameter and function

I have a problem considering implicit parameters in Haskell (GHC). I have a function f, that assumes the implicit parameter x, and would like to encapsulate it in a context by applying f to g f :: (?x :: Int) => Int -> Int f n = n + ?x g :: (Int ->…
niklascp
  • 816
  • 5
  • 10
10
votes
2 answers

How does this recursive List flattening work?

A while back this was asked and answered on the Scala mailing list: Kevin: Given some nested structure: List[List[...List[T]]] what's the best (preferably type-safe) way to flatten it to a List[T] Jesper: A combination of implicits and default…
huynhjl
  • 41,520
  • 14
  • 105
  • 158
10
votes
1 answer

How bad are implicit definitions?

I like implicit definitions. They make the code look nice, they make the user feel some features are naturally available on a class when it's just an implicit definition. Yet, I was thinking about JS prototypes, where you can basically define a…
Dici
  • 25,226
  • 7
  • 41
  • 82
10
votes
3 answers

Why to Use Explicit Interface Implementation To Invoke a Protected Method?

When browsing ASP.NET MVC source code in codeplex, I found it is common to have a class explicitly implementing interface. The explicitly implemented method/property then invoke another "protected virtual" method/property with same name. For…
Morgan Cheng
  • 73,950
  • 66
  • 171
  • 230
10
votes
3 answers

Scala: Why use implicit on function argument?

I have a following function: def getIntValue(x: Int)(implicit y: Int ) : Int = {x + y} I see above declaration everywhere. I understand what above function is doing. It is a currying function which takes two arguments. If you omit the second…
user_1357
  • 7,766
  • 13
  • 63
  • 106
10
votes
1 answer

Using Scala Implicitly for Type Equality

I've been reading some stuff on Scala type level programming. Mainly the Apocalisp blog, and also a youtube talk by Alexander Lehmann. I am a bit stuck on something which I guess is probably very basic, which is the use of implicitly to compare two…
bobbyr
  • 226
  • 1
  • 9
10
votes
1 answer

Scala: Implicit Conversion From Generic Type to Second Generic Type

Say I have two sets of classes and the first set inherits from Foo and the second set inherits from Bar. class Foo class Baz extends Foo class Bar class Qux extends Bar I want to make a generic implicit conversion function that converts any Foo to…
LambdaKnight
  • 103
  • 4
10
votes
2 answers

starting android service using explicit vs implicit intent

According to the standard Android documentation, the prefered way to start a service (started service that is) is to use an explicit intent like this: // Using explicit intent: Intent serviceIntent = new Intent(getApplicationContext(),…
user504342
  • 945
  • 2
  • 16
  • 36
10
votes
1 answer

C++ implicit conversion to bool

In an effort to make my enums more typesafe, I've been using macro-generated overloaded operators to disallow comparing enums against anything but an identically typed enum: #include #define…
Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
9
votes
1 answer

scala - Confusing "diverging implicit expansion" error when using "sortBy"

I wonder why List(3,2,1).toIndexedSeq.sortBy(x=>x) doesn't work: scala> List(3,2,1).toIndexedSeq.sortBy(x=>x) // Wrong :8: error: missing parameter type List(3,2,1).toIndexedSeq.sortBy(x=>x) …
Tom Dong
  • 521
  • 4
  • 10
9
votes
5 answers

Strings and ints, implicit and explicit

Had a coworker ask me this, and in my brain befuddled state I didn't have an answer: Why is it that you can do: string ham = "ham " + 4; But not: string ham = 4; If there's an implicit cast/operation for string conversion when you are…
Rostov
  • 2,516
  • 2
  • 23
  • 17