Florian
11/13/2020, 9:22 PMalex
11/13/2020, 9:30 PMIrving Rivas
11/13/2020, 9:31 PMacc
and i
. acc
’s type is determined from the 0 passed to fold as the first parameter and is considered to be the expected return type of the lambda. but i
comes from the list, which could have other types, like Long or Double or String, thus altering result
’s type — adding an Int and a Long yields a Long, for instance. If you have the type in the arguments, it gets caught at the exact place of the issue (argument is not of the expected type), otherwise you get a return type mismatch.“)Florian
11/13/2020, 9:33 PMFlorian
11/13/2020, 9:34 PMFlorian
11/13/2020, 9:34 PMIrving Rivas
11/13/2020, 9:34 PMFlorian
11/13/2020, 9:34 PMIrving Rivas
11/13/2020, 9:38 PMIrving Rivas
11/13/2020, 9:39 PMIrving Rivas
11/13/2020, 9:39 PMIrving Rivas
11/13/2020, 9:39 PMFlorian
11/13/2020, 9:41 PMFlorian
11/13/2020, 9:42 PMIrving Rivas
11/13/2020, 9:43 PMIrving Rivas
11/13/2020, 9:43 PMval items = listOf(1, 2L, 3, 4L, 5)
, for instance, the lambda will throw.Florian
11/13/2020, 9:44 PMFlorian
11/13/2020, 9:44 PMIrving Rivas
11/13/2020, 9:44 PMFlorian
11/13/2020, 9:45 PMephemient
11/14/2020, 12:46 AMitems.fold(null) { acc, i -> ... }
isn't possible, the type has to be given with one of
items.fold<Int, String?>(null) { acc, i -> ... }
items.fold(null as String?) { acc, i -> ... }
items.fold(null) { acc: String?, i -> ... }
and out of these options, I'd prefer the last one