Feature Request: A way to enforce non-capturing or local-only capturing lambda

Capturing lambda may reference local variables, member fields and functions. This mixture of references sometimes are not wanted by the developer but by careless accident, causing troubles like memory leak or unwanted coupling trouble in future maintenance, especially for project with large team working on it.

Can we have a language feature (probably a lambda modifier?) to enforce a lambda to never capture member reference (thus access member fields and functions)?

A typical use case is AsyncTask in Android, which should never be used with member-capturing lambda inside an Activity or Fragment.

2 Likes