<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Clean-up resource with Scala</title>
	<atom:link href="http://blog.tmorris.net/clean-up-resource-with-scala/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tmorris.net/clean-up-resource-with-scala/</link>
	<description>The weblog of Tony Morris</description>
	<pubDate>Sat, 19 May 2012 02:34:12 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
		<item>
		<title>By: Jason Zaugg</title>
		<link>http://blog.tmorris.net/clean-up-resource-with-scala/#comment-23481</link>
		<dc:creator>Jason Zaugg</dc:creator>
		<pubDate>Sat, 22 Nov 2008 14:00:38 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/?p=423#comment-23481</guid>
		<description>I'm not sure if the type signatures I have for map and flatMap are right -- I'm still on the path to Monadic enlightenment.

I assume we want to use one resource to create another, cleaning up along the way.

The trickiness seems to be converting from B to Resource[B] to return from the flatMap method. This needs to be done by the caller, where the type of B is known, and where the desired implicit conversion functions are in scope. There, map() can be used with a f:A=&#62;B, which is converted to f:A=&#62;Resource[B].



&lt;pre&gt;
sealed trait Resource[+T] {
  val value: T

  def close: Unit

  def use[X](f: T =&#62; X) = try {f(value)} finally {close}

  def map[B](f: T =&#62; Resource[B]): Resource[B] = use(f)

  def flatMap[B](f: T =&#62; B): Resource[B] = error("Compiler can't use the generic type 'B' to find an implicit function B =&#62; Resource[B]")

  def flatMapSpecific(f: T =&#62; java.io.InputStream): Resource[java.io.InputStream] = {
    import Resource._
    val b = use(f)
    b
  }
}

import java.io.InputStream

object Resource {
  def resource[T](t: T, c: =&#62; Unit) = new Resource[T] {
    val value = t

    def close = c
  }

  implicit def InputStreamResource(t: InputStream) = resource(t, t.close)
}


object Example {
  import Resource._

  def main(args: Array[String]) {
    val in = new java.io.FileInputStream("/etc/passwd")
    println(in.map(copyStream).map(copyStream) use (_.read.toChar))
  }

  def copyStream(i: InputStream): InputStream = {
    new ByteArrayInputStream(Array(i.read.toByte))
  }
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I&#8217;m not sure if the type signatures I have for map and flatMap are right &#8212; I&#8217;m still on the path to Monadic enlightenment.</p>
<p>I assume we want to use one resource to create another, cleaning up along the way.</p>
<p>The trickiness seems to be converting from B to Resource[B] to return from the flatMap method. This needs to be done by the caller, where the type of B is known, and where the desired implicit conversion functions are in scope. There, map() can be used with a f:A=&gt;B, which is converted to f:A=&gt;Resource[B].</p>
<pre>
sealed trait Resource[+T] {
  val value: T

  def close: Unit

  def use[X](f: T =&gt; X) = try {f(value)} finally {close}

  def map[B](f: T =&gt; Resource[B]): Resource[B] = use(f)

  def flatMap[B](f: T =&gt; B): Resource[B] = error("Compiler can't use the generic type 'B' to find an implicit function B =&gt; Resource[B]")

  def flatMapSpecific(f: T =&gt; java.io.InputStream): Resource[java.io.InputStream] = {
    import Resource._
    val b = use(f)
    b
  }
}

import java.io.InputStream

object Resource {
  def resource[T](t: T, c: =&gt; Unit) = new Resource[T] {
    val value = t

    def close = c
  }

  implicit def InputStreamResource(t: InputStream) = resource(t, t.close)
}

object Example {
  import Resource._

  def main(args: Array[String]) {
    val in = new java.io.FileInputStream("/etc/passwd")
    println(in.map(copyStream).map(copyStream) use (_.read.toChar))
  }

  def copyStream(i: InputStream): InputStream = {
    new ByteArrayInputStream(Array(i.read.toByte))
  }
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stephan Schmidt</title>
		<link>http://blog.tmorris.net/clean-up-resource-with-scala/#comment-23160</link>
		<dc:creator>Stephan Schmidt</dc:creator>
		<pubDate>Tue, 18 Nov 2008 12:35:33 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/?p=423#comment-23160</guid>
		<description>There are quite some posts about resource handling in Scala, good to see another one as it's an interesting topic (preventing bugs). Nice use of implicits. How would your solution deal with several resources?

Peace
-stpehan</description>
		<content:encoded><![CDATA[<p>There are quite some posts about resource handling in Scala, good to see another one as it&#8217;s an interesting topic (preventing bugs). Nice use of implicits. How would your solution deal with several resources?</p>
<p>Peace<br />
-stpehan</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Quintesse</title>
		<link>http://blog.tmorris.net/clean-up-resource-with-scala/#comment-23095</link>
		<dc:creator>Quintesse</dc:creator>
		<pubDate>Mon, 17 Nov 2008 15:03:54 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/?p=423#comment-23095</guid>
		<description>Just starting to learn Scala (from C/Java, no functional background whatsoever) and I have no idea what this code is doing.

Well, that's not true, I just have no idea HOW it is doing it ;)</description>
		<content:encoded><![CDATA[<p>Just starting to learn Scala (from C/Java, no functional background whatsoever) and I have no idea what this code is doing.</p>
<p>Well, that&#8217;s not true, I just have no idea HOW it is doing it <img src='http://blog.tmorris.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
</channel>
</rss>

