View measuretimeContract.kt
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
@ExperimentalContracts | |
fun main(args: Array<String>) { | |
var assign: String | |
val time = measureTimeMillis { | |
assign = "Value Assigned" | |
} | |
println("$time ms. spent assigning: '$assign'") | |
} | |
@ExperimentalContracts |
View measuretime.kt
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
fun main(args: Array<String>) { | |
var assign: String | |
val time = measureTimeMillis { | |
assign = "Value Assigned" | |
} | |
println("$time ms. spent assigning: '$assign!!'") | |
} | |
private inline fun measureTimeMillis(block: () -> Unit): Long { | |
val start = System.currentTimeMillis() |
View contract.kt
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
contract { | |
returns(true) implies (this@isComplete != null) | |
} |
View withcontract.kt
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
@ExperimentalContracts | |
fun User?.isComplete(): Boolean { | |
contract { returns(true) implies (this@isComplete != null) } | |
return this != null && this.isValid() | |
} |
View usage2.kt
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
fun printUserDetails(user: User?) { | |
if(user.isComplete()) { | |
println("${user!!.name} ${user!!.email}") | |
} | |
} |
View use.kt
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
fun printUserDetails(user: User?) { | |
if(user?.isValid()) { | |
println("${user.name} ${user.email}") | |
} | |
} |
View userclass.kt
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
data class User(val name: String?, val email: String?) { | |
fun isValid() = !name.isNullOrBlank() && name.length > 3 | |
&& !email.isNullOrBlank() && email.length > 3 | |
} |
View method.java
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
var variable = returnValue(); // Type is inferred from the return type of the method | |
// Method declaration | |
public String returnValue() { | |
return "w00p"; | |
} |
View example3.java
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
var strings = Arrays.asList("a", "b", "c"); | |
for(var str: strings) { | |
System.out.println(str); | |
} |
View interface.java
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
// We can explicitly work against interfaces | |
List<String> list = new ArrayList(); // type: List<String> | |
// Using var, we can't do this without casting | |
var list = new ArrayList<String>(); // Inferred type ArrayList<String> | |
var list = (List)new ArrayList<String>(); // Inferred type List<String> |
NewerOlder