From book 'Programming in Scala':
var assertionsEnabled = true
def myAssert(predicate: () => Boolean) =
if (assertionsEnabled && !predicate())
throw new AssertionError
myAssert(() => 5 > 3)
Using empty parameter list is awkward. Scala provides by-name parameter to solve this.
def byNameAssert(predicate: => Boolean) =
if (assertionsEnabled && !predicate)
throw new AssertionError
byNameAssert(5 > 3)
I have a confusion in this discussion.myAssert takes in a parameter which is a function, which in turn takes no parameter and returns a boolean.
What is input type for byNameAssert?Is it same as myAssert?To me,it seems an expression which evaluates to a boolean value, and having it in by-name form means that expression is evaluated whenever it is called and not when being passed to byNameAssert.But then it is not same as input type of myAssert. And if that is the case, then byNameAssert and myAssert are quite different.