Scala - 与条件语句的模式匹配?
是否可以做类似的事情:
def foo(x: Int): Boolean = {
case x > 1 => true
case x < 1 => false
}
Is it possible to do something like:
def foo(x: Int): Boolean = {
case x > 1 => true
case x < 1 => false
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请注意,您没有 x == 1 的情况......
Note that you don't have a case for x == 1 though...
我会写这样的东西:
I would write something like this:
由于您的示例中缺少 x == 1 的情况,因此我假设它的处理方式与 x
x
相同。 1.
.你可以这样做:
但是,这当然可以简化很多:
Since the case of
x == 1
is missing in your example, I assume that it is handled just the same asx < 1
.You can do it like this:
But then, this can of course be simplified a lot: