// class Monkey(name: String, voice: String, val hasHat: Boolean): Pet(name, voice) sealed class MyOptionType { data class OptionA(val info: String): MyOptionType() data class OptionB(val num: Int): MyOptionType() } sealed class LinkedList { object End: LinkedList() data class Node( val word: String, val next: LinkedList ): LinkedList() } fun bigString(llos: LinkedList): String { return when (llos) { is LinkedList.End -> "" is LinkedList.Node -> "${ llos.word }${ bigString( llos.next ) }" } } println(bigString(LinkedList.End)) println( bigString( LinkedList.Node( "Howdy", LinkedList.Node( " ", LinkedList.Node( "World", LinkedList.End ) ) ) ) ) val myMaybeNum1: Int? = 5 val myMaybeNum2: Int? = null // val myNum: Int = null // not allowed, because no ? // treats null as a 0 fun increment(num: Int?): Int { // return num + 1 return when (num) { null -> 1 else -> num + 1 } } println(increment(myMaybeNum1)) println(increment(myMaybeNum2)) println( ( myMaybeNum1 ?: 0 ) + 1) println( ( myMaybeNum2 ?: 0 ) + 1) val myMaybeString1: String? = "howdy" val myMaybeString2: String? = null println( myMaybeString1?.uppercase() ) println( myMaybeString2?.uppercase() ) // From the inventor (https://en.wikipedia.org/wiki/Tony_Hoare): // I call it my billion-dollar mistake. It was the invention // of the null reference in 1965. At that time, I was designing // the first comprehensive type system for references in an // object oriented language (ALGOL W). My goal was to ensure // that all use of references should be absolutely safe, // with checking performed automatically by the compiler. // But I couldn't resist the temptation to put in a null // reference, simply because it was so easy to implement. // This has led to innumerable errors, vulnerabilities, // and system crashes, which have probably caused a billion // dollars of pain and damage in the last forty years." // Lesson: just because you *can*, doesn't always mean you should :)