xii
12/15/2020, 2:57 PMxii
12/15/2020, 2:57 PMandylamax
12/15/2020, 2:58 PMxii
12/15/2020, 2:58 PMfun test(): List<Any> {
val test = listOf(1, 3, 4)
return test.map { number ->
try {
if (number == 4) throw IllegalArgumentException("aaaa")
number+1
} catch (e: Exception){
throw e
}
}
}xii
12/15/2020, 2:58 PMxii
12/15/2020, 2:59 PMChantry Cargill
12/15/2020, 3:00 PMxii
12/15/2020, 3:01 PMRob Elliot
12/15/2020, 3:01 PMandylamax
12/15/2020, 3:02 PMxii
12/15/2020, 3:02 PMRob Elliot
12/15/2020, 3:02 PMxii
12/15/2020, 3:02 PMType inference failed. Expected type mismatch:
required:
List<Number>
found:
List<Any>Rob Elliot
12/15/2020, 3:02 PMxii
12/15/2020, 3:03 PMxii
12/15/2020, 3:03 PMxii
12/15/2020, 3:04 PMfun test(): List<Number> {
val test = listOf(1, 3, 4)
return test.map { number ->
try {
if (number == 4) throw IllegalArgumentException("aaaa")
number+1
} catch (e: Exception){
handle(e)
}
}
}
fun handle(e: Exception){
throw e
}xii
12/15/2020, 3:04 PMRob Elliot
12/15/2020, 3:05 PMhandle return NothingRob Elliot
12/15/2020, 3:06 PMprivate fun handle(e: Exception): Nothing {
throw e
}xii
12/15/2020, 3:06 PMxii
12/15/2020, 3:06 PMRob Elliot
12/15/2020, 3:17 PMNothing is a bottom type - that is, it is a subtype of every type. So the nearest common supertype of Int and Nothing is Int.
Whereas a function that has no explicit return type actually has a return type of Unit, and Unit is a normal type, so the nearest common supertype of Int and Unit is Any.
In general Nothing can be used as the return type of code that will never actually return.xii
12/15/2020, 3:18 PMxii
12/15/2020, 3:18 PMChantry Cargill
12/15/2020, 3:19 PMRob Elliot
12/15/2020, 3:19 PMxii
12/15/2020, 3:21 PMxii
12/15/2020, 3:21 PMChantry Cargill
12/15/2020, 3:21 PMxii
12/15/2020, 3:22 PMChantry Cargill
12/15/2020, 3:22 PMxii
12/15/2020, 3:22 PMRob Elliot
12/15/2020, 3:23 PMEither<L, R> is a generic example of this pattern that’s common in functional statically type checked languages; arrow has an implementation.Chantry Cargill
12/15/2020, 3:28 PMsealed class Example
class A : Example()
class B : Example()
fun example(): HttpResponse<Example> {
val obj: Example = exampleService.getExample()
return when (obj) {
is A -> HttpResponse.ok(obj)
is B -> HttpResponse.badRequest(obj)
}
}Nir
12/15/2020, 3:36 PMChantry Cargill
12/15/2020, 3:45 PMNir
12/15/2020, 4:08 PMChantry Cargill
12/15/2020, 4:13 PMNir
12/15/2020, 4:14 PMNir
12/15/2020, 4:15 PMChantry Cargill
12/15/2020, 4:17 PMChantry Cargill
12/15/2020, 4:19 PMNir
12/15/2020, 4:23 PMNir
12/15/2020, 4:24 PMChantry Cargill
12/15/2020, 4:25 PMNir
12/15/2020, 4:25 PMNir
12/15/2020, 4:26 PMgetOr on your sealed success/error classes for the 20th time is going to get oldChantry Cargill
12/15/2020, 4:27 PMNir
12/15/2020, 4:31 PMNir
12/15/2020, 4:31 PMNir
12/15/2020, 4:31 PMNir
12/15/2020, 4:32 PM