Scala中的范围可以匹配吗? [英] Can a range be matched in Scala?

查看:64
本文介绍了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?

推荐答案

使用Range保护:

val m = t match {
  case x if 0 until 10 contains x => true
  case _ => false
}

这篇关于Scala中的范围可以匹配吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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