Fleshgrinder
09/26/2020, 11:58 AMMarc Knaup
09/30/2020, 8:48 PMelizarov
10/01/2020, 11:57 AMfun f(x: T) {
var x = x // works
}
Marc Knaup
10/01/2020, 12:04 PMelizarov
10/01/2020, 1:33 PMelizarov
10/01/2020, 1:35 PMvar x
from an outer, class scope, for example, and copy it to your own scope with val x = x
. Does not work in the same scope in Kotlin. Would be interesting to see some more worked-out examples from the real code where it would help.Fleshgrinder
10/02/2020, 6:54 PMvar
to val
or val
to var
is all the same as long as it is local.
Nice, didn't know that it's possible in different scopes.
Does now include 1.4.10?Nir
10/03/2020, 2:32 AMNir
10/03/2020, 2:32 AMNir
10/03/2020, 2:33 AMNir
10/03/2020, 2:43 AMval x: List = run {
var x = mutableList<Int>()
...
x
}
Fleshgrinder
10/03/2020, 8:01 AMbuildList
and friends but they're still experimental because the builder inference is still experimental and has many edge cases where it doesn't work. I currently have no good example and don't remember the piece of code that I was porting from Rust. But I'm pretty sure it involved some loop that contained breaks and/or continues.Fleshgrinder
10/03/2020, 8:02 AMNico
10/03/2020, 10:34 AMvar
to val
is just a side effect of allowing shadowing within the same scopeNico
10/03/2020, 10:41 AMFleshgrinder
10/03/2020, 7:34 PMNir
10/03/2020, 8:42 PMNir
10/03/2020, 8:42 PMFleshgrinder
10/04/2020, 9:16 AMbuildList
(and friends) over mutableListOf<Int>().also
(and friends) is that you do not need to specify the types but rather that they are inferred for you. This makes you faster. 🙂Fleshgrinder
10/04/2020, 9:18 AMcapacity
advantage. Using buildMap(42) {}
will set the capacity
to a value that is actually suitable for what you want: it reserves memory for at least 42
elements and will not reallocate anything if you add up to 42
elements. This is something you could only achieve with HashMap(calcActualMapCapacity(42))
(the calcActualMapCapacity
would need to be defined by you) and it's a stupid nuance of the Java map implementation that capacity is not what users expect. We abstracted that away so that users don't need to worry about it. 🙂Marc Knaup
10/04/2020, 12:32 PMvar
is captured by a changing closure smart cast is broken. I’d like to re-assign the var
to a val
of the same name and have type safety from that moment on.
var x: String? = null
somethingWithClosure { … }
val x = checkNotNull(x)
…
Fleshgrinder
10/04/2020, 12:48 PMval x = somethingWithClosure {}
?Marc Knaup
10/04/2020, 12:49 PMMarc Knaup
10/04/2020, 12:49 PMFleshgrinder
10/04/2020, 12:50 PMval a: Int
val b: String
run {
a = 0
b = ""
}
println("$a: $b") // `0: `
Marc Knaup
10/04/2020, 12:51 PMNico
10/04/2020, 12:54 PMFleshgrinder
10/04/2020, 12:59 PM