Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass null to a method expects Long

I have a Scala method that takes 2 parameters:

def test(x:Long,y:Int){}

On some occasion I need to pass null instead of long ... something like that:

test(null,x)

The result:

scala> test(null,2) :7: error: type mismatch; found : Null(null) required: Long test(null,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.

*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?

Any ideas!

Thanks.

like image 295
Echo Avatar asked Nov 30 '12 01:11

Echo


People also ask

Can we pass null long?

You cannot pass the null value as a parameter to a Java scalar type method; Java scalar types are always non-nullable. However, Java object types can accept null values.

What happens if we pass null in a method in Java?

When we pass a null value to the method1 the compiler gets confused which method it has to select, as both are accepting the null. This compile time error wouldn't happen unless we intentionally pass null value.

Is it good to pass null?

Here the function is called with a nullable argument, and if the argument was null, the function will return null, and if not it will return a proper value.

How do you pass a null value?

Passing a null value to any Corticon Server using JSON payloads is accomplished by either: Omitting the JSON attribute inside the JSON object. Including the attribute name in the JSON Object with a value of JSONObject.


2 Answers

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

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.

like image 197
stew Avatar answered Sep 22 '22 12:09

stew


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.

If the param to your function is a lower bound, then Long.MinValue might be a natural default.

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.

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.

like image 21
som-snytt Avatar answered Sep 19 '22 12:09

som-snytt