Last active
May 27, 2017 19:16
-
-
Save orangy/7fc56097d310d36d452b to your computer and use it in GitHub Desktop.
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
Instead of init-increment-condition "for" statement, we use for-each style: "for (x in 0..10)", compiler optimises as needed. | |
Declare immutable value with "val" and mutable variable with "var", either in class, function or top-level. | |
Missing 'With' from Delphi? :) Standard library has "public inline fun <T, R> with(receiver: T, f: T.() → R): R = receiver.f()" | |
I can't have no fact for today, because type system forbids me from passing null to not-null parameter. | |
Last functional argument of a method can be passed outside of parentheses: https://gist.github.com/orangy/11474248 <— "using statement" DSL | |
There are 82 contributors to https://github.com/JetBrains/kotlin/ … and only 12 full-time JetBrains developers working on it. | |
There are 217 repositories on @github about/with Kotlin. https://github.com/search?q=kotlin | |
By convention, if some type has properties named component1, component2, etc, you can decompose value like "val (a,b) = pair" | |
You can pass functional literal to Java method expecting interface with single method. Compiler implements interface for you. | |
Type "C.(T) → R" is a function type invoked on an instance of type C, gets argument of type T and returns R. | |
Non-public functions and properties can omit type specification, it will be inferred: "class C { val x = 1 + 1 }" | |
Missing C# events? Define them with simple DSL: https://gist.github.com/orangy/11178911 | |
"if" is an expression, so you can use it like ternary operator: "fun fn(a : Boolean, b : String, c : String) = if (a) b else c" | |
Powerful "when" expression is replacing complex switch/case statements, and allows for multiple type checks with automatic cast. | |
Language doesn't have pattern matching because we figured that it is really needed mostly to compiler developers. | |
Operators are resolved by convention, so you can provide operators for any type, even "invoke" for call operator () | |
Trait is a type without a state which you cannot instantiate directly. You can inherit multiple traits by a class. | |
Extension function "fun String.firstChar() = get(0)" adds firstChar operation to String, and has String as "this" in the body. | |
Delegating properties are powerful for many cases: val property by Delegates.lazy { calcProperty() } | |
Built-in interface delegation and primary ctor: class ItemsCollection(val items: List<String>) : Iterable<String> by items {} | |
Ranges as in 1..10 are useful to iterate over range of numbers, check if value is within range or get string or list subrange. | |
You can declare local functions and classes with closures, inside another function's body. | |
Autocast helps avoid verbose casts after type check: if (obj is String) return obj.length // obj is String after check | |
Compiler can inline functions with lambdas, when instructed to do so, and thus don't build closures et al. in these cases. | |
There is no "new" keyword, creating an instance looks just like function call: val person = Person("John") | |
All types and members in Kotlin are sealed (non-inheritable, non-overridable) by default. | |
This is valid code calling a bunch of functions, no syntax sugar: "items filter { it.valid } map { it.name}" | |
String interpolation makes it easy to build complex formatting: "My name is $name and I'm ${age.years} old" | |
Like in C#, unlike in Java, there is no binding between file and type/function. You can declare top level functions too. | |
"using { ... }" is just a function call, not a special syntactic sugar. You can define your own. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment