https://kotlinlang.org logo
Title
k

kastork

12/07/2017, 8:53 PM
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

r4zzz4k

12/07/2017, 8:59 PM
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

kastork

12/07/2017, 9:01 PM
I was hoping to not have nullable fields
er, properties
r

r4zzz4k

12/07/2017, 9:09 PM
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

karelpeeters

12/07/2017, 9:17 PM
If you need to do this a lot maybe write a delegate for it?
k

kastork

12/07/2017, 9:18 PM
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

r4zzz4k

12/07/2017, 9:29 PM
@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

karelpeeters

12/07/2017, 9:30 PM
Otherwise, you're right, that wouldn't work.