title: Scala USING of USING
body: <p>I have csv with data like this:</p>
  
  <pre><code>1;57840
  2;57840
  3;57834
  ...
  </code></pre>
  
  <p>And simple scala code to read this data into Seq of Tuple2[Int,Int]</p>
  
  <p>Code:</p>
  
  <pre><code>case class Tick(Data : Tuple2[Int,Int]) {
    val tNum : Int = Data._1
    val tVal : Int = Data._2
  }
  
  case class TicksDs(Data : Seq[Tick]) {
    def size : Int = Data.size
  }
  
  class scvReader(CsvPath : String){
  
    def using[A &lt;: { def close(): Unit }, TicksDs](param: A)(f: A =&gt; TicksDs): TicksDs =
      try {
        f(param)
      } finally {
        //param.close()
      }
  
    def readTicks : TicksDs = {
      using(io.Source.fromFile(CsvPath)) { source =&gt;
         TicksDs(source.getLines.map{oneLine =&gt; (oneLine.split(";")(0).toInt-1,
                                                 oneLine.split(";")(1).toInt)}.toSeq.map(x =&gt; Tick(x._1,x._2)))
      }
    }
  
  }
  
  object CurryingFuncs extends App {
    val ticks : TicksDs = new scvReader("data/simple.csv").readTicks
    println("ticks.size="+ticks.size+" ticks(Class): "+ticks.getClass.getName)
    for (tick &lt;- ticks.Data) {
      println(tick.tNum+" "+tick.tVal+" "+tick.getClass.getName)
    }
  }
  </code></pre>
  
  <p>It's everything fine and this return me output:</p>
  
  <pre><code>ticks.size=50 ticks(Class): TicksDs
  0 57840 Tick
  1 57840 Tick
  2 57834 Tick
  3 57831 Tick
  ...
  </code></pre>
  
  <p>But when I uncomment line  //param.close() it raise error</p>
  
  <pre><code>Exception in thread "main" java.io.IOException: Stream Closed
      at java.io.FileInputStream.readBytes(Native Method)
  ...
  ...
  </code></pre>
  
  <p>Where is mistake and how fix it? Thanks.</p>
  
  <p>Fix (thanks to <a href="https://stackoverflow.com/users/4993128/jwvh">jwvh</a>):</p>
  
  <pre><code>    ...
        def using[A &lt;: { def close(): Unit }, B](param: A)(f: A =&gt; B): B =
          try f(param)
           finally param.close()
      ...
  
     ...
          using(io.Source.fromFile(CsvPath)) { source =&gt;
             TicksDs(source.getLines.map{oneLine =&gt; (oneLine.split(";")(0).toInt-1,
                                                     oneLine.split(";")(1).toInt)}.toList.map(x =&gt; Tick(x._1,x._2))) //was .toSeq now .toList
          }
  </code></pre>