Showing posts with label by-name-parameter. Show all posts
Showing posts with label by-name-parameter. Show all posts

Wednesday, December 16, 2009

By-name-parameter to Function

Today's topic is related to Defining Custom Control Structures. By name parameters are not function objects in the same way as other functions. Normally you need to invoke a function:
  1. scala> def exec(func: () => Int) = func()
  2. exec: (() => Int)Int
  3. // exec invokes the function so 10 is the result
  4. scala> exec (() => 10)  
  5. res1: Int = 10
  6. scala> def exec(func: () => Int) = func
  7. exec: (() => Int)() => Int
  8. // here exec returns the function:
  9. scala> exec (() => 10)                 
  10. res2: () => Int = <function>

Compare that to a by-name-parameter function:
  1. scala> def exec(func: => Int) = func   
  2. exec: (=> Int)Int
  3. // by-name-parameters are executed when they are referenced
  4. // so the result is 10
  5. scala> exec {10}                    
  6. res3: Int = 10
  7. // This is not legal because by-name-parameters
  8. // are not normal functions
  9. scala> def exec(func: => Int) = func()
  10. <console>:4: error: func of type Int does not take parameters
  11.        def exec(func: => Int) = func()

So the issue is how can you pass a by-name-parameter to a method that takes a function as a parameter without having to do:
  1. scala> def takesFunc(func: () => Int) = println(func())
  2. takesFunc: (() => Int)Unit
  3. scala> def send(x: => Int) = takesFunc(() => x) 
  4. send: (=> Int)Unit
  5. scala> send{2}
  6. 2

the alternative syntax is:
  1. scala> def send(x: => Int) = takesFunc (x _)   
  2. send: (=> Int)Unit
  3. scala> send {2}
  4. 2