I found an example of code building a LinkedList from scratch in Scala and I could not understand the last line in the example. Here is the example :
scala> class MyList (val head : Any, val tail : MyList) {
| def isEmpty = (head == null && tail == null)
| def length : Int = if (isEmpty) 0 else 1 + tail.length
| override def toString: String = if (isEmpty) "" else head + " " + tail
| }
defined class MyList
scala> var list = new MyList (null, null)
list: MyList =
scala> list.length
res3: Int = 0
scala> list.isEmpty
res4: Boolean = true
scala> list = new MyList ("ABC", list)
list: MyList = ABC
scala> list.length
res5: Int = 1
scala> list.isEmpty
res6: Boolean = false
scala> list = new MyList("XYZ", list)
list: MyList = XYZ ABC
scala> list = new MyList ("123", list)
list: MyList = 123 XYZ ABC
scala> list.tail.head
res7: Any = XYZ
I dont understand why list.tail.head prints "XYZ". In fact I cannot reason out what list.tail.head should print a priori given the definition of the class MyList as given. Any help in understanding what is going on would be appreciated.