What Automatic Resource Management alternatives ex

2019-01-09 22:30发布

I have seen many examples of ARM (automatic resource management) on the web for Scala. It seems to be a rite-of-passage to write one, though most look pretty much like one another. I did see a pretty cool example using continuations, though.

At any rate, a lot of that code has flaws of one type or another, so I figured it would be a good idea to have a reference here on Stack Overflow, where we can vote up the most correct and appropriate versions.

7条回答
Root(大扎)
2楼-- · 2019-01-09 23:08

Daniel, good you asked this. I am myself intrigued after seeing James Iry's code. I see a gradual 4 step evolution for doing ARM in Scala:

  1. No ARM: Dirt
  2. Only closures: Better, but multiple nested blocks
  3. Continuation Monad: Use For to flatten the nesting, but unnatural separation in 2 blocks
  4. Direct style continuations: Nirava, aha! This is also the most type-safe alternative: a resource outside withResource block will be type error.

What I would really love to see is a presentation describing these. It will be very educational and should convince the begots that there is a world beyond Monads :)

查看更多
登录 后发表回答