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?
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 Comments
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
2 Comments
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
applymethods with context parameters. Specifically, theN-ary function typeT1,… , TN => Ris 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 > 22in 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 Products, Tuples, Functions, PartialFunctions, and case classes has been a significant source of pain in Scala 2.
4 Comments
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).
defs with multiple implicit parameters, so probably implicitdefs with multiple ordinary parameters are kept for consistency although they are not so useful.