Probably only incidental, if lateinit did not exis...
# announcements
a
Probably only incidental, if lateinit did not exist, the culprit would be var some: Something?. If you need a deferred val, use by lazy {}. Problem is often with injection i guess. Which is where the antagonizing of lateinit comes from i believe.
m
@Anders Mikkelsen Please prefer threads when replying. Most DI now support constructor injection, so you don't need to use lateinit. Maybe one should ask the DI creator why it needs to set a property, rather than using constructor injection. Hopefully there aren't many cases that you really need lateinit. @Ray Eldath to bring into thread.
a
Quite right
👍 1
And in that case you can choose whether it should be a property or not. e.g: (some: Something) (private val some: Something)
In some cases it might not actully be viable tho. Like Spring-boot tests and @Autowired as a field, rather than the constructor due to lack of context
m
For Spring Boot, if you're using JUnit5 Jupiter, you can inject into your class constructors/methods now. If you're still using JUnit 4, then seriously consider upgrading.
a
For sure, im not recommending using Junit 4, or lateinit var for that matter. I was just responding to the fact that lateinit var itself probably isnt the problem the OP was originally adressing. ie. proposing to remove it wouldnt change anything.
m
Ahh, ok. Mis-read your previous comment. So we're on the same page. lateinit should very rarely be required, and if it is, then likely something else should be changed so it can be avoided.
r
Wow. so illuminating. thanks a lot!😄
💯 2
a
@Mike Absolutely so 🙂