karelpeeters
09/09/2017, 3:22 PMnull
-related problems in Java is to return a "default value", and this works especially well with collections, there's rarely a reason for them to be null. Just make your mehods return empty collections instead.prat
09/09/2017, 5:32 PMkarelpeeters
09/09/2017, 5:34 PMcodeslubber
09/09/2017, 8:19 PMkarelpeeters
09/09/2017, 8:20 PMcodeslubber
09/09/2017, 8:34 PMkarelpeeters
09/09/2017, 8:35 PMnull
. To stick with your Person
example, let's say it has a list of friends, that should be an empty list from day 1.codeslubber
09/09/2017, 8:40 PMraulraja
09/09/2017, 9:14 PMnull
as unrelated to absence. null
looks like a missing value on a valid reference but for the rest of modeling concerns there are proper datatypes that are mainstream in many other langs beside java/kotlin. Option
comes to mind, but despite Kotlin's superior treatment of nullable types
over other langs like Scala I still see myself and Kotlin code where null is present everywhere whereas in Scala for example everyone uses Option
to model absence and it's rare to see null
in code bases at all.karelpeeters
09/09/2017, 9:15 PMOptional
vs a nullable value in Kotlin? Both force you to handle to "null" case.raulraja
09/09/2017, 9:25 PMOptional
, scala.Option
and nullable types ?
in Kotlin have operators that let you shoot yourself in the foot !!
or .get
when really the proper treatment should be to consider folding the datatype contemplating the absence with a default
value or path. Kotlin's lack of monadic comprehension which is something we address in Kategory does not really let you compute over monadic values such as those of option in a way that does not require nested lambdas or to much boilerplate IMO. Most langs that favor Option
style datatypes over null checks
have syntax to compute over the assumption that the value is fulfilled and if it is ever empty it shortcircuits yielding None
or something equivalent.
Nullable types are definitely an improvement but you can get away ignoring null
altogether with a proper Option
datatype in the same way we use List to represent multiple values.raulraja
09/09/2017, 9:27 PM?
is more efficient from a performance standpoint since it's implemented as part of the Kotlin typesystem rather than an actual datatype subject to instantiation and other things that nullable types probably don't do.prat
09/09/2017, 9:57 PMOptional
is a bit more composable with more APIs e.g. map
, flatMap
, filter
, append
, forEach
karelpeeters
09/09/2017, 9:59 PM!!
is a compiler warning, right?karelpeeters
09/09/2017, 9:59 PMAny?
?raulraja
09/09/2017, 10:06 PM!!
is a lot better than Optional.get
or Option.get
which IMO it should only appear in scene if you do something like import Option.unsafe.*
or something along those lines so you understand what you are doing. This also applies to list[0]
and all other unsafe method/operators. A lot of bugs could be prevented if these features were opt in.prat
09/09/2017, 10:07 PMlet
is actually good enough for me