gregd
07/24/2018, 1:15 PMlateinit
, but it only works with `var`s and non-primitive types).
What I mean is that ALL properties (val, var, objects, primitives) could be defined without the initial value. And in case of accessing non-initialized property we would get an error, e.g. “variable ‘x’ used before being initialized”kingsley
07/24/2018, 1:25 PMDelegates.notNull
?
By the way, this isn’t really a language proposalbenleggiero
07/24/2018, 2:24 PMgregd
07/24/2018, 2:24 PMkarelpeeters
07/24/2018, 2:25 PMgregd
07/24/2018, 2:25 PMgregd
07/24/2018, 2:27 PMlateinit var
/ var
+ providing dumb default values / not being able to use `val`s in many cases - hell that I encounter right now 😕karelpeeters
07/24/2018, 2:30 PMlateinit
that it is for exceptional circumstances and it screams "unsafe".kingsley
07/24/2018, 2:30 PMlateinit
only works with non-nullable, non-primitive types.
But you didn’t say Delegates.notNull
doesn’t solve your problem.
ALL properties (val, var, objects, primitives) could be defined without the initial value. And in case of accessing non-initialized property we would get an error, e.g. “variable ‘x’ used before being initialized
gregd
07/24/2018, 2:32 PMgregd
07/24/2018, 2:34 PMgregd
07/24/2018, 2:35 PMgregd
07/24/2018, 2:35 PMlet a: String
let b: String?
var c: Bool
var d: Bool?
karelpeeters
07/24/2018, 2:36 PMlateinit
on every property (which I strongly disagree with) and you want it to work on nullable types too.gregd
07/24/2018, 2:37 PMgregd
07/24/2018, 2:38 PMlateinit
and 100% consistent across every possible property - with no exceptionskingsley
07/24/2018, 2:40 PMkarelpeeters
07/24/2018, 2:42 PMgregd
07/24/2018, 2:42 PMlateinit
or var ...? = null
+ !!
all over the place. Also the usage of val
is very limited because of this.karelpeeters
07/24/2018, 2:43 PMimport
views, avoiding this problem. Otherwise it's a good case to use lateinit
on.gregd
07/24/2018, 2:48 PMlateinit
prohibits the use of val
, primitives and custom getters and setters… thus forces us to create various workarounds… again, compared to Swift’s consistent aproachilya.gorbunov
07/24/2018, 2:53 PMgregd
07/24/2018, 2:53 PMval
, but you can’t (becasue it’s not passed in the constructor). So you choose one of the non-perfect alternatives: lateinit var or var …? = null or a delegate…gregd
07/24/2018, 2:57 PMkarelpeeters
07/24/2018, 2:58 PMgregd
07/24/2018, 2:59 PMgildor
07/24/2018, 5:16 PMgildor
07/24/2018, 5:18 PMMy use case is basically Android, where you’re basically forced to use eitherMaybe you could share most common use cases of that. We almost never use !! and lateinit only for dependency injection of Android componentsorlateinit
+var ...? = null
all over the place.!!
Allan Wang
07/24/2018, 5:29 PMlateinit
for things that may depend on the context in Android, and for nullable content, I’d just set the default to null since I need to handle that anyways.
You may also note that within a method, you can do something like val a: Int
and initialize it later, so long as it’s initialized once only and before it is usedbenleggiero
07/25/2018, 3:20 AMlazy
?
Maybe they could be implicitly wrapped in individual transparent classes?
I don't think you need to ditch safety to have lazy loading of top-level propertiesvoddan
07/25/2018, 10:22 AMlouiscad
07/25/2018, 6:27 PMvoddan
07/26/2018, 10:30 AMlouiscad
07/26/2018, 10:35 AMArray<Int>
takes up more memory than IntArray
(since there's an array of references, plus all the boxed `Int`s), and that unboxed to boxed Int
conversion also had an allocation cost, which you don't want in code run at a potentially significant frequency