簡體   English   中英

Scala中的奇怪類型不匹配

[英]Strange type mismatch in Scala

我正在嘗試調用在Scala中采用varargs的構造函數。 構造函數用Java類編寫,並帶有兩個參數, BlockIBlockState... 但是,以下代碼似乎無法編譯:

new BlockStateList(this, Seq[IBlockState[_ <: Comparable[_]]](FACING, DAMAGE):_*)

編譯器報告以下消息:

Error:(58, 66) type mismatch;
 found   : Seq[net.minecraft.server.v1_8_R3.IBlockState[_ <: Comparable[_]]]
 required: Seq[net.minecraft.server.v1_8_R3.IBlockState[? <: Comparable[?0]] forSome { type ?0 <: Comparable[?0] }]
   new BlockStateList(this, Seq[IBlockState[_ <: Comparable[_]]](FACING, DAMAGE):_*)
                                      ^

所需的類型對我來說似乎在語法上不正確,並且我不知道它要我提供什么。

非常感謝所有幫助!

問題與可變參數無關,而與序列的內容有關。

正如我推論的那樣,Comparable僅限於其類型參數。 就像是

trait Comparable[C <: Comparable[C]]

查看有關此模式的更多信息 因此,要表示此類型要求,您需要高級的存在性類型規范。 MyType[_]MyType[A] forSome {type A}完整MyType[A] forSome {type A}快捷方式。 在您的情況下,編譯器期望

Seq[IBlockState[_ <: Comparable[C]] forSome { type C <: Comparable[C] }]()

代替

Seq[IBlockState[_ <: Comparable[_]]]()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM