scala 将 null 传递给需要 Long 的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13637827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass null to a method expects Long
提问by Echo
I have a Scala method that takes 2 parameters:
我有一个带有 2 个参数的 Scala 方法:
def test(x:Long,y:Int){}
On some occasion I need to pass null instead of long ... something like that:
在某些情况下,我需要传递 null 而不是 long ... 类似的东西:
test(null,x)
The result:
结果:
scala> test(null,2) :7: error: type mismatch; found : Null(null) required: Long test(null,2)
Scala> test(null,2) :7: 错误:类型不匹配;发现:空(空)要求:长测试(空,2)
Why do I need to pass null? Actually ,for some reason,I can't pass any default values. Thus, I need such a null.
为什么我需要传递null?实际上,出于某种原因,我无法传递任何默认值。因此,我需要这样一个空值。
*Note:*I know that the solution would be making it Option. However let's say I have no control over this method signature,can I do any work around?
*注意:*我知道解决方案是将其设为 Option。但是,假设我无法控制此方法签名,我可以做任何工作吗?
Any ideas!
有任何想法吗!
Thanks.
谢谢。
采纳答案by som-snytt
Since you can't change the signature, consider the mistake of Thinking Option[Foo] is the only/most natural way to express a missing function argument.
由于您无法更改签名,请考虑Thinking Option[Foo]的错误是表达缺失函数参数的唯一/最自然的方式。
If the param to your function is a lower bound, then Long.MinValue might be a natural default.
如果您的函数的参数是一个下限,那么 Long.MinValue 可能是一个自然的默认值。
If by "for some reason,I can't pass any default values" (whatever that could possibly mean) you mean you can't add defaults to the signature, and you're going the route suggested in another answer of adapting the method, you might as well change f(a,b) to g(b, a=Long.MinValue) or whatever before forwarding.
如果通过“出于某种原因,我无法传递任何默认值”(无论这可能意味着什么),您的意思是您无法将默认值添加到签名中,并且您将采用另一个适应方法的答案中建议的路线,您不妨在转发之前将 f(a,b) 更改为 g(b, a=Long.MinValue) 或其他任何内容。
Instead of making clients of your adaptor method call g(b, None), let them call g(b). You're not passing the Option to the underlying f(a,b) anyway.
与其让您的适配器方法的客户端调用 g(b, None),不如让它们调用 g(b)。无论如何,您不会将 Option 传递给基础 f(a,b) 。
回答by stew
Null is a subtype of types which inherit from AnyRef, not from value types which inherit from AnyVal. This is why you are not able to pass null in. This corresponds to how, in java, you cant have a null of type long. (ignoring the boxed Long type).
Null 是继承自 AnyRef 的类型的子类型,而不是继承自 AnyVal 的值类型。这就是您无法传入 null 的原因。这对应于在 Java 中,您不能拥有 long 类型的 null。(忽略盒装 Long 类型)。
However, this is an indication that the signature of the method should be changed to:
但是,这表明该方法的签名应更改为:
def test(x: Option[Long], y: Int)
which indicates that sometimes it goes no value for x. Since we have this nice Option class to deal with just this instance, there is little if any valid reasons to use null values, where you are relying on developers remembering to check for null values. Instead, with Option, the compiler will force you to take care of the fact that the value might not be there.
这表明有时它对 x 没有价值。因为我们有这个很好的 Option 类来处理这个实例,所以几乎没有任何有效的理由使用空值,你依赖于开发人员记住检查空值。相反,使用 Option 时,编译器会强制您处理该值可能不存在的事实。
回答by Staale
The way to convert scala primitives to Java wrapper classes, is to use the static valueOf members on the Java Primitive wrappers. I had this issue where I needed to convert an Option[Double] to a java.lang.Double or null. This is what I did:
将 scala 原语转换为 Java 包装器类的方法是使用 Java 原语包装器上的静态 valueOf 成员。我遇到了这个问题,需要将 Option[Double] 转换为 java.lang.Double 或 null。这就是我所做的:
val value: Option[Double]
val orNull = value.map(java.lang.Double.valueOf(_)).orNull
Just passing literal null should work if you are calling a method that accepts java.lang.Long/Double/Integer
如果您正在调用接受 java.lang.Long/Double/Integer 的方法,则只需传递文字 null 即可

