超全模态模型 × Harness 升级,升级解锁 ArkClaw,最新支持 DeepSeek-V4 系列与 GLM-5.1
先看你给出的Scala类代码:
class Demo { def mandatoryInt: Int = 42 def optInt: Option[Int] = Some(1) def optString: Option[String] = Some("demo") }
你遇到的问题确实是因为Java泛型不支持原始类型(比如int),而Scala的Int默认会映射到Java的int,导致Option[Int]无法直接对应Java合法的泛型类型,最终被擦除为Option<Object>。
int
Int
Option[Int]
Option<Object>
有两种简单的解决方法:
直接把Option[Int]替换为Option[java.lang.Integer],这样编译后Java代码就能识别为Option<Integer>:
Option[java.lang.Integer]
Option<Integer>
class Demo { def mandatoryInt: Int = 42 def optInt: Option[java.lang.Integer] = Some(1) def optString: Option[String] = Some("demo") }
@boxed
在Scala 2.12中,可以通过scala.annotation.meta.boxed注解标记方法,让编译器自动将基本类型的泛型参数装箱为对应的Java包装类,不用修改类型声明:
scala.annotation.meta.boxed
import scala.annotation.meta.boxed class Demo { def mandatoryInt: Int = 42 @boxed def optInt: Option[Int] = Some(1) def optString: Option[String] = Some("demo") }
这两种方法都能让Java代码正确识别optInt的类型为Option<Integer>,你可以根据编码习惯选择。
optInt
内容的提问来源于stack exchange,提问作者Roman
超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起
模型再升级,30秒超长叙事, 模态参考扩容
模型自由,工具不限,最新支持 Deepseek-V4 系列、GLM-5.3 系列
超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列
大模型19元起,Al应用9.9元畅享,新人首购爆款尽享优惠