Dias
10/03/2019, 1:38 PMBob Glamm
10/03/2019, 1:42 PMstreetsofboston
10/03/2019, 1:43 PMT?
and an Optional<T>
is that T
is assignable to a T?
but a T
is not assignable to an Optional<T>
.
That is what I like about T?
, nullable types.
Without that assignability, such as with Optional<T>
, you’d have to use things such as map
and flatMap
, etc to handle transformationsrobstoll
10/03/2019, 1:44 PMT?
streetsofboston
10/03/2019, 1:44 PMnullableValue?.something()?.name
vs optionalValue.flatMap { something() }.map { it.name }
Dias
10/03/2019, 1:45 PMstreetsofboston
10/03/2019, 1:46 PMnull
value is of type Nothing?
and that makes dealing with null values easier.Dias
10/03/2019, 1:46 PMBob Glamm
10/03/2019, 1:46 PMstreetsofboston
10/03/2019, 1:47 PMOptional
maybe preferred, since you’ll be using Either
, Try
, etc as well. The ‘Optional’ fits in this monadic framework, while T?
may not….Bob Glamm
10/03/2019, 1:47 PMstreetsofboston
10/03/2019, 1:50 PMT?
is preferred over a (homegrown) Optional<T>
, in my opinion.
If you use frameworks such as Arrow, and you want to make full use of their functional/monadic properties, then use `Optional<T>`/
Most folks I know never use(d) functional/monadic frameworks. Using T?
is a great solution to deal with nullabilityBob Glamm
10/03/2019, 1:50 PMstreetsofboston
10/03/2019, 1:51 PMrobstoll
10/03/2019, 1:51 PMT
is easy, doing the same for T?
and T
is currently not even possibleT?
streetsofboston
10/03/2019, 1:53 PMT
but not by T?
🙂robstoll
10/03/2019, 1:53 PMT
and not by Optional<T>
streetsofboston
10/03/2019, 1:54 PMrobstoll
10/03/2019, 1:55 PMString?
but not for String
streetsofboston
10/03/2019, 1:56 PM..orBlank()
part makes sense…: "".isNullOrBlank()
karelpeeters
10/03/2019, 1:56 PM?:
actually, it should not be "defined" for T: Any
.robstoll
10/03/2019, 1:56 PMisBlank
IMOstreetsofboston
10/03/2019, 1:57 PM?:
on a T: Any
? I think it does, but i’m not sure….karelpeeters
10/03/2019, 2:30 PMisNullOrBlank
and possibly many other user-defined functions.Optional<T>
over T?
)andrzej
10/03/2019, 2:57 PMval u = ds.findUser(userId)
if (u == null) log.debug("it was empty")
return u?.let { doSomething(it) }
in Scala:
ds.findUser(userId).map(doSomething).getOrElse { log.debug("it was empty"); None }
Scala code from my personal view in this case is more concise, expressive, explicit, you don't use or see null
at all in the code.val mapped = value?.let { transformValue(it) } ?: defaultValueIfValueIsNull
Scala:
val mapped = value.map(transformValue).orElse(defaultValueIfValueIsNull)
henrik
10/09/2019, 7:23 AM