Scalastyle "Public method must have explicit type" in Play Framework
Asked Answered
S

3

7

We've started experimenting with Scala and the Play framework at my work. Setup our auto-linting and testing framework as the first thing, and have deployed Scalastyle to handle the former.

That has been very useful, except that we are getting this specific lint error that we are finding it difficult to resolve in a good way. A simple example is this:

  def helloWorld = Action {
    req =>
      Ok("Hello World!")
  }

Though often it can be much more complex, of course (to the point where it can difficult to figure out what the type actually is).

In either case, this gives us the "Public method must have explicit type" error from Scalastyle.

Unfortunately, setting the expected explicit type here seems typically to cause a syntax error.

Any suggestions on a good solution for this? Or do we just have to turn of this check for Play projects?

Sweetener answered 26/2, 2015 at 12:14 Comment(0)
P
6

Any suggestions on a good solution for this? Or do we just have to turn of this check for Play projects?

I'd suggest to either turn org.scalastyle.scalariform.PublicMethodsHaveTypeChecker rule off completely for your project or mark your controllers to be ignored by this rule (here you'll find info on how to do this).

In the end this check benefit more to people who write libraries (as it helps to be more explicit about api one provide). I found that when you're working on "real" projects check like this does nothing but adding some boilerplate and stops you from leveraging type inference.

Pampero answered 26/2, 2015 at 13:45 Comment(1)
Wish there was a better way, but // scalastyle:off it is.Sweetener
S
2

enter image description here I hope this helps. To to Settings -> Editor -> Scala -> Type Annotations. Change the value to 'Add' instead of 'Add & Check' for Public value and method. Then it IDE will not show that warning anymore.

Slagle answered 7/6, 2017 at 15:10 Comment(0)
S
0

I've found a better way for removing the "Public method must have explicit type" message, without turning it off.

When defining these methods, the body [type] and [implicit] [type] may be set; as Action[JsValue] and implicit RequestHeader for example.

Code example:

def helloWorld:Action[JsValue] = Action { 
    implicit req: RequestHeader =>
      Ok("Hello World!")
  }

or

 def helloWorld:Action[AnyContent] = Action { 
    implicit req: RequestHeader =>
      Ok("Hello World!")
  }
Swaim answered 24/4, 2019 at 22:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.