Pages

Monday, March 7, 2011

C# using is the loan pattern in Scala

One of the remarkably handy little features of C# is the using statement (http://msdn.microsoft.com/en-us/library/yh598w02.aspx). I miss it so in Java! However, Scala will let me make my own, complete with some nice additions. Using relies on IDisposeable for cleanup; for example purposes we'll use Java's Closeable. Our goal is to be able to write code similar to:
...
using(new FileWriter(file)) { fw => fw append code }
...
Simple but pretty cool. Our FileWriter could be any Closeable. This code breaks down like this:
using is just a Scala function:
package com.active.scala.util

import java.io.Closeable

object Loans {
 def using[T <: Closeable, R](c: T)(action: T => R): R = {
  try {
   action(c)
  } finally {
   if (null != c) c.close
  }
 }
}
And again with some notes:
Note that we can return a value from the Scala using if we wish. This is a simple example of the loan pattern in Scala. We can apply this to any resource we would typically have to use in a try { ... } finally { cleanup my resource } structure in Java.

30 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Their only obligation is to keep a record of how much energy they manage to save and what they discovered in the process. www.getsomedosh.com

    ReplyDelete
  3. I miss it so in Java! However, Scala will let me make my own, complete with some nice additions. Credit Restoration

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. You are one step away from a Personal Loan Approval! Complete the form below to secure your loan. ... Receive funds the same day you finalize your loan. Viva Loans

    ReplyDelete
  6. The Institute is entirely funded by donations from its staff, friends and from proceeds from sales at our book servicesnabblÄn

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. I think Sudhir Vankatesh found that Chicago loansharks were offering more extended payback periods, hence a lower rate. free-credit-report.net

    ReplyDelete
  9. And I just checked my credit card to make sure it's paid up. Thanks for the reminder. buy perfect money e-voucher with paypal

    ReplyDelete
  10. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work! no guarantor bad credit loans

    ReplyDelete
  11. Hey your blog is very nice, such useful information you are sharing. I really like your blog the information is very accurate and if you want to know more about free ad posting service,www.helpadya.com there is another website with best information.

    ReplyDelete
  12. very interesting post.this is my first time visit here.i found so many interesting stuff in your blog especially its discussion..thanks for the post!
    Samuel

    ReplyDelete
  13. A debt of gratitude is in order for the blog entry amigo! Keep them coming...
    www.loanmoney.com.sg

    ReplyDelete
  14. This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post!
    online installment loans

    ReplyDelete
  15. Very nice article, I enjoyed reading your post, very nice share, I want to twit this to my followers. Thanks!. slickcashloan.com

    ReplyDelete

  16. PEC is a very authentic education department of Punjab province and every year this education department is performing brilliantly for the progress of education in Punjab and its result is this that every year students from every single district appear in the 5th and 8th Class exams conducted by PEC and for this PEC has set a very suitable procedure in which is issues registration forms for Grade 5th Class and Grade 8th Class.

    board 8th class result 2018
    result of class 5th 2018
    8th class board result 2018

    ReplyDelete
  17. Thanks a lot for sharing us about this update. Hope you will not get tired on making posts as informative as this. http://www.slickcashloan.com

    ReplyDelete
  18. Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know. slickcashloan.com/cash-advance/

    ReplyDelete
  19. i read a lot of stuff and i found that the way of writing to clearifing that exactly want to say was very good so i am impressed and ilike to come again in future.. slickcashloan

    ReplyDelete
  20. Installment loans give you just the money you need to get out of financial jams that arise when you least expect them. click over here

    ReplyDelete
  21. I really thank you for the valuable info on this great subject and look forward to more great posts. Thanks a lot for enjoying this beauty article with me. I am appreciating it very much! Looking forward to another great article. Good luck to the author! All the best! loans while waiting for inheritance

    ReplyDelete
  22. i read a lot of stuff and i found that the way of writing to clearifing that exactly want to say was very good so i am impressed and ilike to come again in future.. visit slick cash loan today

    ReplyDelete
  23. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!. Money Trumpet

    ReplyDelete
  24. Perfect Money Login page. Log in to your PerfectMoney.com online account. An online financial service that allows users who want to pay instantly using secure transfers.
    Perfect Money Login

    ReplyDelete
  25. I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful.Best Mortgage Rate in Mississauga

    ReplyDelete
  26. This is a great article thanks for sharing this informative information.. I will visit your blog regularly for some latest post.Blended Mortgage

    ReplyDelete
  27. Find the best Fixed mortgage rate in Peterborough that work perfectly for you. We make it easy to compare rates in Peterborough big banks and top brokers for free.Best Mortgage Rate in Peterborough

    ReplyDelete
  28. "Great article! The comparison of the loan pattern in C# and Scala is very insightful. It’s fascinating to see how different languages handle resource management. Thanks for sharing!"


    Digital Marketing Course In Hyderabad

    ReplyDelete