This file contains hidden or 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
| // until and step are infix functions | |
| for(i in 0 until 100 step 2) println("$i") | |
| // equivalent to: | |
| for(i in 0.until(100).step(2)) println("$i") | |
| // downTo is an infix function | |
| for(i in 100 downTo 0) println("$i") | |
| // shl, and, xor are infix functions | |
| val i = (0x65acf9 shl 6) and 0x55 xor 0x80 |
This file contains hidden or 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
| class Price(val value: Double, val currency: Currency) | |
| infix fun Int.euro(cents: Int): Price { | |
| return Price(toDouble() + cents / 100.0, Currency.EURO) | |
| } | |
| val price = 1 euro 42 | |
| // equivalent to: | |
| val price = 1.euro(42) |
This file contains hidden or 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
| val listener = object: OnMenuClickListener, OnMenuExpandListener { | |
| override fun onMenuClick(item: Menu) { ... } | |
| override fun onMenuExpand(item: Menu) { ... } | |
| } | |
| menu.setOnClickListener(listener) | |
| menu.setOnExpandListener(listener) |
This file contains hidden or 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
| class Person private constructor(val name: String, val age: Int) { | |
| companion object Factory { | |
| fun create(name: String, age: Int) = Person(name, age) | |
| } | |
| } | |
| // members of the companion object can be called using the class name as qualifier | |
| val bob = Person.create("Bob", 21) |
This file contains hidden or 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
| // object class as a singleton | |
| object Log { | |
| val prop = 42 | |
| fun debug(msg: String) { ... } | |
| fun info(msg: String) { ... } | |
| fun warn(msg: String) { ... } | |
| fun error(msg: String) { ... } | |
| } | |
| // object classes can inherit from classes and interfaces |
This file contains hidden or 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
| class A { | |
| // nested class (equivalent to static in Java) | |
| class B { | |
| // A can't see private members of nested classes | |
| private val invisible = 42 | |
| } | |
| // inner class | |
| inner class C { | |
| // A can't see private members of inner classes |
This file contains hidden or 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
| class Point(val x: Double, val y: Double) { | |
| // addition operator | |
| operator fun plus(other: Point): Point { | |
| return Point(x + other.x, y + other.y) | |
| } | |
| // overload using Double | |
| operator fun plus(value: Double): Point { | |
| return Point(x + value, y + value) | |
| } |
This file contains hidden or 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
| interface View { | |
| fun click() | |
| fun toggle() | |
| } | |
| class ViewImpl: View { | |
| override fun click() { println("click!") } | |
| override fun toggle() { println("toggle!") } | |
| } |
This file contains hidden or 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
| interface A { | |
| val a: Int // abstract | |
| val b: Int // abstract | |
| fun foo() // abstract | |
| fun bar() { | |
| // optionnal body | |
| } | |
| } | |
| interface B { ... } |
This file contains hidden or 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
| // final public class (default) | |
| class A | |
| // final private class (only visible in the file) | |
| private class B | |
| // final internal class (only visible in the module) | |
| internal class C | |
| // abstract public class |