List[Int] can contain null ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

List[Int] can contain null ?

boris pezzatti
I'm a newbie and I was asking myself if I could put a null in a List[Int].

After experimenting a little bit, I'm a bit confused. Actually it shouldn't be possible ... but can somebody help me to undersand following behaviour:  if I cast a List[Any] containing a null, it seems possible ... but sometimes casts the null into 0, e.g. when accessing it directly with the apply of  List ... but why not using a foreach?
Here is an example:

scala> val a:List[Int]=1::null::3::Nil
<console>:5: error: type mismatch;
 found   : List[Any]
 required: scala.List[Int]
       val a:List[Int]=1::null::3::Nil
                        ^

scala> val a:List[Int]=List(1,null,3)
<console>:5: error: type mismatch;
 found   : Null(null)
 required: Int
       val a:List[Int]=List(1,null,3)
                              ^

scala> val a:List[Any]=List(1,null,3)
a: List[Any] = List(1, null, 3)

scala> val b = a.asInstanceOf[List[Int]]
b: List[Int] = List(1, null, 3)

scala> b
res0: List[Int] = List(1, null, 3)


scala> b.foreach(println)
1
null
3


scala> b(1)
res3: Int = 0



Boris

--
P Please consider the environment before deciding to print this e-mail.
_________________________________________________________
Boris Pezzatti
Swiss Federal Research Institute WSL
Research unit Ecosystem Boundaries
Team Insubric Ecosystems
via Belsoggiorno 22
CH-6500 Bellinzona
Switzerland

phone direct ++41 91 821 52 32
phone ++41 91 821 52 30
fax ++41 91 821 52 39
[hidden email]
http://www.wsl.ch
Reply | Threaded
Open this post in threaded view
|

Re: List[Int] can contain null ?

Eric Willigers
On 15/07/10 08:16, boris pezzatti wrote:
> I'm a newbie and I was asking myself if I could put a null in a List[Int].

> scala> val a:List[Any]=List(1,null,3)
> a: List[Any] = List(1, null, 3)

> scala> val b = a.asInstanceOf[List[Int]]
> b: List[Int] = List(1, null, 3)

The static type of b is List[Int]. But the value of b isn't a List[Int]
- asInstanceOf has been used to mislead the compiler.

Here's another example:-

scala> val a: List[Any] = List(1, null, 3, "four")
scala> val b: List[Int] = a.asInstanceOf[List[Int]]
scala> b.foreach(println)
1
null
3
four

scala> b(3)
java.lang.ClassCastException: java.lang.String cannot be cast to
java.lang.Integer
        at scala.runtime.BoxesRunTime.unboxToInt


Work with the compiler instead of against it (using asInstanceOf), and
your List[Int] will never contain a null, a String or anything other
than an Int.

Reply | Threaded
Open this post in threaded view
|

Re: List[Int] can contain null ?

Naftoli Gugenheim
But who converted null to 0?


On Wed, Jul 14, 2010 at 6:42 PM, Eric Willigers <[hidden email]> wrote:
On 15/07/10 08:16, boris pezzatti wrote:
I'm a newbie and I was asking myself if I could put a null in a List[Int].

scala> val a:List[Any]=List(1,null,3)
a: List[Any] = List(1, null, 3)

scala> val b = a.asInstanceOf[List[Int]]
b: List[Int] = List(1, null, 3)

The static type of b is List[Int]. But the value of b isn't a List[Int] - asInstanceOf has been used to mislead the compiler.

Here's another example:-

scala> val a: List[Any] = List(1, null, 3, "four")
scala> val b: List[Int] = a.asInstanceOf[List[Int]]

scala> b.foreach(println)
1
null
3
four

scala> b(3)
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
       at scala.runtime.BoxesRunTime.unboxToInt


Work with the compiler instead of against it (using asInstanceOf), and your List[Int] will never contain a null, a String or anything other than an Int.


Reply | Threaded
Open this post in threaded view
|

Re: List[Int] can contain null ?

Moritz Uhlig
In reply to this post by boris pezzatti

Am 15.07.2010 um 00:16 schrieb boris pezzatti:

> I'm a newbie and I was asking myself if I could put a null in a List[Int].

You could - but you should not. It is better to forget about null for anything but Java interop.
For a safe replacement for null have a look at Option.


> After experimenting a little bit, I'm a bit confused. Actually it shouldn't be possible ... but can somebody help me to undersand following behaviour:  if I cast a List[Any] containing a null, it seems possible ... but sometimes casts the null into 0, e.g. when accessing it directly with the apply of  List ... but why not using a foreach?

> Here is an example:
>
> scala> b.foreach(println)
> 1
> null
> 3

