【问题标题】:Scala way of if else with two conditional variables带有两个条件变量的if else的Scala方式
【发布时间】:2016-08-04 20:38:25
【问题描述】:

我有以下 Java sn-p(其中 ab 是期货):

if (a.isEmpty && b.isEmpty) func(list)
else if (a.isEmpty) func(list, b)
else if (b.isEmpty) func(a, list)
else func(a, list, b)

我拥有函数“func”的所有实现。 有没有合适的方法在 Scala 中编写这个或者这足够好?

【问题讨论】:

  • 我正在扩展一个已经有实现的现有类。
  • 什么是ablistfunc
  • 假设 a 和 b 不等于列表的类型,它是类型安全的并且符合重载。
  • 即使它们都是相同的类型,它也是类型安全的。有三个重载,分别接受一个、两个和三个相同类型的参数。

标签: scala if-statement coding-style modularity


【解决方案1】:

假设ab 是列表,这似乎与list 相关:

(a, b) match {
  case (Nil, Nil) => func(list)
  case (Nil, _)   => func(list, b)
  case (_, Nil)   => func(a, list)
  case _          => func(a, b, list)
}

【讨论】:

  • 其实是期货
  • 好的。早点把一切都说出来对你很有用!
  • 如果ab 是Futures,我不确定isEmpty 的含义。
  • 您确定它们不是期货列表吗? Future 没有 isEmpty 方法
猜你喜欢
  • 2021-12-08
  • 2023-01-11
  • 1970-01-01
  • 1970-01-01
  • 2022-07-08
  • 1970-01-01
  • 2016-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多