fun eval(expr: Expr): Int = when (expr) { is Num -> expr.value is Sum -> expr.left + expr.right } interface Expr class Num(val value: Int) : Expr class Sum(val left: Expr, val right: Expr) : Expr
I would like to ideally use value, left, and right without having to call expr everytime. Is that possible?
value
left
right
expr
That's what the with scope function is for.
with
fun eval(expr: Expr): Int = with (expr) { when (this) { is Num -> value is Sum -> left + right } }
1.4m articles
1.4m replys
5 comments
56.9k users