What is the best way to write the following in Scala? It doesn't look quite right to me – first the forward declaration of the 2 vals, then the long PrintWriter
creation line, then thefinally
block. The only thing that's idiomatic, is the catch
block…
val outputStream = Try(fs.create(tmpFile)) val writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream.get))) if (outputStream.isFailure) { logger.error(s"Couldn't open file: $tmpFile") } try { features.foreach { case (sectionName, modelRDD) => writer.append("{" + sectionName + ", " + modelRDD.getNumPartitions + "}") } } catch { case e: Exception => logger.error(s"Got exception", e) throw e } finally { outputStream.get.close() writer.close() }