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
I don't think this is an option. Kotlin doesn't seem to like delegate properties where getValue returns nullable
supaham
08/29/2017, 2:17 AM
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
supaham
08/29/2017, 2:30 AM
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