[2.0] Type mismatch when rendering template inside pattern matching

1,081 views
Skip to first unread message

juhe

unread,
Jul 3, 2012, 12:18:46 PM7/3/12
to play-fr...@googlegroups.com
  def edit(id: Long) = Action {
    transaction {
      Citizen.findById(id) match {
        case None => NotFound("Citizen not found.")
        case Some(c) => Ok(views.html.edit(c))
      }
    }
  }

with a code like this for some reason I get:
type mismatch; found : play.api.mvc.SimpleResult[_ >: java.lang.String with play.api.templates.Html <: java.io.Serializable] required: play.api.mvc.SimpleResult[_1(in method edit)] where type _1(in method edit) >: java.lang.String with play.api.templates.Html <: java.io.Serializable Note: java.io.Serializable >: _1, but class SimpleResult is invariant in type A. You may wish to define A as -A instead. (SLS 4.5) 

Why is this? If I use the same Ok return outside matching it works fine.


Guillaume Bort

unread,
Jul 3, 2012, 12:35:30 PM7/3/12
to play-fr...@googlegroups.com
Hum, try the return explicitly:

def edit(id: Long): Result

It is a type inference problem, I will check why.
> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/_w8rbwfAAM0J.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.



--
Guillaume Bort, http://guillaume.bort.fr

juhe

