Reputation: 2201
I have this code:
package deliveries.NewPost
import play.api.data._
import play.api.data.Forms._
case class NewPostValidator(town: String, number: Int)
object NewPost extends Delivery{
def form[NewPostValidator](): Form[NewPostValidator] = Form(mapping(
"town" -> nonEmptyText,
"number" -> number)(NewPostValidator.apply)(NewPostValidator.unapply))
}
At compile time it gives me strange error:
type mismatch; [error] found : play.api.data.Mapping[deliveries.NewPost.NewPostValidator] [error] required: play.api.data.Mapping[NewPostValidator]
Basically the same class shows with full path and without full path. What can it be?
I will clarify what I'm trying to achieve: I want every class extending trait to have method "form" that returns Form with some validator in it. For this I've created trait:
trait Delivery {
def form[T](): Form[T]
}
and trying to implement this method in my class.
Upvotes: 1
Views: 77
Reputation: 9698
Saying def form[NewPostValidator]
is like saying def form[T]
, just with a different identifier. Compiler doesn't see the NewPostValidator in the method body as the "real" NewPostValidator, but as your made-up type NewPostValidator that you declared in def form[NewPostValidator]
.
Solution:
Parameterize your trait with [T] instead of your method, and have your class extend the trait with a specific type.
trait Delivery[T] {
def form(): Form[T]
}
object NewPost extends Delivery[NewPostValidator] {
def form(): Form[NewPostValidator] = Form(mapping(
"town" -> nonEmptyText,
"number" -> number)(NewPostValidator.apply)(NewPostValidator.unapply))
}
Upvotes: 1