https://kotlinlang.org logo
Title
a

aerb

05/11/2019, 12:32 PM
Hello 👋, Is there a way to limit access of a block of code to only within a scope? For example, could I somehow have a lambda where assignments outside of scope are not allowed?
k

karelpeeters

05/11/2019, 12:34 PM
You want something like
fun foo(block: (Int) -> Int)
where
block
isn't allowed to access stuff from the scope where it was defined? I don't think that's possible.
a

aerb

05/11/2019, 12:36 PM
Yes!
I think this is loosely similar to @DslMarker
Could be a very useful feature for limiting side effects
Worth bringing up in #language-proposals ?
k

karelpeeters

05/11/2019, 12:43 PM
Maybe, although it's difficult to determine what a side effect is. You wouldn't be able to call any functions at all, or use any libraries.
a

aerb

05/11/2019, 12:44 PM
I'm not really interested in a pure FP side effect free guarantee. More a simple check where if you assign a variable outside of the lambda scope you'll get a compiler error.
p

Pavlo Liapota

05/11/2019, 2:02 PM
I guess you want to force lambda to be non-capturing, right?
a

aerb

05/11/2019, 5:03 PM
Not necessarily non capturing. But you are not allowed to assign outside of your scope. I think if it is being executed on a different thread this could be a useful protective feature.