What’s the correct way to signal that a property g...
# announcements
p
What’s the correct way to signal that a property getter failed to execute? I know kotlin prefers sealed class result types to exceptions, but I can’t think of another good way to communicate to the caller that a property getter failed except to return null, which isn’t always a good option.
Or is the answer basically, “if your property hides complex logic that might fail, it should probably just be a method”?
☝🏻 1
☝️ 5
c
Essentially: • exceptions for impossible cases, programming error • null when the failure is expected and you don't really care why it failed • sealed classes when you need to know why Depending on how complex your thing is, it might be better to make it a method but I don't think it changes the problem that much
p
well I mean
“sealed classes when you need to know why” doesn’t fit well with a getter
because now your property is a SomethingResult<String>
instead of a String
which at least to me feels awkward
but maybe it’s just me! It’s just that it doesn’t feel very much like a property at that point, since it’s basically clearly marked as a return value
also means the implementation can’t change in the future
also only tangentially relevant, but it seems worth mentioning that lazy { } has a useful property which is that if the initializer throws, then it reruns the next time. afaict there’s no way to get this behavior without throwing (or avoiding using lazy { } )
z
Or is the answer basically, “if your property hides complex logic that might fail, it should probably just be a method”?
Yea that’s it
p
righto.
thanks.