简体   繁体   中英

can implicit functions in scala have 2 input parameters?

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?

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 ax 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.

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

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, the N -ary function type T1,…, TN => R is a shorthand for the class type ContextFunctionN[T1,…, TN, R] . Such class types are assumed to have the following definitions, for any value of N >= 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM