简体   繁体   English

什么?=> 在 Scala 中是什么意思?

[英]What does ?=> mean in Scala?

I have seen the ?=> symbol appear in Scala code and in some discussion posts about Scala 3, so I am assuming that it is a Scala 3+ symbol.我已经看到?=>符号出现在 Scala 代码和一些关于 Scala 3 的讨论帖子中,所以我假设它是一个 Scala 3+ 符号。 Nothing appears when searching through documentation or Google, but it looks like the syntactic sugar for the Function types, so maybe it relates to types and functions.通过文档或谷歌搜索时没有任何内容,但它看起来像是Function类型的语法糖,所以它可能与类型和函数有关。 What does it mean?这是什么意思?

The type (a: A, b: B, ..., z: Z)?=> R basically means (using a: A, b: B, ..., z: Z) => R (I believe the latter syntax was valid at one point, but not anymore).类型(a: A, b: B, ..., z: Z)?=> R基本上意味着(using a: A, b: B, ..., z: Z) => R (我相信后一种语法在某一时刻有效,但不再有效)。 All of those parameters become implicit parameters when you use ?=> .当您使用?=>时,所有这些参数都成为隐式参数。 Similarly, a function literal (a, b, ..., z)?=>... makes all of the parameters to that function implicit, and they can be passed implicitly to other methods later.类似地,function 文字(a, b, ..., z)?=>...使 function 的所有参数都是隐式的,以后可以将它们隐式地传递给其他方法。

Here's an example ( Scastie ):这是一个例子( Scastie ):

case class Foo(s: String)
case class Bar(i: Int)

def baz(xyzzy: (Foo, Bar) ?=> String): Unit =
  val foo = Foo("waldo")
  val bar = Bar(2)
  println(xyzzy(using foo, bar))

baz takes a context function. Note how xyzzy is called with the same syntax as a normal method taking a Foo and a Bar as implicit parameters (in Scala 3, blah(using bleh, bluh) is used to explicitly pass implicit arguments bleh and bluh instead of simply blah(bleh, bluh) like in Scala 2). baz采用上下文 function。请注意如何使用与将FooBar作为隐式参数的普通方法相同的语法调用xyzzy (在 Scala 3, blah(using bleh, bluh)用于显式传递隐式 arguments blehbluh而不是像 Scala 2 中那样简单地blah(bleh, bluh) )。

Here's one way we can call baz , by defining a method with implicit parameters:这是我们调用baz的一种方式,通过定义一个带有隐式参数的方法:

def foobar(using Foo, Bar) =
  val foo = summon[Foo]
  val bar = summon[Bar]
  s"Method - foo: $foo, bar: $bar"

baz(foobar)

We can also pass in a function literal.我们也可以传入一个 function 字面值。 There are no regular parameters, so it looks a little like a by-name parameter.没有常规参数,所以它看起来有点像名参数。 There are implicit instances of Foo and Bar available because of the (Foo, Bar)?=> type of the literal.由于文字的(Foo, Bar)?=>类型,存在可用的FooBar的隐式实例。

baz {
  val foo = summon[Foo]
  val bar = summon[Bar]
  s"Function literal - foo: $foo, bar: $bar"
}

You can also use ?=> in the function literal itself to name the implicit parameters without having to summon them and assign them to values.您还可以在 function 文字本身中使用?=>来命名隐式参数,而不必调用它们并将它们分配给值。 Since they're implicit, you can also call foobar from above because an implicit Foo and Bar are available (you can also do this in the second example despite not having named the parameters explicitly).由于它们是隐式的,您还可以从上面调用foobar ,因为隐式FooBar可用(尽管没有明确命名参数,您也可以在第二个示例中执行此操作)。

baz { (foo: Foo, bar: Bar) ?=>
  val fromMethod = foobar
  s"Explicit context function literal - foo: $foo, bar: $bar; $fromMethod"
}

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

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