Thursday, 7 March 2013

Loan pattern in Scala

Loan pattern is really coming from loaning some context to some block, which doesn't care about lifecycle of the context, just using it (as you can find isn't not real world loan, but it work in magic Scala's borders;-)).
A lot of boilerplate in Java can't be avoided when we have to work with connection, resources, threads and etc.

for example:

Essence is writing to stream, 1 line of code in our case, others aren't part of payload, just a ritual. It's easy to ignore the extra work when we do it once, but reaping it pushes to get ride of such boring staff. Lets introduce high order function which incapsulates the stream management:
and the way how we do use it.
Actually that is one of the most used patterns in Java library wrapper for Scala. Sometimes it becomes the main style in library itself. For example this gist from Scala STM example:
Extremely good example of encapsulation context lifecycle, end even it's not mentioned explicitly as a parameter, while it's stands after "implicit" keyword.

No comments:

Post a Comment