Scala 空默认闭包?

发布于 2024-10-06 18:50:40 字数 346 浏览 4 评论 0原文

只是一个简单的问题,我似乎无法找到答案。

我在 Scala 中有一个方法定义,如下所示:

def execute(goals: List[String],
            profiles: List[String] = List(),
            loggingCallback: (String) => Unit = { _ => }): Result = {
    // method body
    loggingCallback("a message")
}

我想知道是否有更好的方法来指定默认的空闭包。问题不在于如何实现日志记录,这只是一个示例。

just a quick question I seem to be unable to find an answer to.

I have a method definition in Scala that looks like this:

def execute(goals: List[String],
            profiles: List[String] = List(),
            loggingCallback: (String) => Unit = { _ => }): Result = {
    // method body
    loggingCallback("a message")
}

I would like to know whether there is a better way to specify a default empty closure. The question is not about how to implement logging, this is just an example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

書生途 2024-10-13 18:50:40

你的解决方案很好。您可以为 Function1[X, Unit] 引入类型别名;按照凯文的回答使用 () ,并删除不必要的括号。

scala> type Effect[-A] = (A => Unit)
defined type alias Effect

scala> def foo(f: Effect[String] = _ => ()) = ()
foo: (f: (String) => Unit)Unit

您还可以定义一个 noop 函数:

scala> val noop = (a: Any) => ()
noop: (Any) => Unit = <function1>

scala> def foo(f: Effect[String] = noop) = ()

Your solution is fine. You could introduce a type alias for Function1[X, Unit]; use () as per Kevin's answer, and drop unnecessary parens.

scala> type Effect[-A] = (A => Unit)
defined type alias Effect

scala> def foo(f: Effect[String] = _ => ()) = ()
foo: (f: (String) => Unit)Unit

You could also define a noop function:

scala> val noop = (a: Any) => ()
noop: (Any) => Unit = <function1>

scala> def foo(f: Effect[String] = noop) = ()
水水月牙 2024-10-13 18:50:40

()是unit的一个实例,所以这应该可以解决问题:

def execute(
  goals: List[String],
  profiles: List[String] = Nil,
  loggingCallback: (String) => Unit = { _ => () }): Result =
{
  // method body
  loggingCallback("a message")
  // do something returning a Result
}

update

如果某些东西是可选的,那么明确地声明通常更有意义,这也产生更好的自文档化代码:

def execute(
  goals: List[String],
  profiles: List[String] = Nil,
  loggingCallback: Option[(String) => Unit] = None): Result =
{
  // method body
  loggingCallback forEach { _.apply("a message") }
  // do something returning a Result
}

update 2

像这样的 DSL 式情况也是我会容忍在 Scala 中使用 null 的极少数情况之一:

def execute(
  goals: List[String],
  profiles: List[String] = Nil,
  loggingCallback: (String) => Unit = null
): Result = {
  // method body
  val log = Option(loggingCallback) getOrElse {_ => ()}
  log("a message")
  // do something returning a Result
}

请注意Option(loggingCallback)立即将可空loggingCallback转换为一个很好的类型安全Option,然后< code>getOrElse 提供后备替代方案。

The value () is an instance of unit, so this should do the trick:

def execute(
  goals: List[String],
  profiles: List[String] = Nil,
  loggingCallback: (String) => Unit = { _ => () }): Result =
{
  // method body
  loggingCallback("a message")
  // do something returning a Result
}

update

If something is optional, then it often makes more sense to state so explicitly, this also results in better self-documenting code:

def execute(
  goals: List[String],
  profiles: List[String] = Nil,
  loggingCallback: Option[(String) => Unit] = None): Result =
{
  // method body
  loggingCallback forEach { _.apply("a message") }
  // do something returning a Result
}

update 2

DSL-esque situations like this are also one of the very few situations where I'll condone the use of null in Scala:

def execute(
  goals: List[String],
  profiles: List[String] = Nil,
  loggingCallback: (String) => Unit = null
): Result = {
  // method body
  val log = Option(loggingCallback) getOrElse {_ => ()}
  log("a message")
  // do something returning a Result
}

Note the Option(loggingCallback) to immediately convert the nullable loggingCallback into a nice type-safe Option, then getOrElse to provide a fallback alternative.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文