JVM.Languages.Compare (Java, Kotlin, Groovy, Scala) Talk Repo
A class in Kotlin can have a primary constructor and one or more secondary constructors. The primary constructor is part of the class header: it goes after the class name (and optional type parameters).
class Card constructor(rank: Rank, suit: Suit) : Comparable<Card> {
private val rank = rank;
private val suit = suit;
class Card constructor(private val rank: Rank, private val suit: Suit) : Comparable<Card> {
Classes in Kotlin can have properties. These can be declared as mutable, using the var
keyword or read-only using the val
keyword.
there's no new
keyword in Kotlin
To use a property, we simply refer to it by name, as if it were a field in Java:
The initializer, getter and setter are optional. Property type is optional if it can be inferred from the initializer (or from the getter return type, as shown below).
The full syntax of a read-only property declaration differs from a mutable one in two ways: it starts with val
instead of var
and does not allow a setter
Each enum constant is an object. Enum constants are separated with commas. Just like in Java, enum classes in Kotlin have synthetic methods allowing to list the defined enum constants and to get an enum constant by its name.
We frequently create classes whose main purpose is to hold data. In such a class some standard functionality and utility functions are often mechanically derivable from the data. In Kotlin, this is called a data class and is marked as data:
- equals()/hashCode() pair;
- toString() of the form "User(name=John, age=42)";
- componentN() functions corresponding to the properties in their order of declaration;
- copy() function.
Unlike many languages, Kotlin distinguishes between mutable and immutable collections (lists, sets, maps, etc). Precise control over exactly when collections can be edited is useful for eliminating bugs, and for designing good APIs. It is important to understand up front the difference between a read-only view of a mutable collection, and an actually immutable collection. Both are easy to create, but the type system doesn't express the difference, so keeping track of that (if it's relevant) is up to you. No Multimap in Kotlin
Range expressions are formed with rangeTo
functions that have the operator form ..
which is complemented by in
and !in
.
Range is defined for any comparable type, but for integral primitive types it has an optimized implementation.
The truth is: data classes do not play too well with inheritance. We are considering prohibiting or severely restricting inheritance of data classes.
For example, it's known that there's no way to implement equals()
correctly in a hierarchy on non-abstract classes.
So, all I can offer: don't use inheritance with data classes.
Reference: https://stackoverflow.com/a/26467380
public inline fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R> {
return mapTo(ArrayList<R>(collectionSizeOrDefault(10)), transform)
}
In the code the assertions are done after calling toString()
so that we can assert that the code is correct.
This has to be done using toString()
because in Kotlin you cannot extend a class and implement an interface at the same time.
- Kotlin docs: https://kotlinlang.org/docs/reference/
- StackOverflow: https://stackoverflow.com/a/26467380
- Groovy is a dynamic JVM language which works very well with Java.
- Now an Apache project.
- Working with Collections - http://docs.groovy-lang.org/next/html/documentation/working-with-collections.html
- Differences with Java - http://groovy-lang.org/differences.html
- Style Guide: http://groovy-lang.org/style-guide.html
- Scala is a statically typed JVM language which combines OO and Functional features.
- Mutable and Immutable - https://docs.scala-lang.org/overviews/collections/overview.html
- Documentation: https://docs.scala-lang.org/
- Scala for Java Programmers - https://docs.scala-lang.org/tutorials/scala-for-java-programmers.html