Reputation: 8421
I have a class with a constructor as defined below:
class Worker(val action: String, val owner: Component,
validate: (RequestScopeObject) => Boolean,
model: (RequestScopeObject) => Unit,
connector: (RequestScopeObject) => Unit)
and another java class called FrameFactory with a method as below:
public static <T extends Component> T
createContainerAndShow(String framesetid, Component component)
When I am creating an instance of the Worker class as below:
new Worker(
action = "actionName",
owner = null,
validate = (scopeObject) => {true},
model = (scopeObject) => {},
connector = (scopeObject: RequestScopeObject) => {
FrameFactory.createContainerAndShow("string", new JPanel())
() // forced to put a '()' here as FrameFactory.createContainerAndShow
//returns the Jpanel created
}
))
I was wondering if there was any way of writing this better? How do i declare the connector parameter as a function that returns Unit and ignore the JPanel returned by FrameFactory
Upvotes: 0
Views: 269
Reputation: 27702
You're not forced to put ()
at the end of the function block. If your function return type is of type Unit
the instance resulting of the last block expression won't be returned.
Consider the following example:
def f(g: Int => Unit) = {}
f((x: Int) => 3)
Upvotes: 1
Reputation: 8673
You can simply ignore the fact that your connector looks like it is returning a frame, compiler knows that it is function RequestScopeObject => Unit
and it will be treated as such. You can think of it like compiler will add ()
as last statement in your function.
Also note that you don't need curly braces if you have one instruction only, and you don't need parens around single argument as well.
new Worker(
action = "actionName",
owner = null,
validate = scopeObject => true,
model = scopeObject => (),
connector = scopeObject =>
FrameFactory.createContainerAndShow("string", new JPanel())
))
Upvotes: 1