范围可以在 Scala 中匹配吗? [英] Can a range be matched in Scala?
本文介绍了范围可以在 Scala 中匹配吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在 Scala 中匹配一系列值?
Is it possible to match a range of values in Scala?
例如:
val t = 5
val m = t match {
0 until 10 => true
_ => false
}
如果 t
介于 0 和 10 之间,则
m
将为 true
,否则为 false.这一点当然是行不通的,但是有没有什么办法可以实现呢?
m
would be true
if t
was between 0 and 10, but false otherwise. This little bit doesn't work of course, but is there any way to achieve something like it?
推荐答案
Guard using Range
:
Guard using Range
:
val m = t match {
case x if 0 until 10 contains x => true
case _ => false
}
这篇关于范围可以在 Scala 中匹配吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文