Scala does not output the corresponding method

I have the following method

def show[E: Writes, T: Writes](block: RequestWithUser[AnyContent] => Either[E, T]): Action[AnyContent] = { withErr(block) } 

which I use like this from the controller:

 def show(id: Long) = CrudAuthAction.show { request => IdeaType.findByIdWithErr(id) } 

And I want the request method to be optional, so I defined a different signature for the same method:

 def show[E: Writes, T: Writes](block: => Either[E, T]): Action[AnyContent] = { withErr(request => block) } 

And it works fine, I can omit the query parameter

But when I try to do the same with this other method

 def list[T: Writes](block: RequestWithUser[AnyContent] => T): Action[AnyContent] = { fromRequest(block) } def list[T: Writes](block: => T): Action[AnyContent] = { fromRequest(request => block) } 

When I want to use it as follows:

 def list = CrudAuthAction.list { request => IdeaType.find(request.queryString) } 

he tells me that the request is missing a parameter type, and I must specify it as follows:

 def list = CrudAuthAction.list { request: RequestWithUser[AnyContent] => 

I do not see what is different from the first case, but scala cannot seem like it is requesting the correct request type ...

The only difference I see is that in the first case, the block returns A [E, T], but in the second case only the general T is returned ...

+6
source share
1 answer

The problem is that in the second example, the compiler does not know which of the overloaded methods to choose, since T may very well be a type of function. Since Either clearly not a function, it works in the first case.

To work around this problem, you can change the second method to

 def list[T: Writes](block: => Foo[T]): Action[AnyContent] = { fromRequest(request => block.v) } 

With Foo it is defined as follows:

 case class Foo[T](val v:T) 

Unfortunately, adding an implicit conversion to Foo again breaks the situation unless you create an implicit conversion for each type in a class of type Writes .

+5
source

All Articles