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
Mark
10/31/2018, 5:50 AMit
to avoid the recast?gildor
10/31/2018, 5:51 AMgildor
10/31/2018, 5:51 AMit
cannot be used with when
gildor
10/31/2018, 5:51 AMMark
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 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 AMMark
10/31/2018, 5:56 AMwhen(val x = x)
seems to do the trickMark
10/31/2018, 5:56 AMwhen(x)
does not allow smartcaststevecstian
10/31/2018, 5:57 AMgildor
10/31/2018, 5:57 AMgildor
10/31/2018, 5:57 AMgildor
10/31/2018, 5:59 AMwhen
, smartcast also works
https://pl.kotl.in/HyqoVpU3XMark
10/31/2018, 6:05 AMMark
10/31/2018, 6:05 AMMark
10/31/2018, 6:08 AMgildor
10/31/2018, 6:09 AMlet
in this example?gildor
10/31/2018, 6:10 AMvar
gildor
10/31/2018, 6:10 AMMark
10/31/2018, 6:10 AMgildor
10/31/2018, 6:11 AMlet
not clear for me and probably can be avoidedgildor
10/31/2018, 6:11 AMvar
Mark
10/31/2018, 6:11 AMAny().let
is just to show the impact of let
but you can imagine something useful in thereMark
10/31/2018, 6:12 AMvar
is necessary because it’s reassigned laterMark
10/31/2018, 6:13 AMgildor
10/31/2018, 6:13 AMgildor
10/31/2018, 6:13 AMgildor
10/31/2018, 6:14 AMgildor
10/31/2018, 6:14 AMlet
uses contractsgildor
10/31/2018, 6:14 AMMark
10/31/2018, 6:15 AMval x = x
before the let
?Mark
10/31/2018, 6:16 AMgildor
10/31/2018, 6:16 AMgildor
10/31/2018, 6:16 AMval x = x
also finegildor
10/31/2018, 6:16 AMMark
10/31/2018, 6:18 AMwhen
I quite like when(val x = x)
Mark
10/31/2018, 6:18 AMlet
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 AMstevecstian
10/31/2018, 6:46 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