7

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 ?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()
Sven Jacobs
  • 6,278
  • 5
  • 33
  • 39
  • Is it just me or none of your links seem to work? – agilesteel Aug 31 '11 at 11:36
  • It seems java.net is down at the moment :-( – Sven Jacobs Aug 31 '11 at 11:42
  • 1
    The error has something to do with interop between Java wildcards and Scala existentials. Although similar, these two type system features are not the same. This is a shot in the dark, but it might be worth trying with the just released Scala 2.9.1. – Kipton Barros Aug 31 '11 at 14:22
  • I have had a few interop problems with 2.9.0.1 when using Hadoop (TextInputFormat does not work where an InputFormat is expected). Things worked better with 2.9.1. I would at least hope that you would get a more informative exception! – schmmd Sep 07 '11 at 16:43
  • 2.9.1 didn't fix this problem for me :( I still have to use the explicit casts. – Sven Jacobs Sep 12 '11 at 11:33
  • This is tangential, but you may want to check out scalatra as a Jersey alternative. – schmmd Sep 14 '11 at 21:51

1 Answers1

2

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()
Chochos
  • 5,155
  • 22
  • 27