Created
August 22, 2016 05:31
-
-
Save igorlukanin/920cb178c0adfbddd8ea5f3c4b25d316 to your computer and use it in GitHub Desktop.
wtf-kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Wtf | |
// Primitives | |
operator fun Int.plus(that: String) = "$this$that" | |
operator fun String.plus(that: Int) = "$this$that" | |
// Dictionary | |
class Dictionary<K, V>(val pairs: Map<K, V>) { | |
override fun toString() = "[object Object]" | |
} | |
fun <K, V> dictOf(vararg pairs: Pair<K, V>) = Dictionary<K, V>(mapOf(*pairs)) | |
// Array | |
class Array<T>(val elements: kotlin.Array<out T>) { | |
override fun toString() = elements.joinToString(",") | |
} | |
fun <T> arrayOf(vararg elements: T) = Array<T>(elements) | |
// Dict/Array operations | |
operator fun Array<*>.plus(that: Dictionary<*, *>) = "[object Object]" | |
operator fun Dictionary<*, *>.plus(that: Array<*>) = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Wtf | |
import junit.framework.TestCase | |
class WtfTests : TestCase() { | |
fun testAddNumberToString() { | |
assertEquals("12", 1 + "2") | |
} | |
fun testAddStringToNumber() { | |
assertEquals("12", "1" + 2) | |
} | |
fun testCastEmptyDictToString() { | |
val dict = dictOf<String, Int>() | |
assertEquals("[object Object]", dict.toString()) | |
} | |
fun testCastEmptyArrayToString() { | |
val array = arrayOf<Int>() | |
assertEquals("", array.toString()) | |
} | |
fun testCastArrayToString() { | |
val dict = arrayOf(1, 2, 3) | |
assertEquals("1,2,3", dict.toString()) | |
} | |
fun testAddArrayToDict() { | |
val array = arrayOf<Int>() | |
val dict = dictOf<String, Int>() | |
assertEquals("[object Object]", array + dict) | |
} | |
fun testAddDictToArray() { | |
val array = arrayOf<Int>() | |
val dict = dictOf<String, Int>() | |
assertEquals(0, dict + array) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment