104

我在网上看到了很多 Scala 的 ARM(自动资源管理)示例。写一个似乎是一种仪式,尽管大多数看起来很像。不过,我确实看到了一个非常酷的使用延续的例子。

无论如何,很多代码都有这样或那样的缺陷,所以我认为在 Stack Overflow 上有一个参考是个好主意,在这里我们可以投票选出最正确和最合适的版本。

4

10 回答 10

75

Chris Hansen 于 2009 年 3 月 26 日发表的博客文章 'ARM Blocks in Scala: Revisited'谈到了 Martin Odersky 的FOSDEM 演示文稿的幻灯片 21 。下一个块直接取自幻灯片 21(经许可):

def using[T <: { def close() }]
    (resource: T)
    (block: T => Unit) 
{
  try {
    block(resource)
  } finally {
    if (resource != null) resource.close()
  }
}

--结束报价--

然后我们可以这样调用:

using(new BufferedReader(new FileReader("file"))) { r =>
  var count = 0
  while (r.readLine != null) count += 1
  println(count)
}

这种方法的缺点是什么?这种模式似乎可以解决我需要自动资源管理的 95% 的问题......

编辑:添加代码片段


Edit2:扩展设计模式 - 从 pythonwith语句和寻址中获取灵感:

  • 在块之前运行的语句
  • 根据托管资源重新抛出异常
  • 用一个 using 语句处理两个资源
  • 通过提供隐式转换和Managed类进行资源特定处理

这是 Scala 2.8。

trait Managed[T] {
  def onEnter(): T
  def onExit(t:Throwable = null): Unit
  def attempt(block: => Unit): Unit = {
    try { block } finally {}
  }
}

def using[T <: Any](managed: Managed[T])(block: T => Unit) {
  val resource = managed.onEnter()
  var exception = false
  try { block(resource) } catch  {
    case t:Throwable => exception = true; managed.onExit(t)
  } finally {
    if (!exception) managed.onExit()
  }
}

def using[T <: Any, U <: Any]
    (managed1: Managed[T], managed2: Managed[U])
    (block: T => U => Unit) {
  using[T](managed1) { r =>
    using[U](managed2) { s => block(r)(s) }
  }
}

class ManagedOS(out:OutputStream) extends Managed[OutputStream] {
  def onEnter(): OutputStream = out
  def onExit(t:Throwable = null): Unit = {
    attempt(out.close())
    if (t != null) throw t
  }
}
class ManagedIS(in:InputStream) extends Managed[InputStream] {
  def onEnter(): InputStream = in
  def onExit(t:Throwable = null): Unit = {
    attempt(in.close())
    if (t != null) throw t
  }
}

implicit def os2managed(out:OutputStream): Managed[OutputStream] = {
  return new ManagedOS(out)
}
implicit def is2managed(in:InputStream): Managed[InputStream] = {
  return new ManagedIS(in)
}

def main(args:Array[String]): Unit = {
  using(new FileInputStream("foo.txt"), new FileOutputStream("bar.txt")) { 
    in => out =>
    Iterator continually { in.read() } takeWhile( _ != -1) foreach { 
      out.write(_) 
    }
  }
}
于 2010-02-08T03:59:21.560 回答
63

丹尼尔,

我最近刚刚部署了用于自动资源管理的 scala-arm 库。你可以在这里找到文档:https ://github.com/jsuereth/scala-arm/wiki

该库支持三种使用方式(当前):

1)命令式/用于表达:

import resource._
for(input <- managed(new FileInputStream("test.txt")) {
// Code that uses the input as a FileInputStream
}

2) 单子风格

import resource._
import java.io._
val lines = for { input <- managed(new FileInputStream("test.txt"))
                  val bufferedReader = new BufferedReader(new InputStreamReader(input)) 
                  line <- makeBufferedReaderLineIterator(bufferedReader)
                } yield line.trim()
lines foreach println

3) 定界延续风格

这是一个“echo”tcp 服务器:

