Mark
10/31/2018, 4:43 AMvar x = ...
when (x) is X ->
// cannot use x as X unless use x as X
Is there any other way apart from make val x
?gildor
10/31/2018, 5:11 AMMark
10/31/2018, 5:14 AMval
into when
, then smartcasting is available. Otherwise it is not and so you have to do x as X
. It’s not an unreasonably workaround, but just curious if there is a better way.gildor
10/31/2018, 5:17 AMwhen
Mark
10/31/2018, 5:45 AMvar
. True that this issue is not specific to when
but workaround/solution may be specific to when
it
to avoid the recast?gildor
10/31/2018, 5:51 AMit
cannot be used with when
Mark
10/31/2018, 5:52 AMgildor
10/31/2018, 5:52 AMwhen (val x = doTypeCheckOrCast()) {
x // here is val and type safe
}
Mark
10/31/2018, 5:52 AMgildor
10/31/2018, 5:53 AMvar
if it’s local variable and cannot be changed by some other part of your codestevecstian
10/31/2018, 5:54 AMx
an open property or has custom getter?gildor
10/31/2018, 5:55 AMMark
10/31/2018, 5:55 AMvar =
gildor
10/31/2018, 5:55 AMvar
Mark
10/31/2018, 5:56 AMwhen(val x = x)
seems to do the trickwhen(x)
does not allow smartcaststevecstian
10/31/2018, 5:57 AMgildor
10/31/2018, 5:57 AMMark
10/31/2018, 6:05 AMgildor
10/31/2018, 6:09 AMlet
in this example?var
Mark
10/31/2018, 6:10 AMgildor
10/31/2018, 6:11 AMlet
not clear for me and probably can be avoidedvar
Mark
10/31/2018, 6:11 AMAny().let
is just to show the impact of let
but you can imagine something useful in therevar
is necessary because it’s reassigned latergildor
10/31/2018, 6:13 AMlet
uses contractsMark
10/31/2018, 6:15 AMval x = x
before the let
?gildor
10/31/2018, 6:16 AMval x = x
also fineMark
10/31/2018, 6:18 AMwhen
I quite like when(val x = x)
let
because that is what is causing the issuestevecstian
10/31/2018, 6:31 AMMark
10/31/2018, 6:40 AMstevecstian
10/31/2018, 6:42 AMgildor
10/31/2018, 6:48 AMdsavvinov
10/31/2018, 8:16 AMActually in think KT-7186 should be extended to contracts, because in case of let there is also contract that already used by compiler, but not in this caseYes, we have it on the table. It requires a bit more work than it may appear, though.
gildor
10/31/2018, 8:16 AM