From the guide I know that `lateinit` vars cannot ...
# getting-started
k
From the guide I know that
lateinit
vars cannot be primitive types. If you're trying to create a bean-compliant class, how do you designate an
Integer
or
Double
type? If you make the property type
Int
you get the error that primitive types can't be
lateinit
, if you give it the type
Integer
you get the warning that you shouldn't use
Integers
but should use
Int
.. And for
Double
there doesn't even appear to be a way to say you want the Object
Double
and not a primitive
double
r
IIRC
Int?
should always correspond to
Integer
, so you can use that. And you can create additional
@Transient
property with overriden
get()
and
set()
which would allow to avoid constant
!!
-ing. Not the best solution, but should work. Maybe someone could suggest better options though.
k
I was hoping to not have nullable fields
er, properties
r
Well, docs (https://kotlinlang.org/docs/reference/java-interop.html) specifically mention when boxed types are used, and the topic (https://discuss.kotlinlang.org/t/force-hint-of-int-to-java-lang-integer/2441) doesn't have anything useful either. If you stick with wrapper property as I suggested you won't have to deal with nullable property in most of the code anyway.
k
If you need to do this a lot maybe write a delegate for it?
k
I would think that making a class that can be used where a bean is expected by a java library would be a common thing.
In this case it turns out that using a Long and giving everything a default value satisfies the need for a default constructor (no lateinit needed)
thanks for the help guys
r
@karelpeeters, are you sure there is a way to use delegated property in a bean class? I'm not very good in specifics of desktop Java but I definitely remember there was some case in Android where delegated property couldn't be used, probably required actual backing field or something.
k
Otherwise, you're right, that wouldn't work.