christophsturm
11/24/2020, 3:35 PMreturn if (value != null)
(value as Number).toDouble()
else
null
(not (value as? Number).toDouble()
, which acts differently)wbertan
11/24/2020, 3:35 PM(value as? Number)?.toDouble()
christophsturm
11/24/2020, 3:36 PMwbertan
11/24/2020, 3:37 PMreturn value?.let { (it as Number).toDouble() }
Would be something like this?christophsturm
11/24/2020, 3:38 PMchristophsturm
11/24/2020, 3:39 PM(value? as Number)
would be greatwbertan
11/24/2020, 3:39 PMfun asas1(value: Any?): Double? {
return value?.let { (it as Number).toDouble() }
}
fun asas2(value: Any?): Double? {
return if (value != null)
(value as Number).toDouble()
else
null
}
Generates:
@Nullable
public final Double asas1(@Nullable Object value) {
Double var10000;
if (value != null) {
boolean var3 = false;
boolean var4 = false;
int var6 = false;
if (value == null) {
throw new NullPointerException("null cannot be cast to non-null type kotlin.Number");
}
var10000 = ((Number)value).doubleValue();
} else {
var10000 = null;
}
return var10000;
}
@Nullable
public final Double asas2(@Nullable Object value) {
return value != null ? ((Number)value).doubleValue() : null;
}
So I would say to use the if
🙃Joris PZ
11/24/2020, 3:40 PM(value as Number?)?.toDouble()
?christophsturm
11/24/2020, 3:40 PMchristophsturm
11/24/2020, 3:41 PMKroppeb
11/27/2020, 5:49 PM