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

Declare a Function `type` with `implicit` parameters

Is it somehow possible to declare something like type F = (Int, Boolean)(implicit String) => Unit in Scala?
scravy
  • 11,904
  • 14
  • 72
  • 127
16
votes
2 answers

`implicit' modifier cannot be used for top-level objects

I recently started to work on play & reactive mongo. Referred the reactive mongo documentation to create a SimpleAlbum. When I run the play app I am getting an error like "Implicit modifier cannot be used for top-level objects". Why am I getting…
Kaushik
  • 1,271
  • 2
  • 18
  • 35
16
votes
5 answers

Why Can A C# Class Inherit From One Interface Both Implicitly and Explicitly?

Today I happens to find that one C# class can inherit one interface both in implicit and explicit way. This surprises me. If C# works in this way, then one instance can behave differently when referenced in different way. interface IFoo { void…
Morgan Cheng
  • 73,950
  • 66
  • 171
  • 230
16
votes
2 answers

Implicit (bool) and == operator override - handle if statements correctly

I have a custom class with implement both the == and the implicit for boolean operator. Is this the correct way to handle all possible, if ==/!= statements and get the expected result? Like this: public class Foo { public bool Result { get; set;…
Rand Random
  • 7,300
  • 10
  • 40
  • 88
16
votes
2 answers

Partially applying a function that has an implicit parameter

Can I turn a method which takes an implicit parameter into a function? trait Tx def foo(bar: Any)(implicit tx: Tx) {} foo _ // error: could not find implicit value for parameter tx: Tx I am trying to achieve the following, preferably if I can…
0__
  • 66,707
  • 21
  • 171
  • 266
15
votes
2 answers

Confused about Scala method calling conventions, specifically the sum function on Seq

I was playing around with the new Scala IDE (Eclipse 3.6.2 + Scala IDE 2.0.0 [Scala 2.9.0]) and I tried to do something simple like this: (1 to 10).sum That works fine, but I've been doing a lot of Groovy also recently and I automatically wrote: (1…
Phuong LeCong
  • 1,834
  • 16
  • 19
15
votes
8 answers

IdentityServer4 PostLogoutRedirectUri null

I am attempting to get the implicit flow working for IdentityServer4. Login and logout work correctly, however the PostLogoutRedirectUri is coming back null, despite setting the value where it needs to be set. What I would like is for the logout…
DoubleTK
  • 183
  • 1
  • 1
  • 8
15
votes
4 answers

what's different between <:< and <: in scala

I already know that: <: is the Scala syntax type constraint while <:< is the type that leverage the Scala implicit to reach the type constrait for example: object Test { // the function foo and bar can have the same effect def…
Zava
  • 743
  • 6
  • 17
15
votes
6 answers

Avoiding implicit def ambiguity in Scala

I am trying to create an implicit conversion from any type (say, Int) to a String... An implicit conversion to String means RichString methods (like reverse) are not available. implicit def intToString(i: Int) = String.valueOf(i) 100.toCharArray //…
Synesso
  • 37,610
  • 35
  • 136
  • 207
14
votes
3 answers

Scala implicit conversion from parent trait

The following code does not compile: import scala.language.implicitConversions trait Base { class Wrp[+T](val v: T) // wrapper / internal representation } trait BooleanOps extends Base { // implicit conversion implicit def…
perovic
  • 266
  • 1
  • 7
14
votes
2 answers

How to stub a method call with an implicit matcher in Mockito and Scala

My application code uses AService trait AService { def registerNewUser (username: String)(implicit tenant: Tenant): Future[Response] } to register a new user. Class Tenant is a simple case class: case class Tenant(val vstNumber:String, val…
simou
  • 2,467
  • 4
  • 30
  • 39
14
votes
2 answers

Scala Implicit Ordering

Is there a way for me to define the same implicit ordering for two different classes? I tried to do something along the following lines but it doesn't detect the ordering. abstract class Common case class A extends Common case class B extends…
Olshansky
  • 5,904
  • 8
  • 32
  • 47
14
votes
1 answer

Scala implicitly vs implicit arguments

I am new to Scala, and when I look at different projects, I see two styles for dealing with implicit arguments scala]]>def sum[A](xs:List[A])(implicit m:Monoid[A]): A = xs.foldLeft(m.mzero)(m.mappend) sum:[A](xs:List[A])(implicit m:Monoid[A])A and…
ekaqu
  • 2,038
  • 3
  • 24
  • 38
14
votes
2 answers

using coalescing null operator on nullable types changes implicit type

I would expect the next three lines of code to be the same: public static void TestVarCoalescing(DateTime? nullableDateTime) { var dateTimeNullable1 = nullableDateTime.HasValue ? nullableDateTime : DateTime.Now; var dateTimeNullable2 =…
comecme
  • 6,086
  • 10
  • 39
  • 67
13
votes
1 answer

What is the performance impact of using the type class pattern in Scala

I'm currently making extensive use of the type class pattern in to be performance-relevant portions of my code. I made out at least two potential sources of inefficiency. The implicit parameters get passed along message calls. I don't know whether…
ziggystar
  • 28,410
  • 9
  • 72
  • 124