Scala type (inference) issue?
I'm developing a REST webservice in Scala using the Jersey JAX-RS reference implementation and I'm getting a strange error.
I'm trying to create a ContentDisposition object using the ContentDisposition.ContentDispositionBuilder.
ContentDisposition.ContentDispositionBuilder
has two types T extends ContentDisposition.ContentDispositionBuilder
and V extends ContentDisposition
. The method type
of ContentDisposition
returns a builder instance.
The code
val contentDisposition = ContentDisposition.`type`(MediaType.APPLICATION_OCTET_STREAM).build()
works however
val contentDisposition = ContentDisposition.`type`(MediaType.APPLICATION_OCTET_STREAM).fileName("dummy").build()
produces the compiler error
error: value build is not a member of 开发者_如何学JAVA?0
val contentDisposition = ContentDisposition.`type`(MediaType.APPLICATION_OCTET_STREAM).fileName("dummy").build()
^
(Note that type
needs to be put in "quotation marks" because it's a keyword in Scala)
fileName
of ContentDispositionBuilder
returns an instance of T
so this should actually work.
I don't get this. Any idea? I'm using Scala 2.9.0.1 by the way.
Update:
This works. But why do I need the casting here?
val contentDisposition = ContentDisposition.`type`(MediaType.APPLICATION_OCTET_STREAM)
.fileName("dummy")
.asInstanceOf[ContentDisposition.ContentDispositionBuilder[_,_]]
.build()
I guess type inference can only go so far... You can probably do it in two lines, without having to do any casts; have you tried this?
val something=ContentDisposition.`type`(MediaType.APPLICATION_OCTET_STREAM)
val contentDisposition=something.fileName("dummy").build()
or maybe
val builder=ContentDisposition.`type`(MediaType.APPLICATION_OCTET_STREAM).fileName("dummy")
val contentDisposition=builder.build()
精彩评论