在密封案例类声明内是否可以结合警戒条件和模式匹配?斯卡拉:我们可以结合警戒条件和模式匹配密封案例类声明
我意识到在匹配块中可能包含警戒条件,但我觉得在密封的案例类中预先定义这个条件是有益的。 这将允许开发人员定义严格的可能输入集合,编译器将在模式匹配时检查这些输入。
因此,在总结我希望能够做的是这样的等价物:
// create a set of pattern matchable cases with guards built in
sealed abstract class Args
case class ValidArgs1(arg1:Int,arg2:Int) if arg1>1 && arg2<10 extends Args
case class ValidArgs2(arg1:Int,arg2:Int) if arg1>5 && arg2<6 extends Args
case class InvalidArgs(arg1:Int,arg2:Int) if arg1<=1 && arg2>=10 extends Args
// the aim of this is to achieve pattern matching against an exhaustive set of
// pre-defined possibilities
def process(args:Args){
args match
{
case ValidArgs1 = > // do this
case ValidArgs2= > // do this
case InvalidArgs = > // do this
}
}
建议的功能不需要更多的Scala已经有了。它只是一个语法糖。 – ayvango 2012-08-13 18:46:46
@ayvango Scala绝对不支持静态验证。你是什么意思,为什么downvote? – 2012-08-13 19:23:46
主题启动器不需要静态验证。他只是希望通过提供的提示扩展无用的方法。 – ayvango 2012-08-13 19:37:55