I tried to find various use cases of implicit functions in scala, but I found the examples with only one parameter to an implicit function, can it have 2 parameters if yes what will be the use case for it also, I want to know which are the real-world scenarios where implicit functions are used?
-
Based on your deleted comment *"what you have explained is a function with 2 implicit parameters, what I am looking for is an implicit function with 2 parameters"* you're looking for implicit conversions with multiple parameters https://stackoverflow.com/questions/20290630/implicit-conversion-for-multiple-parameters https://stackoverflow.com/questions/21131509/scala-implicit-conversions-with-multiple-arguments https://stackoverflow.com/questions/2416733/how-can-implicits-with-multiple-inputs-be-used-in-scala – Dmytro Mitin Oct 09 '20 at 18:45
-
https://stackoverflow.com/questions/10635406/defining-a-function-with-multiple-implicit-arguments-in-scala – Dmytro Mitin Oct 09 '20 at 18:45
-
Do these links answer your question? – Dmytro Mitin Oct 09 '20 at 18:48
-
Important are implicit `def`s with multiple **implicit** parameters, so probably implicit `def`s with multiple ordinary parameters are kept for consistency although they are not so useful. – Dmytro Mitin Oct 09 '20 at 19:13
-
I mean, not so useful as implicit defs with single ordinary parameter (implicit conversions) or implicit defs with single/multiple implicit parameters (conditional implicits). – Dmytro Mitin Oct 09 '20 at 19:49
3 Answers
Scala doesn't have a feature called implicit functions, but it does have one that's called views, or sometimes implicit conversions. It works as follows: If you have an implicit value of type A => B, or an implicit def that can be expanded to such a value in scope, when a value of type B is expected where you pass a value of type A, or when you make a selection a.x where there some x
on B
but not on A
, then that conversion is called on a first.
so a implicit def foo(a: A): B = ???
can be expanded into an implicit val A => B
and is an eligible implicit conversion, but an implicit def foo(a: A, b: B): C = ???
can't, so isn't eligible as a conversion.