unread,
Jul 3, 2012, 12:43:17 PM7/3/12
to play-fr...@googlegroups.com
I`m still fairly new to Scala, but if you meant this:
  def edit(id: Long): Result = Action {
    transaction {
      Citizen.findById(id) match {
        case None => NotFound("Citizen not found.")
        case Some(c) => Ok(views.html.edit(c))
      }
    }
  }

It gives:
type mismatch; found : play.api.mvc.Action[play.api.mvc.AnyContent] required: play.api.mvc.Result 

On Tuesday, July 3, 2012 7:35:30 PM UTC+3, Guillaume Bort wrote:
Hum, try the return explicitly:

def edit(id: Long): Result

It is a type inference problem, I will check why.

On Tue, Jul 3, 2012 at 6:18 PM, juhe <mirko...@gmail.com> wrote:
>   def edit(id: Long) = Action {
>     transaction {
>       Citizen.findById(id) match {
>         case None => NotFound("Citizen not found.")
>         case Some(c) => Ok(views.html.edit(c))
>       }
>     }
>   }
>
> with a code like this for some reason I get:
> type mismatch; found : play.api.mvc.SimpleResult[_ >: java.lang.String with
> play.api.templates.Html <: java.io.Serializable] required:
> play.api.mvc.SimpleResult[_1(in method edit)] where type _1(in method edit)
>>: java.lang.String with play.api.templates.Html <: java.io.Serializable
> Note: java.io.Serializable >: _1, but class SimpleResult is invariant in
> type A. You may wish to define A as -A instead. (SLS 4.5)
>
> Why is this? If I use the same Ok return outside matching it works fine.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/_w8rbwfAAM0J.
> To post to this group, send email to play-framework@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framework+unsubscribe@googlegroups.com.

Guillaume Bort

unread,
Jul 3, 2012, 1:16:54 PM7/3/12
to play-fr...@googlegroups.com
Yes sorry. You need to give explicit type to the Result. So:

Citizen.findById(id) match {
case None => NotFound("Citizen not found.")
case Some(c) => Ok(views.html.edit(c))
}:Result
>> > To post to this group, send email to play-fr...@googlegroups.com.
>> > To unsubscribe from this group, send email to
>> > play-framewor...@googlegroups.com.
>> > For more options, visit this group at
>> > http://groups.google.com/group/play-framework?hl=en.
>>
>>
>>
>> --
>> Guillaume Bort, http://guillaume.bort.fr
>
> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/X9Klf5hbmLUJ.
>
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.

juhe

unread,
Jul 3, 2012, 1:30:59 PM7/3/12
to play-fr...@googlegroups.com
This is not syntactically correct :) Can you even force pattern match into specific type? Anyways hope this issue gets fixed, cause pattern matching enables some really neat and concise actions.
>> > To post to this group, send email to play-framework@googlegroups.com.
>> > To unsubscribe from this group, send email to
>> > play-framework+unsubscribe@googlegroups.com.
>> > For more options, visit this group at
>> > http://groups.google.com/group/play-framework?hl=en.
>>
>>
>>
>> --
>> Guillaume Bort, http://guillaume.bort.fr
>
> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/X9Klf5hbmLUJ.
>
> To post to this group, send email to play-framework@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framework+unsubscribe@googlegroups.com.

Nilanjan

unread,
Jul 3, 2012, 3:15:50 PM7/3/12
to play-fr...@googlegroups.com
What about this?

transaction {
val r: Result = Citizen.findById(id) match {
    case None => NotFound("Citizen not found.")
    case Some(c) => Ok(views.html.edit(c))
}
r
}


Nilanjan

juhe

unread,
Jul 3, 2012, 3:33:50 PM7/3/12
to play-fr...@googlegroups.com
  def edit(id: Long) = Action {
    transaction {
      val r:Result = Citizen.findById(id) match {
        case None => NotFound("Citizen not found.")
        case Some(c) => Ok(views.html.edit(c))
      }
      r
    }
  }
This does work, but now its boilerplate all over and kind of defeats the purpose. Guillaume let me know if I can provide any more data to help fix this issue, tho its relatively simple to reproduce.

juhe

unread,
Jul 11, 2012, 4:13:28 PM7/11/12
to play-fr...@googlegroups.com
Any updates on this?

Guillaume Bort

unread,
Jul 30, 2012, 2:23:29 PM7/30/12
to play-fr...@googlegroups.com
I'm not sure why the type inference doesn't work properly here and if
we could change something to make it work, but you can also specify
the type to solve this issue:

def edit(id: Long) = Action {
inTransaction {
Citizen.findById(id).map { c =>
Ok(views.html.edit(c))
}.getOrElse(NotFound("Citizen not found."))
}:Result
}

On Mon, Jul 30, 2012 at 7:29 AM, Thomas Rawyler <raw...@gmail.com> wrote:
> I'd like to add that the small boilerplate addition works as well with the
> getOrElse variant.
>
> def edit(id: Long) = Action {
> inTransaction {
> val r = Citizen.findById(id).map { c =>
> Ok(views.html.edit(c))
> }.getOrElse(NotFound("Citizen not found."))
> r
> }
> }
>
> but of course would look more beautiful like this if it would compile:
>
> def edit(id: Long) = Action {
> inTransaction {
> Citizen.findById(id).map { c =>
> Ok(views.html.edit(c))
> }.getOrElse(NotFound("Citizen not found."))
> }
> }
>
> (btw, I prefer to use inTransaction (propagation required) instead of
> transaction (require new))
>>>>>> >> > play-fr...@googlegroups.com.
>>>>>> >> > To unsubscribe from this group, send email to
>>>>>> >> > play-framewor...@googlegroups.com.
>>>>>> >> > For more options, visit this group at
>>>>>> >> > http://groups.google.com/group/play-framework?hl=en.
>>>>>> >>
>>>>>> >>
>>>>>> >>
>>>>>> >> --
>>>>>> >> Guillaume Bort, http://guillaume.bort.fr
>>>>>> >
>>>>>> > --
>>>>>> > You received this message because you are subscribed to the Google
>>>>>> > Groups
>>>>>> > "play-framework" group.
>>>>>> > To view this discussion on the web visit
>>>>>> > https://groups.google.com/d/msg/play-framework/-/X9Klf5hbmLUJ.
>>>>>> >
>>>>>> > To post to this group, send email to
>>>>>> > play-fr...@googlegroups.com.
>>>>>> > To unsubscribe from this group, send email to
>>>>>> > play-framewor...@googlegroups.com.
>>>>>> > For more options, visit this group at
>>>>>> > http://groups.google.com/group/play-framework?hl=en.
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Guillaume Bort, http://guillaume.bort.fr
>
> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/JvvIqW0Fd-UJ.
>
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.

Thomas Rawyler

unread,
Jul 31, 2012, 2:04:33 AM7/31/12
to play-fr...@googlegroups.com
I tried the type hint suggestion before but still got the same type inference compilation error.
To make it work I hat to put the type after the getOrElse clause:

def edit(id: Long) = Action { 
    inTransaction { 
      Citizen.findById(id).map { c => 
        Ok(views.html.edit(c)) 
      }.getOrElse(NotFound("Citizen not found.")): Result
    }
}

Thanks again for the suggestion. It's a workaround but at least it's small.
>>>>>> >> > To unsubscribe from this group, send email to
>>>>>> >> > For more options, visit this group at
>>>>>> >> > http://groups.google.com/group/play-framework?hl=en.
>>>>>> >>
>>>>>> >>
>>>>>> >>
>>>>>> >> --
>>>>>> >> Guillaume Bort, http://guillaume.bort.fr
>>>>>> >
>>>>>> > --
>>>>>> > You received this message because you are subscribed to the Google
>>>>>> > Groups
>>>>>> > "play-framework" group.
>>>>>> > To view this discussion on the web visit
>>>>>> > https://groups.google.com/d/msg/play-framework/-/X9Klf5hbmLUJ.
>>>>>> >
>>>>>> > To post to this group, send email to
>>>>>> > To unsubscribe from this group, send email to
>>>>>> > For more options, visit this group at
>>>>>> > http://groups.google.com/group/play-framework?hl=en.
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Guillaume Bort, http://guillaume.bort.fr
>
> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/JvvIqW0Fd-UJ.
>
> To post to this group, send email to play-framework@googlegroups.com.
> To unsubscribe from this group, send email to
Reply all
Reply to author
Forward
0 new messages