Why is it not possible to have a custom getter on ...
# getting-started
a
Why is it not possible to have a custom getter on
lateinit
? I use
isInitialized
everytime I access
lateinit
, so I wonder if I could optimize that and only do the check once. In case of a nullable type, I could do something like that.
n
not sure i understood your question but have you looked at lazy delegates?
👍 1
p
Why are you using
lateinit
if you always check
isInitialized
? Just use nullable type and check
!= null
. And why do you throw exception in your example? Just use
lateinit
and exception will be thrown if field is not initialized.
a
Is it possible to throw a custom exception on `lateinit`if it’s not initialized? Because that’s exactly what I’m trying to do, calling
isInitialized
once and throw a custom exception if not initialized, same if it would be nullable type in code snippet.
n
it’s by default you’ll have a
NotInitializedException
if i remember correctly
a
Yep, and if it would a nullable type, I would need to use the safe call operator at every time I access the object. But I do believe it should be
lateinit
as the object is being set later in a setter and MUST not be null.
p
So why do you need custom exception here? If your really need it, you can write your own property delegate, see
Delegates.notNull
as an example.
👍 1
a
Because I’m working on a library and would be better to have more descriptive message along side the exception.
Yep, something like
Delegates.notNull
would work. Thank you.
👍 1
m
lateinit
is generally used as a ‘hint/helper’ to the compiler that this variable will be non-null BUT isn’t getting set at construction time. Very commonly used with DI frameworks so can define the dependency as non-null BUT it gets defined later. What you’re doing is not a good candidate for lateinit IMO. If the consumer of the library needs to pass it, make the variable non-null, check it, and throw your exception. If it is null, then after the !null check, you can reference it as non-null.
a
@Mike Totally agree with you. But when working on a library that will be used be other developers, I must well define if some methods take nullable objects or not, specifically when there’s not guarantee if some method is called before the
setter
method. Hence, I prefer using delegates in this case, in which I could define that this method not accepting nullable object and could throw custom exception in case some other methods are called before init that object.
m
I understand that, just don’t feel
lateinit
is the way to ‘solve’ it. Are you worried about interop with Java? I must assume so as otherwise you wouldn’t need to ‘jump through hoops’. I would suggest EITHER taking a null as the parameter, and doing null checking in Kotlin with custom exception (as you want a custom exception) OR annotate with @Nonnull annotations, and let consumers know they must pay attention.
And backing up, are you using
lateinit
on a function parameter? That’s not clear, so I may be completely off here.
a
No, I use
Delegates.notNullOrException
as extension function, so I could do something like that
private var config: Config by Delegates.notNullOrException(errorMessage = "configurations must be set")