简体   繁体   English

scala 中的隐式函数可以有 2 个输入参数吗?

[英]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 中找到隐式函数的各种用例,但我发现隐式 function 只有一个参数的示例,它可以有 2 个参数吗,如果是的话,它的用例也是什么,我想知道哪些是使用隐式函数的真实场景?

Scala doesn't have a feature called implicit functions, but it does have one that's called views, or sometimes implicit conversions. Scala 没有称为隐式函数的功能,但它确实具有称为视图或有时称为隐式转换的功能。 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.它的工作原理如下:如果你有一个类型 A => B 的隐式值,或者一个可以在 scope 中扩展为这样一个值的隐式 def,当你传递一个类型 A 的值的地方期望一个类型 B 的值时,或者当您在B上有一些xA上没有的地方选择 ax 时,则首先调用该转换。

so a implicit def foo(a: A): B =???所以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 =???可以扩展为implicit val A => B并且是符合条件的隐式转换,但是implicit def foo(a: A, b: B): C =??? can't, so isn't eligible as a conversion.不能,因此不符合转换条件。

Probably you mean method extension .可能你的意思是method extension You can extend tuple in this case(or case class )您可以在这种情况下扩展tuple (或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另外,您可以阅读官方文档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我试图在 scala 中找到隐式函数的各种用例,但我发现只有一个参数到隐式 function 的示例,它可以有 2 个参数吗

Yes.是的。 It can have as many parameters as you want.它可以有任意多的参数。 (The 22 parameter limit no longer exists in Scala 3.) (Scala 3中不再存在22个参数限制。)

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.上下文 function 类型是 class 类型的简写,它定义了带有上下文参数的apply方法。 Specifically, the N -ary function type T1,…, TN => R is a shorthand for the class type ContextFunctionN[T1,…, TN, R] .具体来说, N -ary function 类型T1,…, TN => R是 class 类型ContextFunctionN[T1,…, TN, R]的简写。 Such class types are assumed to have the following definitions, for any value of N >= 1 : […]对于N >= 1的任何值,假定此类 class 类型具有以下定义:[…]

and further down:再往下:

Context function types generalize to N > 22 in the same way that function types do上下文 function 类型概括为N > 22 ,其方式与 function 类型相同

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. Scala 3 尚未发布,因此代码体仍然有限。 The largest system that is built using Scala 3 is probably the Scala 3 compiler itself, which indeed uses context functions heavily.使用 Scala 3 构建的最大系统可能是 Scala 3 编译器本身,它确实大量使用了上下文函数。 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. Scala 的一些主要目标是简单性、规律性和正交性,没有任何奇怪的异常或极端情况。

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.事实上, Product s、 Tuple s、 Function s、 PartialFunction s 和case class es 的任意 22 个参数限制一直是 Scala 2 中痛苦的重要来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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