https://kotlinlang.org logo
Title
s

supaham

08/26/2017, 6:39 PM
I'm writing a DAO in Kotlin. I would like to, and believe it to be acceptable, throw an error if they try to read a value that isn't set. lateinit does the trick, but not for fields that are nullable. Any suggestions without actually writing exception throwing for each field
k

karelpeeters

08/26/2017, 7:02 PM
s

supaham

08/29/2017, 2:12 AM
I don't think this is an option. Kotlin doesn't seem to like delegate properties where getValue returns nullable
Nevermind, it can work. However the field must be nullable as well as getValue return value. A workaround is to create two implementations a
OptionalPresence
and
OptionalNullablePresence
Delegation isnt an option for me. I need to find out whether a field is present or not. Delegate properties don't offer the ability to access the delegate class's members. The alternative solution is to try-catch every get operation, but that sounds terrible. I'll stick to an java Optional style implementation