When I run null.asInstanceOf[Int], I am surprised to find that the result is 0, I would ask why it is 0, I thought it should be null
I thought it should be null
It can't be null because Int doesn't have a null value. You can see it if you try null: Int (type ascription instead of a cast), though the error message is ugly.
why it is 0
Because it's defined this way in http://scala-lang.org/files/archive/spec/2.12/06-expressions.html#the-null-value:
asInstanceOf[T] [on null] returns the default value of type T.
Interestingly, it used to be defined to throw NullPointerException instead, but actually implemented in the current way, so the specification was changed: https://issues.scala-lang.org/browse/SI-5236
Related
Want to check if the column has values that have certain length and contains only digits.
The problem is that the .rlike or .contains returns a Column type. Something like
.when(length(col("abc")) == 20 & col("abc").rlike(...), myValue)
won't work as col("abc").rlike(...) will return Column and unlike length(col("abc")) == 20 which returns Boolean (length() however also returns Column). How do I combine the two?
After doing a bit of searching in compiled code, found this
def when(condition : org.apache.spark.sql.Column, value : scala.Any) : org.apache.spark.sql.Column
Therefore the conditions in when must return Column type. length(col("abc")) == 20 was evaluating to Boolean.
Also, found this function with the following signature
def equalTo(other : scala.Any) : org.apache.spark.sql.Column
So, converted the whole expression to this
.when(length(col("abc")).equalTo(20) && col("abc").rlike(...), myValue)
Note that the logical operator is && and not &.
Edit/Update : #Histro's comment is correct.
I'm new to Scala. I'm trying to create a test case for a simple factorial function.
I couldn't assign the result value in the assert statement. I'm getting
Integer number is out of range even for type Long error in IntelliJ.
test("Factorial.factorial6") {
assert(Factorial.factorial(25) == 15511210043330985984000000L)
}
I also tried to assign the value to val, using the 'L' literal, again it shows the same
message.
val b: BigInt = 15511210043330985984000000L
I'm clearly missing some basic stuff about Scala, I would appreciate your help, to solve this
The value you are giving is indeed larger than can be held in a Long, and that is the maximum size for a literal value in Scala. However you can initialise a BigInt using a String containing the value:
val b = BigInt("15511210043330985984000000")
and therefore
assert(Factorial.factorial(25) == BigInt("15511210043330985984000000"))
I am coming from Java, and trying to understand the following Scala syntax
null.asInstanceOf[Double]
Why is this not a NullPointerException?
I was trying to do something like:
var d : Double = 0
if(d == null)
// do something
However, I got following error message:
comparing values of types Double and Null using `==' will always yield false
This got fixed when I changed null to null.asInstanceOf[Double] as per this answer, but this is a weird statement for me, how on earth this is working?
Scala's scala.Double does not correspond to Java's java.lang.Double. Scala's double inherits from AnyVal, the parent of all value types. It most closely corresponds to the primitive type double in Java, which can't be null. When you do null.asInstanceOf[Double], you're actually getting the double value 0.0, not a null double.
From the Scala language specification section 6.3
[the null value] implements methods in scala.AnyRef as follows
...
asInstanceOf[T] returns the default value of type T
And the default value of Double is zero.
So, in short, your value can't possibly be null because it's like a Java primitive. So you don't need to do a null check in this case.
When using vDSP to perform some speedy calculations, I often don't care about one of the output parameters. Let's say I'm finding the index of an array's maximum value:
var m:Float = 0
var i:vDSP_Length = 0
vDSP_maxvi(&array,
1,
&m,
&i,
vDSP_Length(array.count))
Ideally, I'd like to get rid of m altogether so that vDSP_maxvi fills i only. Something like:
var i:vDSP_Length = 0
vDSP_maxvi(&array,
1,
nil,
&i,
vDSP_Length(array.count))
But of course this doesn't work ("nil is not compatible with expected argument type 'UnsafeMutablePointer<Float>'"). Is there some sort of argument I can send to these kinds of methods that says "ignore this parameter"? Thanks for reading.
Except for documented cases where a null argument is accepted, you must pass a valid address. There is no argument value that tells vDSP to ignore the argument.
In objective-c is the end result of !variable the same as variable==nil Also I think I read somewhere that iVars are initialized to "nil" (i.e. o) but sadly I can't seem to find where I spotted it now. If I am correct is this initialization to nil part of declaring an iVar or is it linked to something else like #property?
i.e. do these evaluate the same ...
if(!myObject) ...
and
if(myObject == nil) ...
Cheers Gary.
Edited: hopefully for more clarity.
Your question subject and question body appear to be asking different things, so…
To answer the question subject: No, ! and nil are not at all the same. ! is an operator that returns the logical negation of its operand (that is, !0 returns 1 and ! with anything else returns 0), while nil is a macro for 0.
To answer the question itself: Yes, !foo and foo == nil give the same result for object variables. And yes, instance variables are initialized to 0. So are static variables and global variables.
! is a C logical operator that means not (like && means and and || means or).
nil is the way for expressing an unitialized object pointer.
Applied to a pointer, the value of the expression is TRUE whenever the pointer is NULL or nil.
In the end, if(!myObject) and if(myObject==nil) have the same behaviour.
In objective-c the ! is a boolean operator and returns the opposite of a boolean value or expression result. In the below example myObj is really evaluating it's pointer value as a boolean, zero or non-zero. Since myObj is a non-zero pointer this evaluates to true. !myObj will return the opposite, in this case it would return false.
id myObj = nil;
int x = 0;
if (myObj)
x++;