带有中缀符号的有趣行为

带有中缀符号的有趣行为

本文介绍了带有中缀符号的有趣行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时会躲在你的电脑屏幕后面试图摆脱你的女朋友.然而,我发现 Scala 有时和我的女孩一模一样......

One sometimes try to get away from your girlfriend by hiding behind your computer screen. However, I find that Scala is sometimes exactly like my girl...

这将打印两个列表之间的交集:

This prints the intersection between two lists:

  val boys = List(Person("John"), Person("Kim"), Person("Joe"), Person("Piet"), Person("Alex"))

  val girls = List(Person("Jana"), Person("Alex"), Person("Sally"), Person("Kim"))

  println("Unisex names: " + boys.intersect(girls))

这绝对不会打印任何内容:

This prints absolutely nothing:

  val boys = List(Person("John"), Person("Kim"), Person("Joe"), Person("Piet"), Person("Alex"))

  val girls = List(Person("Jana"), Person("Alex"), Person("Sally"), Person("Kim"))

  println("Unisex names: " + boys intersect girls)

没有编译器警告,并且该语句绝对不会向控制台打印任何内容.有人能解释一下吗(我宿醉了),为什么会这样.

There are no compiler warnings and the statement prints absolutely nothing to the console. Could someone please explain gently (I have a hangover), why this is so.

推荐答案

它变得如此脱糖:

println("Unisex names: ".+(boys).intersect(girls))

然后根据 -Xprint:typer 编译器选项,它会像这样重写:

then according to the -Xprint:typer compiler option it gets rewritten like this:

println(augmentString("Unisex names: ".+(boys.toString)).intersect[Any](girls))

其中 augmentString 是从 String 类型到 StringOps 类型的隐式转换,它提供了 intersect 方法.

where augmentString is an implicit conversion from type String to StringOps, which provides the intersect method.

这篇关于带有中缀符号的有趣行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:34