代码改变世界

Scala 学习笔记

2014-05-13 16:07  briller  阅读(148)  评论(0编辑  收藏  举报

class Rational(n: Int, d: Int) {
  require(d != 0)
  override def toString = n +"/"+ d
}
The require method takes one boolean parameter. If the passed value is
true, require will return normally. Otherwise, require will prevent the ob-
ject from being constructed by throwing an IllegalArgumentException .

 

def byNameAssert(predicate: => Boolean) =
if (assertionsEnabled && !predicate)
throw new AssertionError

这个如果assertionsEnabled 为false的话   如果外面传入的为 5 > 3  ,那么不会进行计算

 

def boolAssert(predicate: Boolean) =
if (assertionsEnabled && !predicate)
throw new AssertionError

这个无论如何都会计算5 > 3,因为要进行变量类型的转化