Naveen
01/06/2025, 10:22 PMlateinit
when defining variables due to the fact that it is only makes sense when using Java interop. Is there any other specific reason to not use lateinit
?agrosner
01/07/2025, 1:53 AMDavid
01/07/2025, 6:54 AMonly makes sense when using Java interopI don't think this is true at all. As @agrosner, it is for what the keyword says is "late init" when a variable is not suppose to be nullable but will be initialised at a later stage, e.g because of android's lifecycle.
Dimkinware
01/07/2025, 4:22 PMlateinit
easily leads to exceptions and crashes, which is why better to avoid itagrosner
01/07/2025, 4:24 PMDavid
01/07/2025, 7:42 PM!!
everywhere.agrosner
01/07/2025, 10:22 PMgildor
01/08/2025, 1:10 PMgildor
01/08/2025, 1:12 PMgildor
01/08/2025, 1:14 PMhafiz
01/09/2025, 4:14 AMNaveen
01/09/2025, 9:11 AMTest
with 3 variables in it a,b and c. If I have 2 objects of Test class then they can be equal if variable a and b of both objects are equal but variable c can have any value. To achieve this I went with this option.
I made a and b variable as constructor parameter but made c variable as lateinit var.
I know I can override equals and hascode method to achieve this but is there any other way?David
01/09/2025, 9:44 AMvar
or lateinit
in a data class
. A data class
should, as the name infer, contain data. Imo it should only contain val
and possibly member functions to derive some extra data out from those val
's.
Since the context of your problem is not clear there is no one solution, it could be many options depending on the case:
• Remove c
from Test
if it is not data/state.
• Consider restructuring Test
in to a sealed structure if c
only exists in some cases.
• Implement another function instead of using equals and hashcode.gildor
01/10/2025, 3:25 AMNaveen
01/10/2025, 9:26 AMc
will always be present and is required in all objects created.David
01/10/2025, 12:30 PMvar
& lateinit
in any data class
though. Then you still can have a custom compare method or Comparator
to compare the two classes using only some of the properties.David
01/10/2025, 12:30 PM