import java.io._
import util.continuations._
import resource._
def each_line_from(r : BufferedReader) : String @suspendable =
  shift { k =>
    var line = r.readLine
    while(line != null) {
      k(line)
      line = r.readLine
    }
  }
reset {
  val server = managed(new ServerSocket(8007)) !
  while(true) {
    // This reset is not needed, however the  below denotes a "flow" of execution that can be deferred.
    // One can envision an asynchronous execuction model that would support the exact same semantics as below.
    reset {
      val connection = managed(server.accept) !
      val output = managed(connection.getOutputStream) !
      val input = managed(connection.getInputStream) !
      val writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(output)))
      val reader = new BufferedReader(new InputStreamReader(input))
      writer.println(each_line_from(reader))
      writer.flush()
    }
  }
}

该代码使用 Resource 类型特征,因此它能够适应大多数资源类型。它有一个回退,可以通过 close 或 dispose 方法对类使用结构类型。请查看文档,如果您想添加任何方便的功能,请告诉我。

于 2010-08-09T13:42:04.180 回答
18

这是使用延续的James Iry解决方案:

// standard using block definition
def using[X <: {def close()}, A](resource : X)(f : X => A) = {
   try {
     f(resource)
   } finally {
     resource.close()
   }
}

// A DC version of 'using' 
def resource[X <: {def close()}, B](res : X) = shift(using[X, B](res))

// some sugar for reset
def withResources[A, C](x : => A @cps[A, C]) = reset{x}

以下是用于比较的有和没有延续的解决方案:

def copyFileCPS = using(new BufferedReader(new FileReader("test.txt"))) {
  reader => {
   using(new BufferedWriter(new FileWriter("test_copy.txt"))) {
      writer => {
        var line = reader.readLine
        var count = 0
        while (line != null) {
          count += 1
          writer.write(line)
          writer.newLine
          line = reader.readLine
        }
        count
      }
    }
  }
}

def copyFileDC = withResources {
  val reader = resource[BufferedReader,Int](new BufferedReader(new FileReader("test.txt")))
  val writer = resource[BufferedWriter,Int](new BufferedWriter(new FileWriter("test_copy.txt")))
  var line = reader.readLine
  var count = 0
  while(line != null) {
    count += 1
    writer write line
    writer.newLine
    line = reader.readLine
  }
  count
}

这是 Tiark Rompf 的改进建议:

trait ContextType[B]
def forceContextType[B]: ContextType[B] = null

// A DC version of 'using'
def resource[X <: {def close()}, B: ContextType](res : X): X @cps[B,B] = shift(using[X, B](res))

// some sugar for reset
def withResources[A](x : => A @cps[A, A]) = reset{x}

// and now use our new lib
def copyFileDC = withResources {
 implicit val _ = forceContextType[Int]
 val reader = resource(new BufferedReader(new FileReader("test.txt")))
 val writer = resource(new BufferedWriter(new FileWriter("test_copy.txt")))
 var line = reader.readLine
 var count = 0
 while(line != null) {
   count += 1
   writer write line
   writer.newLine
   line = reader.readLine
 }
 count
}
于 2010-02-13T22:08:15.070 回答
16

目前Scala 2.13终于支持:try with resources通过使用Using :),示例:

val lines: Try[Seq[String]] =
  Using(new BufferedReader(new FileReader("file.txt"))) { reader =>
    Iterator.unfold(())(_ => Option(reader.readLine()).map(_ -> ())).toList
  }

或使用Using.resource避免Try

val lines: Seq[String] =
  Using.resource(new BufferedReader(new FileReader("file.txt"))) { reader =>
    Iterator.unfold(())(_ => Option(reader.readLine()).map(_ -> ())).toList
  }

您可以从Using doc中找到更多示例。

用于执行自动资源管理的实用程序。它可用于执行使用资源的操作,然后以与创建资源相反的顺序释放资源。

于 2019-06-11T16:34:41.160 回答
7

