Skip to content

Feature/task07#6

Open
fpandyz wants to merge 10 commits intomainfrom
feature/task07
Open

Feature/task07#6
fpandyz wants to merge 10 commits intomainfrom
feature/task07

Conversation

@fpandyz
Copy link
Owner

@fpandyz fpandyz commented May 12, 2021

No description provided.

import java.io.FileInputStream

trait Connection {
def close();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for ";" here

}

object Utils {
def withConnection[TResult](port: Int)(body: (Connection) => TResult) = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better write Connection => Tresult, without addtional braces

(resourceFactory: () => TResource)
(body: (TResource) => TResult)
(cleanup: (TResource) => Unit = (resource: TResource) => {}): TResult = {
var resource: TResource = null.asInstanceOf[TResource]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be done without mutability and nulls. Looks very weird

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try\catch is ok, you can use it for sure

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Too many braces again :))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants