Conversation
…s parameters instead of printlns, Connection trait extracted, case classes changed to classes to compare by reference not by value
| import java.io.FileInputStream | ||
|
|
||
| trait Connection { | ||
| def close(); |
| } | ||
|
|
||
| object Utils { | ||
| def withConnection[TResult](port: Int)(body: (Connection) => TResult) = { |
There was a problem hiding this comment.
Better write Connection => Tresult, without addtional braces
| (resourceFactory: () => TResource) | ||
| (body: (TResource) => TResult) | ||
| (cleanup: (TResource) => Unit = (resource: TResource) => {}): TResult = { | ||
| var resource: TResource = null.asInstanceOf[TResource] |
There was a problem hiding this comment.
It can be done without mutability and nulls. Looks very weird
There was a problem hiding this comment.
Try\catch is ok, you can use it for sure
There was a problem hiding this comment.
I needed to create a variable where I put resource if it was created successfully. It is the same pattern in .net using. How to do it correctly saving this feature for accessing resource?
| } | ||
|
|
||
| def withResource[TResource, TResult] | ||
| (resourceFactory: () => TResource) |
There was a problem hiding this comment.
It can be written as resourceFactory : => Tresource, When you specify () as input it means there will be no parameters to the function, it is unnecessarily restrict you, it can be ANY function returns Tresult
|
|
||
| def withResource[TResource, TResult] | ||
| (resourceFactory: () => TResource) | ||
| (body: (TResource) => TResult) |
No description provided.