title: Scala - dynamically choose function
body: <p>I'm trying to dynamically choose a function, as follows:</p>
  
  <pre><code>val yld: (Nothing =&gt; Object) = {
            column.getType match {
              case PrimitiveType.PrimitiveTypeName.BINARY =&gt; new String(creader.getBinary.getBytes)
              case PrimitiveType.PrimitiveTypeName.BOOLEAN =&gt; creader.getBoolean
              case PrimitiveType.PrimitiveTypeName.DOUBLE =&gt; creader.getDouble
              case PrimitiveType.PrimitiveTypeName.FLOAT =&gt; creader.getFloat
              case PrimitiveType.PrimitiveTypeName.INT32 =&gt; creader.getInteger
              case PrimitiveType.PrimitiveTypeName.INT64 =&gt; creader.getLong
              case PrimitiveType.PrimitiveTypeName.INT96 =&gt; new String(creader.getBinary.getBytes)
              case PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY =&gt; new String(creader.getBinary.getBytes)
            }
          }
  </code></pre>
  
  <p><strong>Question</strong>: However, the inner functions (e.g. creader.getBoolean, etc.) are (obviously) executed at the time of the match. How do I capture those functions as objects to execute at a later time rather than having them execute when yld is assigned? </p>
  
  <p><strong>Context:</strong></p>
  
  <p>This is so that in a later <code>for</code> loop, I don't have to match again:</p>
  
  <pre><code>for (i &lt;- 0L to 900000) {
      println(yld)
  }
  </code></pre>
  
  <p>Because my object <code>creader</code> is an iterator on column-stored data, being able to make the type decision only one time should be more efficient than having to make it every time, i.e.</p>
  
  <pre><code>for (i &lt;- 0L to 900000) {      
       column.getType match {
          case PrimitiveType.PrimitiveTypeName.BINARY =&gt; println(new String(creader.getBinary.getBytes))
          case PrimitiveType.PrimitiveTypeName.BOOLEAN =&gt; println(creader.getBoolean)
          case PrimitiveType.PrimitiveTypeName.DOUBLE =&gt; println(creader.getDouble)
          case PrimitiveType.PrimitiveTypeName.FLOAT =&gt; println(creader.getFloat)
          case PrimitiveType.PrimitiveTypeName.INT32 =&gt; println(creader.getInteger)
          case PrimitiveType.PrimitiveTypeName.INT64 =&gt; println(creader.getLong)
          case PrimitiveType.PrimitiveTypeName.INT96 =&gt; println(new String(creader.getBinary.getBytes))
          case PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY =&gt; println(new String(creader.getBinary.getBytes))
        }          
  }
  </code></pre>