Showing posts with label abaaz| By-name parameters. Show all posts
Showing posts with label abaaz| By-name parameters. Show all posts

Saturday, October 1, 2016

FunSuite Example...

scala> import org.scalatest.FunSuite
import org.scalatest.FunSuite

scala> class ElementSuite extends FunSuite {
     |   // This is a function literal passed as by-name
     |   // parameter #297
     |   test("No should be 2") {
     |     val no = 5
     |     assert(no == 2)
     |   }
     | }
defined class ElementSuite

scala> (new ElementSuite).execute()
ElementSuite:
- No should be 2 *** FAILED ***
  5 did not equal 2 (<console>:17)

Wednesday, September 21, 2016

By-name Parameters : Example...

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

Thursday, September 15, 2016

Short Circuit Operators & 'by-name parameters' : Example...

'||' and '&&' are Short circuit Operators #126
scala> def math():Boolean = {
     |     println("Math -> False")
     |     false
     | }
math: ()Boolean

scala> def science():Boolean = {
     |     println("Science -> True")
     |     true
     | }
science: ()Boolean

scala>

scala> println("Case 1")
Case 1

scala> //Note here only math() is evaluated

scala> //ie... the evaluation for science() is declined

scala> //This is achieved through the facility for Methods(in our case &&)

scala> //called by-name parameters

scala> val res = math() && science()
Math -> False
res: Boolean = false

scala>

scala> println("Case 2")
Case 2

scala> //Note here both math() & science are evaluated

scala> val res = science() && math()
Science -> True
Math -> False
res: Boolean = false