Add structured coroutines and explicit error handling
This commit is contained in:
parent
de1262b4cf
commit
fe62e81152
31 changed files with 1701 additions and 325 deletions
|
|
@ -13,25 +13,6 @@ class PrefixGreeter(val prefix: String): Greeter {
|
|||
}
|
||||
}
|
||||
|
||||
worker Counter {
|
||||
var count = 0
|
||||
|
||||
fun increment() {
|
||||
count += 1
|
||||
}
|
||||
|
||||
fun value(): Int {
|
||||
return count
|
||||
}
|
||||
}
|
||||
|
||||
fun risky(input: String): String {
|
||||
if (input == "boom") {
|
||||
throw "boom requested"
|
||||
}
|
||||
return input
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val greeter: Greeter = PrefixGreeter("hello")
|
||||
println(greeter.greet("gotlin"))
|
||||
|
|
@ -44,35 +25,25 @@ fun main() {
|
|||
val upper = strings.ToUpper("gotlin")
|
||||
fmt.Println("interop:", upper)
|
||||
|
||||
val result = runCatching({ risky("boom") })
|
||||
if (result.isSuccess()) {
|
||||
println("runCatching: success")
|
||||
} else {
|
||||
println("runCatching:")
|
||||
println(result.exceptionOrNull())
|
||||
}
|
||||
|
||||
val maybe: any = null
|
||||
val maybe: String? = null
|
||||
if (maybe == null) {
|
||||
println("null check works")
|
||||
}
|
||||
|
||||
val counter = Counter()
|
||||
counter.increment()
|
||||
counter.increment()
|
||||
fmt.Println("worker value:", counter.value())
|
||||
|
||||
val ready = Channel<String>()
|
||||
go {
|
||||
select {
|
||||
after(120) -> ready.send("timer fired")
|
||||
runBlocking {
|
||||
val ready = Channel<String>()
|
||||
launch {
|
||||
delay(120)
|
||||
ready.send("timer fired")
|
||||
}
|
||||
select {
|
||||
ready -> println("channel says: " + it)
|
||||
}
|
||||
}
|
||||
select {
|
||||
ready -> println("channel says: " + it)
|
||||
}
|
||||
|
||||
select {
|
||||
every(50) -> println("one periodic tick")
|
||||
val answer = async<Int> {
|
||||
delay(50)
|
||||
return 42
|
||||
}
|
||||
fmt.Println("async value:", answer.await())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue