Robert Jaros
02/24/2020, 10:51 PMa as String
Dominaezzz
02/24/2020, 10:59 PMa as Any
?streetsofboston
02/24/2020, 10:59 PMinline fun <reified T: Any>test1(a: T?): Unit {
if (a!=null) {
test2(a)
}
}
Dominaezzz
02/24/2020, 11:00 PMstreetsofboston
02/24/2020, 11:01 PMT
is bound to Any?
, the type-inference can’t cast to to an upper bound to Any
after a null-check…. It requires a bit of gymnastics…..Dominaezzz
02/24/2020, 11:02 PMstreetsofboston
02/24/2020, 11:14 PMabstract class State<S, out A> {
/**
* Runs this [State]'s computation.
*
* It returns a (modified) state of type [S] with a result of this computation of type [A].
*/
internal abstract val run: (S) -> Pair<S, A>
}
class StateFx<S> {
private var currentState: S? = null
fun <B> State<S, B>.bind(): B {
val (newState, b) = <mailto:this@bind.run|this@bind.run>(currentState)
currentState = newState
return b
}
}
In the above example, <mailto:this@bind.run|this@bind.run>(currentState)
shows a compiler error for currentState: Required S, found S?
If I change the currentState as follows,
private var currentState: S = null
, then this shows an error: Null cannot be a value of non-null type S
Even though S has upper-bound `Any?`…..<mailto:this@bind.run|this@bind.run>(currentState as S)
)bbaldino
02/24/2020, 11:50 PM<T : Enum<T>>
). couldn't find any way to coerce it...I think even a cast there didn't work, but maybe i'm misrememberingKroppeb
02/25/2020, 9:53 AM