Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I think there is a distinction.

A nested procedure ("function" is a bit misleading in imperative languages) can close over variables in the outer scope, but has to have its own isolated control.

A truly[2] anonymous procedure can also close over control flow:

    fun hasZeros(ints: List<Int>): Boolean {
      ints.forEach {
        if (it == 0) return true // returns from hasZeros
      }
      return false
    }
That's an example from Kotlin's docs. [1]

[1]: https://kotlinlang.org/docs/reference/inline-functions.html

[2]: "truly" in the sense it's never assigned to a name, and is known to not escape its surrounding scope, which is necessary if the compiler is going to allow it to, e.g. do a non-local return.



I think I'm glad C++ doesn't have that feature.

Can you provide a ... slightly more real world example of what non-local returns are for? As described it sounds equally capable of shooting both varmints and one's own feet.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: