`it` within `when`


#1

Two seconds suggestion: allow referring to the parameter of a when block via the implicit it variable, just like in lambdas. Or failing that, allowing something like when (val x = ....) { ... }.

One problem I see: in case of nested when, it is ambiguous (but you’re not worse off than you are today).

I’ll also use this topic to piggyback my question from this topic:

How to deal with nested “receivers functions”? e.g.

thing.apply { stuff.apply { /* how to refer to "thing" using "this" here? */ } }

Declarations in `when` statement
#2

You can use labels for nested calls:

thing.apply a@{ 
    stuff.apply b@{  
         this@a.foo()
         this@b.bar()
    } 
}

#3

there is a compelling case for giving when a scoped binding:

when(a.b) {
  is B -> a.b //type inference for B doesnt work here
}

so u have to do:

    val b = a.b
    when(b) {
      is B -> b.foo() //type inference works
    }

which pollutes the surrounding code with another variable.
better seems:

when(val b = a.b) {
  is B -> b.foo()
}