我看到了在 Scala 中做 ARM 的一个渐进的 4 步演变:

  1. 没有 ARM:污垢
  2. 只有闭包:更好,但有多个嵌套块
  3. Continuation Monad:使用 For 将嵌套变平,但不自然的分隔为 2 个块
  4. 直接风格延续:Nirava,啊哈!这也是最类型安全的替代方案:withResource 块之外的资源将是类型错误。
于 2010-02-08T03:11:44.677 回答
6

更好的文件中包含轻量级(10 行代码)ARM。见:https ://github.com/pathikrit/better-files#lightweight-arm

import better.files._
for {
  in <- inputStream.autoClosed
  out <- outputStream.autoClosed
} in.pipeTo(out)
// The input and output streams are auto-closed once out of scope

如果您不想要整个库,以下是它的实现方式:

  type Closeable = {
    def close(): Unit
  }

  type ManagedResource[A <: Closeable] = Traversable[A]

  implicit class CloseableOps[A <: Closeable](resource: A) {        
    def autoClosed: ManagedResource[A] = new Traversable[A] {
      override def foreach[U](f: A => U) = try {
        f(resource)
      } finally {
        resource.close()
      }
    }
  }
于 2015-10-10T17:50:37.883 回答
1

如何使用 Type 类

trait GenericDisposable[-T] {
   def dispose(v:T):Unit
}
...

def using[T,U](r:T)(block:T => U)(implicit disp:GenericDisposable[T]):U = try {
   block(r)
} finally { 
   Option(r).foreach { r => disp.dispose(r) } 
}
于 2017-07-06T17:42:12.780 回答
1

另一种选择是 Choppy 的 Lazy TryClose monad。数据库连接非常好:

val ds = new JdbcDataSource()
val output = for {
  conn  <- TryClose(ds.getConnection())
  ps    <- TryClose(conn.prepareStatement("select * from MyTable"))
  rs    <- TryClose.wrap(ps.executeQuery())
} yield wrap(extractResult(rs))

// Note that Nothing will actually be done until 'resolve' is called
output.resolve match {
    case Success(result) => // Do something
    case Failure(e) =>      // Handle Stuff
}

并使用流:

val output = for {
  outputStream      <- TryClose(new ByteArrayOutputStream())
  gzipOutputStream  <- TryClose(new GZIPOutputStream(outputStream))
  _                 <- TryClose.wrap(gzipOutputStream.write(content))
} yield wrap({gzipOutputStream.flush(); outputStream.toByteArray})

output.resolve.unwrap match {
  case Success(bytes) => // process result
  case Failure(e) => // handle exception
}

更多信息在这里:https ://github.com/choppythelumberjack/tryclose

于 2018-01-17T14:36:57.237 回答
0

这是@chengpohi 的答案,经过修改,它适用于 Scala 2.8+,而不仅仅是 Scala 2.13(是的,它也适用于 Scala 2.13):

def unfold[A, S](start: S)(op: S => Option[(A, S)]): List[A] =
  Iterator
    .iterate(op(start))(_.flatMap{ case (_, s) => op(s) })
    .map(_.map(_._1))
    .takeWhile(_.isDefined)
    .flatten
    .toList

def using[A <: AutoCloseable, B](resource: A)
                                (block: A => B): B =
  try block(resource) finally resource.close()

val lines: Seq[String] =
  using(new BufferedReader(new FileReader("file.txt"))) { reader =>
    unfold(())(_ => Option(reader.readLine()).map(_ -> ())).toList
  }
于 2019-07-10T03:07:01.833 回答
0

虽然Using没问题,但我更喜欢资源组合的单子风格。Twitter UtilManaged非常好,除了它的依赖项和不是很完善的 API。

为此,我为 Scala 2.12、2.13 和 3.0.0发布了https://github.com/dvgica/managerial 。很大程度上基于 Twitter UtilManaged代码,没有依赖关系,一些 API 改进是受猫效应启发的Resource

简单的例子:

import ca.dvgi.managerial._
val fileContents = Managed.from(scala.io.Source.fromFile("file.txt")).use(_.mkString)

但图书馆的真正优势在于通过 for comprehension 编写资源

让我知道你的想法!

于 2021-06-04T13:16:48.490 回答