Commits

Zemian Deng committed 58b41e5

Added more Kotlin demos.

Comments (0)

Files changed (10)

kotlin-demo/scripts/run.sh

 # Run Kotlin Program. User only needs to give the package name, and it will auto add the .namepace suffix.
 # Example: scripts/run.sh hello
 #
-DIR=$(cd $(dirname $0) && pwd)
-PKG=$1
-shift
-$DIR/run-java "$PKG.namespace" "$@"
+DIR=$(cd "$(dirname $0)/.." && pwd)
+PKG='namespace'
+if [[ $# -ge 1 ]]; then PKG="$1.namespace"; shift; fi
+$DIR/scripts/run-java -cp "$DIR/target/test-classes:$DIR/target/classes:$DIR/target/dependency*" $PKG "$@"

kotlin-demo/src/main/kotlin/ClassDemo.kt

 open class B
 class C : B()
 fun main(args : Array<String>) {
-    println(C())
+	println(C())
 }

kotlin-demo/src/main/kotlin/CollectionDemo.kt

 package collectiondemo
 fun main(args : Array<String>) {
 	println(array(11.00, 12.25, 13.99, 14.01))
-    println(arrayList(1,2,3))
-    println(hashSet("a", "b", "c", "a")) // There should only be 3 unique elements
-    println(hashMap("a" to 11, "b" to 12, "c" to 13))
+	println(arrayList(1,2,3))
+	println(hashSet("a", "b", "c"))
+	println(hashMap("a" to 11, "b" to 12, "c" to 13))
+
+	assert(arrayList("a", "b", "c", "a").toString() == "[a,b,c,a]")
+	assert(hashSet("a", "b", "c", "a").toString() == "[a,b,c]")
+
+	// compile error?
+	//for (n in arrayList(1,2,3))
+	//	println(n)
+
+	for (n in hashSet("a", "b", "c"))
+		println(n)
+
+	for ((k,v) in hashMap("a" to 11, "b" to 12, "c" to 13))
+		println("$k = $v")	
+
+	// Java Properties entry are not <String, String>, so we must cast!
+	for ((name, value) in System.getProperties())
+		if (name.toString().startsWith("java"))
+			println("Sys prop: $name=$value")
+
+	// Due to Kotlin treat all Java as null-able object, we need use !!.
+	for (name in System.getProperties()!!.stringPropertyNames()!!.iterator())
+		if (name!!.startsWith("java.vendor"))
+			println("Sys prop: $name=${System.getProperty(name)}")
 }

kotlin-demo/src/main/kotlin/DataClassDemo.kt

 package dataclassdemo
 data class Employee(val id : Int, val name : String)
 fun main(args : Array<String>) {
-    val list = arrayList(
-        Employee(101, "Zemian"),
-        Employee(102, "Julie")
-    )
-    println("List $list")
+	val list = arrayList(
+		Employee(101, "Zemian"),
+		Employee(102, "Julie")
+	)
+	println("List $list")
 }

kotlin-demo/src/main/kotlin/FactorialDemo.kt

+package factorydemo
+
+fun main(args : Array<String>) {
+	println("Hello")
+
+	val max : Int = if (args.size >= 1) args[0].toInt() else 10
+	for (i in (0..max))
+		println(fact(i))
+
+	for (i in (0..max))
+		println(fact2(i))
+}
+
+fun fact(n : Int) : Int {
+	if (n <= 1) return 1
+	else return n * fact(n -1)
+}
+
+fun fact2(n : Int) : Int {
+	when (n) {
+		0, 1 -> return 1
+		else -> return n * fact2(n - 1)
+	}
+}

kotlin-demo/src/main/kotlin/FactoryDemo.kt

-package factorydemo
-
-fun main(args : Array<String>) {
-	println("Hello")
-
-	val max : Int = if (args.size >= 1) args[0].toInt() else 10
-	for (i in (0..max))
-		println(fact(i))
-
-	for (i in (0..max))
-		println(fact2(i))
-}
-
-fun fact(n : Int) : Int {
-	if (n <= 1) return 1
-	else return n * fact(n -1)
-}
-
-fun fact2(n : Int) : Int {
-	when (n) {
-		0, 1 -> return 1
-		else -> return n * fact2(n - 1)
-	}
-}

kotlin-demo/src/main/kotlin/Hello.kt

 package hello
 fun main(args : Array<String>) {
-    println("Hello2")
+	println("Hello2")
 }

kotlin-demo/src/main/kotlin/LoopDemo.kt

 package loopdemo
 fun main(args : Array<String>) {
-    for (s in args)
-    	println("#1 Arg: $s")
+	for (s in args)
+		println("#1 Arg: $s")
 
-    var i = 0
-    while (i < args.size)
-    	println("#2 Arg: ${args[i++]}")
+	var i = 0
+	while (i < args.size)
+		println("#2 Arg: ${args[i++]}")
 
-    /*for ((name, value) in System.getProperties())
-    	if (name.startsWith("java"))
-    		println("Sys prop: $name=$value")*/
+	for (i in (0..args.size - 1))
+		println("#3 Arg: ${args[i]}")
 }

kotlin-demo/src/main/kotlin/Test.kt

 package test
 fun main(args : Array<String>) {
-	for ((name, value) in System.getProperties())
-		if (name.startsWith("java"))
-			println("Sys prop: $name=$value")
 }

kotlin-demo/src/main/kotlin/TypesDemo.kt

+package typedemo
+fun main(args : Array<String>) {
+	println(123.javaClass)
+	println('x'.javaClass)
+	println("hello".javaClass)
+
+	// runtime error?
+	//println(123.99.javaClass)
+}