scala> //#215 scala> /************************************************ | Example 1 : An assert function without | By-name parameter | *************************************************/ | val enableAssert = true enableAssert: Boolean = true scala> def customAssert(checkPredicate: () => Boolean) = { | if (enableAssert) { | if (checkPredicate() == false) { | println("Throwing AssertionError..") | throw new AssertionError | } | } | } customAssert: (checkPredicate: () => Boolean)Unit scala> scala> //Ideally we would like to call like this. scala> //But this do not work scala> customAssert(1 > 5) <console>:28: error: type mismatch; found : Boolean(false) required: () => Boolean customAssert(1 > 5) ^ scala> scala> //This does work scala> customAssert(() => 1 > 5) Throwing AssertionError.. java.lang.AssertionError at customAssert(<console>:29) ... 48 elided scala> scala> /************************************************ | Example 2 : An assert function with | By-name parameter | *************************************************/ | val enableAssert = true enableAssert: Boolean = true scala> //Note here the previous example had scala> // - checkPredicate: () => Boolean scala> //Whereas, in the current example we have removed scala> //Parenthesis. This method is referred as By-name scala> //parameter scala> // - checkPredicate: => Boolean scala> scala> def customAssert(checkPredicate: => Boolean) = { | if (enableAssert) { | if (checkPredicate == false) { | println("Throwing AssertionError...") | throw new AssertionError | } | } | } customAssert: (checkPredicate: => Boolean)Unit scala> scala> customAssert(1 > 5) Throwing AssertionError... java.lang.AssertionError at customAssert(<console>:29) ... 48 elided