From 731d854af80a3dfd0201ea3ef4c7f8fedd92cd2a Mon Sep 17 00:00:00 2001 From: joey Date: Wed, 11 Dec 2019 14:37:22 -0800 Subject: [PATCH] Compile fixes --- kotlin-examples/.gitignore | 3 ++- kotlin-examples/src/_01_beans/KotlinBeans.kt | 4 ++-- kotlin-examples/src/_02_extensions/Filter.kt | 4 ++-- kotlin-examples/src/_02_extensions/MultiDecl.kt | 4 ++-- .../src/_02_extensions/StringIterator.kt | 2 +- kotlin-examples/src/_02_extensions/guava.kt | 14 +++++++------- .../src/_02_extensions/loopWithIndex.kt | 2 +- kotlin-examples/src/_03_swing/SwingBuilder.kt | 4 ++-- kotlin-examples/src/_03_swing/SwingDemo.kt | 4 ++-- kotlin-examples/src/_03_swing/html/html.kt | 6 +++--- kotlin-examples/src/_05_nulls/Files.kt | 2 +- kotlin-examples/src/_07_annotations/Tests.kt | 3 ++- kotlin-examples/src/_08_collections/collections.kt | 2 +- 13 files changed, 28 insertions(+), 26 deletions(-) diff --git a/kotlin-examples/.gitignore b/kotlin-examples/.gitignore index 2ecca59..12a959e 100644 --- a/kotlin-examples/.gitignore +++ b/kotlin-examples/.gitignore @@ -1,2 +1,3 @@ lib/kotlin-runtime.jar -libraries/runtime.xml \ No newline at end of file +libraries/runtime.xml +out/ \ No newline at end of file diff --git a/kotlin-examples/src/_01_beans/KotlinBeans.kt b/kotlin-examples/src/_01_beans/KotlinBeans.kt index a845ce3..f7e48a5 100644 --- a/kotlin-examples/src/_01_beans/KotlinBeans.kt +++ b/kotlin-examples/src/_01_beans/KotlinBeans.kt @@ -32,7 +32,7 @@ fun main(args: Array) { - val users = arrayList(john, jane, poss) + val users = arrayListOf(john, jane, poss) for (user in users) { println(user) @@ -51,7 +51,7 @@ fun main(args: Array) { - val byNickName = hashMap( + val byNickName = hashMapOf( "Johny" to john, "Jan" to jane, "Boo" to poss diff --git a/kotlin-examples/src/_02_extensions/Filter.kt b/kotlin-examples/src/_02_extensions/Filter.kt index 692bf8e..efb5599 100644 --- a/kotlin-examples/src/_02_extensions/Filter.kt +++ b/kotlin-examples/src/_02_extensions/Filter.kt @@ -32,7 +32,7 @@ fun main(args : Array) { - println("${over30.size()} user(s) over 30 years old") + println("${over30.size} user(s) over 30 years old") @@ -41,7 +41,7 @@ fun main(args : Array) { - val youngestOver20 = users filter {u -> u.age > 20 } min {a, b -> a.age - b.age} + val youngestOver20 = users.filter {u -> u.age > 20 }.min {a, b -> a.age - b.age} println("Youngest over 20: $youngestOver20") } diff --git a/kotlin-examples/src/_02_extensions/MultiDecl.kt b/kotlin-examples/src/_02_extensions/MultiDecl.kt index ac5a964..c581b76 100644 --- a/kotlin-examples/src/_02_extensions/MultiDecl.kt +++ b/kotlin-examples/src/_02_extensions/MultiDecl.kt @@ -13,8 +13,8 @@ fun main(args : Array) { // testIndexedIteration() } -fun Point.component1() = x -fun Point.component2() = y +operator fun Point.component1() = x +operator fun Point.component2() = y diff --git a/kotlin-examples/src/_02_extensions/StringIterator.kt b/kotlin-examples/src/_02_extensions/StringIterator.kt index e822dd9..c4865a4 100644 --- a/kotlin-examples/src/_02_extensions/StringIterator.kt +++ b/kotlin-examples/src/_02_extensions/StringIterator.kt @@ -28,7 +28,7 @@ fun main(args : Array) { -fun String.iterator() = StringIterator(this) +operator fun String.iterator() = StringIterator(this) class StringIterator(val str: String): Iterator { diff --git a/kotlin-examples/src/_02_extensions/guava.kt b/kotlin-examples/src/_02_extensions/guava.kt index 63ede00..bcd6a62 100644 --- a/kotlin-examples/src/_02_extensions/guava.kt +++ b/kotlin-examples/src/_02_extensions/guava.kt @@ -5,7 +5,7 @@ import com.google.common.base.Function import com.google.common.base.Predicate fun main(args: Array) { - val list = arrayList("a", "bb", "acc") + val list = arrayListOf("a", "bb", "acc") @@ -37,10 +37,10 @@ fun main(args: Array) { } -fun p(body: (T) -> Boolean): Predicate +fun p(body: (T) -> Boolean): Predicate = object : Predicate { - public override fun apply(p0: T): Boolean { - return body(p0) + public override fun apply(p0: T?): Boolean { + return body(checkNotNull(p0)) } public override fun equals(p0: Any?): Boolean { @@ -48,10 +48,10 @@ fun p(body: (T) -> Boolean): Predicate } } -fun gf(body: (String) -> T): Function +fun gf(body: (String) -> T): Function = object : Function { - public override fun apply(p0: String): T { - return body(p0) + public override fun apply(p0: String?): T { + return body(checkNotNull(p0)) } public override fun equals(p0: Any?): Boolean { throw UnsupportedOperationException() diff --git a/kotlin-examples/src/_02_extensions/loopWithIndex.kt b/kotlin-examples/src/_02_extensions/loopWithIndex.kt index 877d64a..6d7af3e 100644 --- a/kotlin-examples/src/_02_extensions/loopWithIndex.kt +++ b/kotlin-examples/src/_02_extensions/loopWithIndex.kt @@ -1,7 +1,7 @@ package _02_extensions.loopWithIndex fun main(args: Array) { - val list = arrayList("a", "b", "c") + val list = arrayListOf("a", "b", "c") list.loopWithIndex({i, v -> println("list[$i] = $v")}) } diff --git a/kotlin-examples/src/_03_swing/SwingBuilder.kt b/kotlin-examples/src/_03_swing/SwingBuilder.kt index b3c5047..3b19600 100644 --- a/kotlin-examples/src/_03_swing/SwingBuilder.kt +++ b/kotlin-examples/src/_03_swing/SwingBuilder.kt @@ -67,11 +67,11 @@ var JFrame.size : Pair get() = Pair(getSize().getWidth().toInt(), getSize().getHeight().toInt()) set(dim) {setSize(Dimension(dim.first, dim.second))} -var JFrame.height : Int +var JFrame.heightInt : Int get() = getSize().getHeight().toInt() set(h) {setSize(width, h)} -var JFrame.width : Int +var JFrame.widthInt : Int get() = getSize().getWidth().toInt() set(w) {setSize(height, w)} diff --git a/kotlin-examples/src/_03_swing/SwingDemo.kt b/kotlin-examples/src/_03_swing/SwingDemo.kt index ca8119d..e34a1cf 100644 --- a/kotlin-examples/src/_03_swing/SwingDemo.kt +++ b/kotlin-examples/src/_03_swing/SwingDemo.kt @@ -7,8 +7,8 @@ val greeting = "Hello,\n\nEnter some text here!" fun main(args : Array) { JFrame("Demo") { - height = 400 - width = 400 + heightInt = 400 + widthInt = 400 val text = JTextArea(greeting) center = text diff --git a/kotlin-examples/src/_03_swing/html/html.kt b/kotlin-examples/src/_03_swing/html/html.kt index e378fb9..5cc38fa 100644 --- a/kotlin-examples/src/_03_swing/html/html.kt +++ b/kotlin-examples/src/_03_swing/html/html.kt @@ -11,13 +11,13 @@ import javax.swing.JScrollPane import java.util.ArrayList class Attribute(val name : String, val value : String) { - fun toString() = """$name="$value" """ + override fun toString() = """$name="$value" """ } abstract class Tag(val name : String) { val children : MutableList = ArrayList() val attributes : MutableList = ArrayList() - open fun toString() = "<$name${attributes.join(prep = " ")}>${children.join()}" + override fun toString() = "<$name${attributes.join(prep = " ")}>${children.join()}" } class HTML : Tag("html") @@ -29,7 +29,7 @@ class Text(val text : String) : Tag("b") { override fun toString() = text } -fun Tag.doInit(t : T, f : T.() -> Unit) : T{ +fun Tag.doInit(t : T, f : T.() -> Unit) : T{ t.f() children.add(t) return t diff --git a/kotlin-examples/src/_05_nulls/Files.kt b/kotlin-examples/src/_05_nulls/Files.kt index 1837d53..36ad827 100644 --- a/kotlin-examples/src/_05_nulls/Files.kt +++ b/kotlin-examples/src/_05_nulls/Files.kt @@ -71,4 +71,4 @@ fun main(args : Array) { println(files?.size ?: "no files") } -fun fail() = throw IllegalArgumentException() \ No newline at end of file +fun fail(): Nothing = throw IllegalArgumentException() \ No newline at end of file diff --git a/kotlin-examples/src/_07_annotations/Tests.kt b/kotlin-examples/src/_07_annotations/Tests.kt index eacba2d..24fd3f7 100644 --- a/kotlin-examples/src/_07_annotations/Tests.kt +++ b/kotlin-examples/src/_07_annotations/Tests.kt @@ -4,7 +4,8 @@ import org.junit.Test as test import org.junit.Assert.* class Tests { - test fun simple() { + @test + fun simple() { assertEquals(4, 2 * 2) } } diff --git a/kotlin-examples/src/_08_collections/collections.kt b/kotlin-examples/src/_08_collections/collections.kt index c5d277a..4e235ca 100644 --- a/kotlin-examples/src/_08_collections/collections.kt +++ b/kotlin-examples/src/_08_collections/collections.kt @@ -9,7 +9,7 @@ fun print(list: List) { } fun main(args: Array) { - val l: ArrayList = arrayList(1, 2, 3) + val l: ArrayList = arrayListOf(1, 2, 3) print(l) }