This is because println takes an argument of type Any. If you pass a function that expects an Int it also works with foreach :

scala> def p(i: Int) = println(i)
p: (i: Int)Unit

scala> l.foreach(p)
1
0
3

or like this:

scala> l.foreach(println(_:Int))        
1
0
3

Unboxing of primitives works a bit differently from how it works in Java. As a primitive value can never be null the default value is assumed which is 0. It is of course arguable if this is always what you want. In such a case you are better off using Option[Int] to denote values as optional.

> scala> b(1)
> res3: Int = 0

Here the result is expected to be an Int, you can also add a type annotation to expect a value of type Any or AnyRef:

scala> l(1) : Any
res5: Any = null


scala> l(1).asInstanceOf[AnyRef]
res8: AnyRef = null

I actually would not recommend doing that - just for the record.


Moritz




Reply | Threaded
Open this post in threaded view
|

Re: List[Int] can contain null ?

boris pezzatti
In reply to this post by Naftoli Gugenheim
I don't know who  :-) ... but it works like that ...

scala> null.asInstanceOf[Int]
res5: Int = 0

Thank you for the reply
Boris


On 07/15/2010 12:45 AM, Naftoli Gugenheim wrote:
But who converted null to 0?


On Wed, Jul 14, 2010 at 6:42 PM, Eric Willigers <[hidden email]> wrote:
On 15/07/10 08:16, boris pezzatti wrote:
I'm a newbie and I was asking myself if I could put a null in a List[Int].

scala> val a:List[Any]=List(1,null,3)
a: List[Any] = List(1, null, 3)

scala> val b = a.asInstanceOf[List[Int]]
b: List[Int] = List(1, null, 3)

The static type of b is List[Int]. But the value of b isn't a List[Int] - asInstanceOf has been used to mislead the compiler.

Here's another example:-

scala> val a: List[Any] = List(1, null, 3, "four")
scala> val b: List[Int] = a.asInstanceOf[List[Int]]

scala> b.foreach(println)
1
null
3
four

scala> b(3)
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
       at scala.runtime.BoxesRunTime.unboxToInt


Work with the compiler instead of against it (using asInstanceOf), and your List[Int] will never contain a null, a String or anything other than an Int.



--
P Please consider the environment before deciding to print this e-mail.
_________________________________________________________
Boris Pezzatti
Swiss Federal Research Institute WSL
Research unit Ecosystem Boundaries
Team Insubric Ecosystems
via Belsoggiorno 22
CH-6500 Bellinzona
Switzerland

phone direct ++41 91 821 52 32
phone ++41 91 821 52 30
fax ++41 91 821 52 39
[hidden email]
http://www.wsl.ch
Reply | Threaded
Open this post in threaded view
|

Re: List[Int] can contain null ?

Josh Suereth
Did you notice the class cast exception on the apply for string?

Look up the boxesruntime methods.  You will find your magic there.

- Josh

On Jul 14, 2010, at 7:10 PM, boris pezzatti <[hidden email]> wrote:

I don't know who  :-) ... but it works like that ...

scala> null.asInstanceOf[Int]
res5: Int = 0

Thank you for the reply
Boris


On 07/15/2010 12:45 AM, Naftoli Gugenheim wrote:
But who converted null to 0?


On Wed, Jul 14, 2010 at 6:42 PM, Eric Willigers <[hidden email]> wrote:
On 15/07/10 08:16, boris pezzatti wrote:
I'm a newbie and I was asking myself if I could put a null in a List[Int].

scala> val a:List[Any]=List(1,null,3)
a: List[Any] = List(1, null, 3)

scala> val b = a.asInstanceOf[List[Int]]
b: List[Int] = List(1, null, 3)

The static type of b is List[Int]. But the value of b isn't a List[Int] - asInstanceOf has been used to mislead the compiler.

Here's another example:-

scala> val a: List[Any] = List(1, null, 3, "four")
scala> val b: List[Int] = a.asInstanceOf[List[Int]]

scala> b.foreach(println)
1
null
3
four

scala> b(3)
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
       at scala.runtime.BoxesRunTime.unboxToInt


Work with the compiler instead of against it (using asInstanceOf), and your List[Int] will never contain a null, a String or anything other than an Int.



--
P Please consider the environment before deciding to print this e-mail.
_________________________________________________________
Boris Pezzatti
Swiss Federal Research Institute WSL
Research unit Ecosystem Boundaries
Team Insubric Ecosystems
via Belsoggiorno 22
CH-6500 Bellinzona
Switzerland

phone direct ++41 91 821 52 32
phone ++41 91 821 52 30
fax ++41 91 821 52 39
[hidden email]
http://www.wsl.ch