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:

lingmaaki said...

An example on ...C# using statement

Ling

albina N muro said...
This comment has been removed by the author.
albina N muro said...

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

albina N muro said...

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

Anna Schafer said...
This comment has been removed by the author.
Elizabeth J. Neal said...

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

Anna Schafer said...

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

Anna Schafer said...
This comment has been removed by the author.
Richard C. Lambert said...

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

Robert said...

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

Robert said...

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

Helpadya said...

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.

Anna Schafer said...

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

Anna Schafer said...

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

Unknown said...

Brilliant data. carry on the nice work. Personal Loan in Bangalore, Business Loan in Bangalore, Car Loan in Bangalore, Home Loan in Bangalore

Anna Schafer said...

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

Anna Schafer said...

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

Katrina Kaif said...


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

Anna Schafer said...

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

Elizabeth J. Neal said...

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/

Richard C. Lambert said...

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

nickmiddleton010 said...

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

brittanymlemay said...

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

Robert D. Lennox said...

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

Unknown said...

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

Betting wala said...

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

Shivam said...

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

Shivam said...

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

Shivam said...

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

Gouse said...

"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

Post a Comment