- 11,964
- 12
- 50
- 96
-
"Scala doesn't have a feature called implicit functions" – While that is *technically* true, I feel that you are splitting hairs. Yes, they were renamed to *Context Functions* last Summer, and yes, technically, Scala 3 isn't released yet, but it's clear what the OP is talking about. – Jörg W Mittag Oct 09 '20 at 12:27
-
I never heard anything called implicit functions myself. I'm assuming they're talking about views/implicit conversions, but I'm not 100% sure. Scala 3 context functions are really something else entirely. – Martijn Oct 09 '20 at 12:53
-
1Context functions were called implicit functions for almost the entire time that they existed, and the vast majority of resources still calls them that. Even [the tutorial](https://www.scala-lang.org/blog/2016/12/07/implicit-function-types.html#implicit-functions) that is linked on the [Context Functions](https://dotty.epfl.ch/docs/reference/contextual/context-functions.html#reference) page calls them Implicit Functions. – Jörg W Mittag Oct 09 '20 at 13:03
-
1Ah, so they are. But are scala3/dotty implicit functions indeed what the question means? I doubt that. – Martijn Oct 09 '20 at 14:00
-
@Martijn yeah, based in OP's comment to your deleted answer, the question was about implicit conversions with multiple parameters. – Dmytro Mitin Oct 09 '20 at 18:59
-
@Martijn This part *"... can be expanded into an `implicit val A => B` ... but an `implicit def foo(a: A, b: B): C = ???` can't"* seems to be literally **incorrect** because if we define `implicit def foo(a: A, b: B): C = ???` or `implicit val foo: (A, B) => C = ???` then `implicitly[(A, B) => C]` compiles, just it doesn't work as an implicit conversion: `val x: C = ??? : (A, B)` doesn't compile (for this to compile we would need `implicit val foo: ((A, B)) => C = ???` or `implicit def foo(ab: (A, B)): C = ???`, then `implicitly[((A, B)) => C]` would compile). – Dmytro Mitin Oct 09 '20 at 19:06
-
@Martijn: "are scala3/dotty implicit functions indeed what the question means?" – Implicit functions are *explicitly* mentioned no less than four times in the question, and a fifth time in a comment to a now-deleted answer. Neither views nor implicit conversions are mentioned anywhere by the OP, nor have they said anything to the contrary in comments or edited the question, so if they are asking 5 times about implicit functions and 0 times about views, I am going to assume, they want to know about implicit functions, not views. We might argue whether they should use the terminology "context … – Jörg W Mittag Oct 11 '20 at 12:31
-
… function" instead, but I think that can be forgiven. Almost all the documentation that is out there calls them "implicit function". All the talks that are linked from the Dotty homepage call them "implicit function". "Implicit function" is the term Odersky used when he introduced them, it's the term that is used in the pull request that implements them, it is the term that is used in the SIP that proposes them, it is even the term that is used in the blog post tutorial that is linked from the "Context Functions" documentation page. In fact, until two days ago, I didn't even know that they … – Jörg W Mittag Oct 11 '20 at 12:34
-
… were renamed. I literally watched a talk on "implicit functions" linked to from the Dotty website only hours before reading this question. So, I think the OP can be forgiven for not knowing about the name change: I am pretty interested in Scala and Dotty, and *I* myself missed it. – Jörg W Mittag Oct 11 '20 at 12:35
-
1@JörgWMittag regardless of whether you think OP ought to have meant something else, it has every appearance of them not talking about the former name of the yet unreleased dotty feature. – Martijn Oct 12 '20 at 08:05
-
@JörgWMittag OP just misuses terminology (calling `def`s functions). – Dmytro Mitin Oct 12 '20 at 10:24
Probably you mean method extension
. You can extend tuple
in this case(or case class
)
def main(args: Array[String]): Unit = {
val tuple = ("John", "Smith")
val concat = tuple.tupleConcat
println(concat)
}
implicit class TupleExt(val tuple: (String, String)) extends AnyVal {
def tupleConcat = s"${tuple._1} ${tuple._2}"
}
Also, you can read the official docs IMPLICIT CLASSES

- 549
- 3
- 14
-
The OP is asking about [*implicit functions*](https://www.scala-lang.org/blog/2016/12/07/implicit-function-types.html#implicit-functions), though, which are a very different concept from implicit classes. – Jörg W Mittag Oct 12 '20 at 08:24
-
1@JörgWMittag The OP seems NOT to be asking about Scala-3 implicit functions. And extension methods (introduced by implicit classes) are sometimes legitimate replacement for implicit conversions (including absent implicit conversions with multiple parameters). – Dmytro Mitin Oct 12 '20 at 12:22
Please note that Implicit Functions were renamed to Context Functions. They are now documented here.
I tried to find various use cases of implicit functions in scala, but I found the examples with only one parameter to an implicit function, can it have 2 parameters
Yes. It can have as many parameters as you want. (The 22 parameter limit no longer exists in Scala 3.)
It is clear from the specification that there is no limit:
Context function types are shorthands for class types that define
apply
methods with context parameters. Specifically, theN
-ary function typeT1,… , TN => R
is a shorthand for the class typeContextFunctionN[T1 ,… , TN, R]
. Such class types are assumed to have the following definitions, for any value ofN >= 1
: […]
and further down:
Context function types generalize to
N > 22
in the same way that function types do
So, it is clear that there can be an arbitrary number of parameters.
if yes what will be the use case for it
The obvious use case would be if you don't want a single context, but want to compose your context out of multiple orthogonal bits. Imagine a scenario where you need both logging and transactions. There is no reason why this should be a single monolithic context.
also, I want to know which are the real-world scenarios where implicit functions are used?
Scala 3 isn't released yet, so the body of code is still limited. The largest system that is built using Scala 3 is probably the Scala 3 compiler itself, which indeed uses context functions heavily. Not quite as heavy as context parameters, though.
I suspect that the vast majority of implicit functions will only ever have a single parameter. But that's not a reason to artificially restrict them. Some of the major goals of Scala are simplicity, regularity, and orthogonality, without any weird exceptions or corner cases.
Methods can have arbitrarily many parameters, ordinary functions can have arbitrarily many parameters, type constructors can have arbitrarily many parameters, constructors can have arbitrarily many parameters, in fact everything that can take parameters at all can take arbitrarily many of them, it would be strange to artificially restrict only implicit functions.
I mean, the vast majority of type constructors, methods, and ordinary functions probably also only have one parameter, or at most two, but we don't restrict them either. In fact, the arbitrary 22 parameter limit for Product
s, Tuple
s, Function
s, PartialFunction
s, and case class
es has been a significant source of pain in Scala 2.

- 363,080
- 75
- 446
- 653
-
1It seems OP meant not Scala-3 implicit functions but implict conversions with multiple parameters in Scala 2. – Dmytro Mitin Oct 09 '20 at 18:47
-
I do not see any statements of the OP to that effect in either the question or the comments to any of the answers. In a comment on a deleted answer, the OP even *reaffirmed* they are asking about implicit functions. – Jörg W Mittag Oct 12 '20 at 08:12
-
From that deleted Martijn's answer and Vivek's comment it's clear that Vivek (the OP) calls `def example(normal: Int, params: String)(implicit config: Configuration, context: ExecutionContext) = ???` (Martijn's code snippet) a "function with 2 implicit parameters" but is looking for "an implicit function with 2 parameters" i.e., I guess, something like `implicit def example(normal: Int, params: String)`. – Dmytro Mitin Oct 12 '20 at 10:21
-
I'll copy the deleted answer for those who can't see it https://snipboard.io/i9Ze5l.jpg – Dmytro Mitin Oct 12 '20 at 12:37