本文介绍了Scala类型(推断)问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Jersey JAX-RS参考实现在Scala中开发REST Web服务,遇到一个奇怪的错误.

I'm developing a REST webservice in Scala using the Jersey JAX-RS reference implementation and I'm getting a strange error.

我正在尝试创建ContentDisposition 对象. rel ="nofollow"> ContentDisposition.ContentDispositionBuilder .

I'm trying to create a ContentDisposition object using the ContentDisposition.ContentDispositionBuilder.

ContentDisposition.ContentDispositionBuilder具有两种类型的T extends ContentDisposition.ContentDispositionBuilderV extends ContentDisposition. ContentDisposition的方法type返回一个生成器实例.

ContentDisposition.ContentDispositionBuilder has two types T extends ContentDisposition.ContentDispositionBuilder and V extends ContentDisposition. The method type of ContentDisposition returns a builder instance.

代码

val contentDisposition = ContentDisposition.`type`(MediaType.APPLICATION_OCTET_STREAM).build()

有效

val contentDisposition = ContentDisposition.`type`(MediaType.APPLICATION_OCTET_STREAM).fileName("dummy").build()

产生编译器错误

error: value build is not a member of ?0
val contentDisposition = ContentDisposition.`type`(MediaType.APPLICATION_OCTET_STREAM).fileName("dummy").build()
                                                                                                         ^

(请注意,由于type是Scala中的关键字,因此必须在引号中加上type)

(Note that type needs to be put in "quotation marks" because it's a keyword in Scala)

fileName返回T的实例,因此这应该可以正常工作.

fileName of ContentDispositionBuilder returns an instance of T so this should actually work.

我不明白这一点.任何的想法?顺便说一下,我正在使用Scala 2.9.0.1.

I don't get this. Any idea?I'm using Scala 2.9.0.1 by the way.

更新:

这有效.但是,为什么我需要在这里铸造?

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()

或者也许

val builder=ContentDisposition.`type`(MediaType.APPLICATION_OCTET_STREAM).fileName("dummy")
val contentDisposition=builder.build()

这篇关于Scala类型(推断)问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 00:39