通过中缀表示法调用柯里化方法 [英] Curried method call by infix notation

查看:56
本文介绍了通过中缀表示法调用柯里化方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了以下类,如scala.Predef.Ensuring:

scala> :paste
// Entering paste mode (ctrl-D to finish)

implicit class Doing[A](val self: A) extends AnyVal {
  def doing(body: => Unit): A = { body; self }
  def doWhen(pred: A => Boolean)(body: => Unit): A =
    doing{ if (pred(self)) body }
}

// Exiting paste mode, now interpreting.

defined class Doing

但是 doWhen 方法不适用于中缀表示法.

but doWhen method doesn't work with infix notation.

scala> "abc" doWhen(_ == "abc") { println("do") }
<console>:13: error: missing parameter type for expanded function ((x$1) => x$1.$eq$eq("abc"))
       "abc" doWhen(_ == "abc") { println("do") }
                    ^

scala> "abc".doWhen(_ == "abc") { println("do") }
do
res1: String = abc

如何通过中缀样式使用doWhen?

How to use doWhen by infix style?

推荐答案

如果我了解 Scala 参考 正确,这是不允许的:中缀操作的右侧可以是一个表达式或括号中的多个表达式,您可以'没有多个参数列表.

If I understand the relevant section of Scala Reference correctly, it's simply not allowed: the right-hand side of an infix operation can either be an expression or several expressions in parentheses, you can't have multiple argument lists.

这篇关于通过中缀表示法调用柯里化方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