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
data class KotlinUserWithNulls(val firstName: String?, | |
// String? means that it is either a String object or a null | |
val lastName: String?, | |
val addresses: List<Address> = listOf()) { | |
data class Address(val city: String?) | |
companion object { | |
fun fetchFirstCity(user: KotlinUserWithNulls?): String? { | |
user?.addresses?.forEach { it.city?.let { return it } } |
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
data class KotlinUserWithoutNulls(val firstName: String, | |
// this parameter can't be null | |
val lastName: String, | |
val addresses: List<Address> = listOf()) { | |
data class Address(val city: String) | |
companion object { | |
fun fetchFirstCity(user: KotlinUserWithNulls) = | |
user.addresses.first().city |
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
public class JavaUser { | |
// ... | |
private final String firstName; | |
private final String lastName; | |
private final List<Address> addresses; | |
public Address getFirstAddress() { | |
Address firstAddress = addresses.get(0); |
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
data class KotlinUser(val firstName: String, | |
val lastName: String, | |
val addresses: List<Address> = listOf()) { | |
data class Address(val city: String) | |
/** | |
* This is the same as in `JavaUser`. | |
*/ | |
fun getFirstAddressNoInference(): Address { |
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
/** | |
* Here the type of `firstAddress` is inferred from the context. | |
*/ | |
fun getFirstAddressInferred(): Address { | |
val firstAddress = addresses.first() | |
return firstAddress | |
} |
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
/** | |
* Here the return type is inferred. Note that | |
* if a method consists of only one statement | |
* you can omit the curly braces. | |
*/ | |
fun getFirstAddress() = addresses.first() |
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
public class JavaLineLoader { | |
public List<String> loadLines(String path) { | |
List<String> lines = new ArrayList<>(); | |
try(BufferedReader br = new BufferedReader(new FileReader(path))) { | |
String line; | |
while((line = br.readLine()) != null) { | |
lines.add(line); | |
} | |
} catch (IOException e) { |
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 KotlinLineLoader { | |
fun loadLines(path: String) = File(path).readLines() | |
} |
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
public class JavaFilterOperation { | |
private List<String> items; | |
@FunctionalInterface | |
interface FilterOperation { | |
Boolean filter(String element); | |
} | |
private List<String> filterBy(FilterOperation fn) { |
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 KotlinFilterOperation { | |
private val items = listOf<String>() | |
fun filterBy(fn: (String) -> Boolean) = items.filter(fn) | |
fun doFilter() { | |
filterBy(String::isNotEmpty) | |
// note the exension function `isNotEmpty` added to `String`! | |
} |