From 161edb1f6e76a8451d2e74a8fb24d6874f7dab66 Mon Sep 17 00:00:00 2001 From: Paolo Furini Date: Mon, 12 Sep 2016 16:33:16 +0200 Subject: [kotlin/en] Add "if" usage as an expression (#2367) * [kotlin/en] Add "if" usage as a function * [kotlin/en] Change comment on "if" expression Rephrase the comment to use the term "expression" in place of "function" --- kotlin.html.markdown | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'kotlin.html.markdown') diff --git a/kotlin.html.markdown b/kotlin.html.markdown index 605b1a63..00182d81 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -261,6 +261,14 @@ fun helloWorld(val name : String) { ctr++ } while (ctr < 10) + /* + "if" can be used as an expression that returns a value. + For this reason the ternary ?: operator is not needed in Kotlin. + */ + val num = 5 + val message = if (num % 2 == 0) "even" else "odd" + println("$num is $message") // => 5 is odd + // "when" can be used as an alternative to "if-else if" chains. val i = 10 when { -- cgit v1.2.3 From 2fa414912e2fd9bdcd8fd5aa6df59e70f24dac06 Mon Sep 17 00:00:00 2001 From: Sergey Mashkov Date: Wed, 5 Oct 2016 13:27:41 +0300 Subject: [Kotlin/en] correct object clarification (#2413) * Kotlin: correct object clarification * Kotlin: correct object clarification --- kotlin.html.markdown | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'kotlin.html.markdown') diff --git a/kotlin.html.markdown b/kotlin.html.markdown index 00182d81..4c2f3082 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -337,7 +337,7 @@ enum class EnumExample { /* The "object" keyword can be used to create singleton objects. -We cannot assign it to a variable, but we can refer to it by its name. +We cannot instantiate it but we can refer to its unique instance by its name. This is similar to Scala singleton objects. */ object ObjectExample { @@ -346,6 +346,11 @@ object ObjectExample { } } +fun useObject() { + ObjectExample.hello() + val someRef: Any = ObjectExample // we use objects name just as is +} + ``` ### Further Reading -- cgit v1.2.3 From 98bb8f8432e66c5721bb7cf7b808e750da9d499d Mon Sep 17 00:00:00 2001 From: Sergey Mashkov Date: Wed, 5 Oct 2016 14:34:08 +0300 Subject: [Kotlin/en] Add when smartcast example (#2418) --- kotlin.html.markdown | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'kotlin.html.markdown') diff --git a/kotlin.html.markdown b/kotlin.html.markdown index 4c2f3082..fe2811c5 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -317,6 +317,14 @@ fun helloWorld(val name : String) { println(smartCastExample(0)) // => false println(smartCastExample(true)) // => true + // Smartcast also works with when block + fun smartCastWhenExample(x: Any) = when (x) { + is Boolean -> x + is Int -> x > 0 + is String -> x.isNotEmpty() + else -> false + } + /* Extensions are a way to add new functionality to a class. This is similar to C# extension methods. -- cgit v1.2.3 From 7d00a22bded0b80f67e08fc807e857041f394ae5 Mon Sep 17 00:00:00 2001 From: Sergey Mashkov Date: Wed, 5 Oct 2016 17:59:16 +0300 Subject: [Kotlin/en] Add more destructuring examples (#2419) --- kotlin.html.markdown | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'kotlin.html.markdown') diff --git a/kotlin.html.markdown b/kotlin.html.markdown index fe2811c5..d29b1471 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -175,6 +175,17 @@ fun helloWorld(val name : String) { // Objects can be destructured into multiple variables. val (a, b, c) = fooCopy println("$a $b $c") // => 1 100 4 + + // destructuring in "for" loop + for ((a, b, c) in listOf(fooData)) { + println("$a $b $c") // => 1 100 4 + } + + val mapData = mapOf("a" to 1, "b" to 2) + // Map.Entry is destructurable as well + for ((key, value) in mapData) { + println("$key -> $value") + } // The "with" function is similar to the JavaScript "with" statement. data class MutableDataClassExample (var x: Int, var y: Int, var z: Int) -- cgit v1.2.3 From 858171d7231035a286ae1b1e4aea39435dacb384 Mon Sep 17 00:00:00 2001 From: Jakukyo Friel Date: Wed, 12 Oct 2016 17:47:41 +0800 Subject: Several changes on Kontlin. (#2335) * kotlin: remove unnecessary semicolons. Also replace code block language `java` with `kotlin`. * kotlin: change coding style (Kotlin Reference) * kotlin: anonymous function -> lambda expression Anonymous functions and lambda expressions are different in Kotlin. The code example uses `anonymous function` in the comment, while the code below is in fact lambda expressions. * Remove myself from contributors. Not for trivial changes. * kotlin: fix a typo. Thanks @geoffliu to point out this. * kotlin: change style of default parameter according to Referenec. --- kotlin.html.markdown | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'kotlin.html.markdown') diff --git a/kotlin.html.markdown b/kotlin.html.markdown index d29b1471..d1f1aae6 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -9,7 +9,7 @@ Kotlin is a statically typed programming language for the JVM, Android and the browser. It is 100% interoperable with Java. [Read more here.](https://kotlinlang.org/) -```java +```kotlin // Single-line comments start with // /* Multi-line comments look like this. @@ -42,12 +42,12 @@ fun main(args: Array) { Strings can be represented in a similar way as in Java. Escaping is done with a backslash. */ - val fooString = "My String Is Here!"; - val barString = "Printing on a new line?\nNo Problem!"; - val bazString = "Do you want to add a tab?\tNo Problem!"; - println(fooString); - println(barString); - println(bazString); + val fooString = "My String Is Here!" + val barString = "Printing on a new line?\nNo Problem!" + val bazString = "Do you want to add a tab?\tNo Problem!" + println(fooString) + println(barString) + println(bazString) /* A raw string is delimited by a triple quote ("""). @@ -87,7 +87,7 @@ fun helloWorld(val name : String) { Function arguments can optionally have a default value. The function return type, if required, is specified after the arguments. */ - fun hello(name: String = "world") : String { + fun hello(name: String = "world"): String { return "Hello, $name!" } println(hello("foo")) // => Hello, foo! @@ -119,16 +119,16 @@ fun helloWorld(val name : String) { println(even(7)) // => false // Functions can take functions as arguments and return functions. - fun not(f: (Int) -> Boolean) : (Int) -> Boolean { + fun not(f: (Int) -> Boolean): (Int) -> Boolean { return {n -> !f.invoke(n)} } // Named functions can be specified as arguments using the :: operator. val notOdd = not(::odd) val notEven = not(::even) - // Anonymous functions can be specified as arguments. + // Lambda expressions can be specified as arguments. val notZero = not {n -> n == 0} /* - If an anonymous function has only one parameter + If a lambda has only one parameter then its declaration can be omitted (along with the ->). The name of the single parameter will be "it". */ @@ -139,11 +139,11 @@ fun helloWorld(val name : String) { // The "class" keyword is used to declare classes. class ExampleClass(val x: Int) { - fun memberFunction(y: Int) : Int { + fun memberFunction(y: Int): Int { return x + y } - infix fun infixMemberFunction(y: Int) : Int { + infix fun infixMemberFunction(y: Int): Int { return x * y } } @@ -228,16 +228,16 @@ fun helloWorld(val name : String) { Sequences represent lazily-evaluated collections. We can create a sequence using the "generateSequence" function. */ - val fooSequence = generateSequence(1, {it + 1}) + val fooSequence = generateSequence(1, { it + 1 }) val x = fooSequence.take(10).toList() println(x) // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] // An example of using a sequence to generate Fibonacci numbers: - fun fibonacciSequence() : Sequence { + fun fibonacciSequence(): Sequence { var a = 0L var b = 1L - fun next() : Long { + fun next(): Long { val result = a + b a = b b = result @@ -360,7 +360,7 @@ We cannot instantiate it but we can refer to its unique instance by its name. This is similar to Scala singleton objects. */ object ObjectExample { - fun hello() : String { + fun hello(): String { return "hello" } } -- cgit v1.2.3