// Kotlin's claimed to be statically type safe, but in what way? // basic class for linked list: class Node(var car:Int, var cdr:Node?) { // function (method) to add to end of list fun add(x:Int) { var i:Node? = this; while (i!=null) i = i.cdr;// gives compiler warnings while (x>0 && i!=null) i=i.cdr; // no compiler warnings with x>0 !!!! //i.cdr = Node(x,null); compiler error if (i!=null) i.cdr = Node(x,null); //note: error in code! } } // function to return last value in list, with intentional error fun last(m:Node?):Int { var i:Node? = m; while (i!=null && i.cdr!=null) i=i.cdr; //return i.car; compiler error! //val x:Any? = i; return x as Int //compiles with same problems if (i!=null) return i.car; else throw RuntimeException("bad node"); } fun main(argv:Array) { val m:Node = Node(3,Node(4,Node(5,null))); println(m.car); // works //println(m.cdr.car); // compiler error, check neededfor nullable m.cdr if (m.cdr != null) println(m.cdr?.car); println(m.cdr?.car); // OK println( last(m) ); m.add(7); println( "after add(7): "+last(m) ); /* val x:Any = 9; m.cdr = x as Node? println(m.cdr); */ }//main