{"id":197,"date":"2023-08-18T21:14:00","date_gmt":"2023-08-18T13:14:00","guid":{"rendered":"https:\/\/blog.bexonbai.com\/?p=197"},"modified":"2026-03-02T21:15:49","modified_gmt":"2026-03-02T13:15:49","slug":"closure-in-kotlin","status":"publish","type":"post","link":"https:\/\/blog.bexonbai.com\/?p=197","title":{"rendered":"Closure in Kotlin"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In Kotlin, a lambda expression or anonymous function (as well as a&nbsp;<a href=\"https:\/\/kotlinlang.org\/docs\/functions.html#local-functions\">local function<\/a>&nbsp;and an&nbsp;<a href=\"https:\/\/kotlinlang.org\/docs\/object-declarations.html#object-expressions\">object expression<\/a>) can access its&nbsp;<strong><em>closure<\/em><\/strong>, which includes the variables declared in the outer scope. The variables captured in the closure can be modified within the lambda.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example from Kotlin\u2019s website:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>var<\/strong> sum = 0\nints.<strong>filter<\/strong> { it &gt; 0 }.<strong>forEach<\/strong> {\n    sum += it\n}\n<strong>print<\/strong>(sum)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Java 8 introduced lambdas, which can access external variables but cannot modify them. This limitation restricts the ability to perform functional programming in Java 8.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This example will result in a compilation error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>List<\/strong><strong>&lt;<\/strong><strong>String<\/strong><strong>&gt;<\/strong> strings <strong>=<\/strong> <strong>Arrays<\/strong><strong>.<\/strong>asList<strong>(<\/strong>\"sup1\"<strong>,<\/strong> \"sup2\"<strong>,<\/strong> \"sup3\"<strong>);<\/strong>\n<strong>List<\/strong><strong>&lt;<\/strong><strong>String<\/strong><strong>&gt;<\/strong> transformers <strong>=<\/strong> <strong>Arrays<\/strong><strong>.<\/strong>asList<strong>(<\/strong>\"trans1\"<strong>,<\/strong> \"trans2\"<strong>,<\/strong> \"trans3\"<strong>);<\/strong>\n\n@Test\n<strong>public<\/strong> <strong>void<\/strong> <strong>demo2<\/strong><strong>()<\/strong> <strong>{<\/strong>\n    strings<strong>.<\/strong>stream<strong>().<\/strong>map<strong>(<\/strong>s <strong>-&gt;<\/strong> transformers<strong>.<\/strong>stream<strong>().<\/strong>forEach<strong>(<\/strong>t <strong>-&gt;<\/strong> <strong>{<\/strong>  <em>\/\/ There will be error here.<\/em>\n        s <strong>=<\/strong> <strong>(<\/strong>t <strong>+<\/strong> \", \" <strong>+<\/strong> s<strong>);<\/strong>\n    <strong>})).<\/strong>forEach<strong>(<\/strong><strong>System<\/strong><strong>.<\/strong>out<strong>::<\/strong>println<strong>);<\/strong>\n<strong>}<\/strong>\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>error: incompatible types: inference variable R has incompatible bounds\n        strings.stream<strong>()<\/strong>.map<strong>(<\/strong>s -&gt; transformers.stream<strong>()<\/strong>.forEach<strong>(<\/strong>t -&gt; <strong>{<\/strong>\n                            ^\n    lower bounds: Object\n    lower bounds: void\n  where R,T are type-variables:\n    R extends Object declared <strong>in <\/strong>method &lt;R&gt;map<strong>(<\/strong>Function&lt;? super T,? extends R&gt;<strong>)<\/strong>\n    T extends Object declared <strong>in <\/strong>interface Stream\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">but not in Kotlin.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>var<\/strong> strings: <strong>List<\/strong>&lt;<strong>String<\/strong>&gt; = <strong>mutableListOf<\/strong>(\"sup1\", \"sup2\", \"sup3\")\n<strong>var<\/strong> transformers: <strong>List<\/strong>&lt;<strong>String<\/strong>&gt; = <strong>mutableListOf<\/strong>(\"trans1\", \"trans2\", \"trans3\")\n\n@Test\n<strong>fun<\/strong> <strong>demo2<\/strong>() {\n    strings.<strong>stream<\/strong>().map&lt;<strong>Any<\/strong>&gt; { s: <strong>String<\/strong> -&gt;\n        <strong>var<\/strong> s = s\n        transformers.<strong>stream<\/strong>().<strong>forEach<\/strong> { t: <strong>String<\/strong> -&gt; s = \"$t, $s\" }\n    }.<strong>forEach<\/strong> { o: <strong>Any<\/strong>? -&gt; <strong>println<\/strong>(o) }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Extended link:<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/note.bexonbai.com\/post\/closure-in-swift\">Closure in Swift<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Kotlin, a lambda expression or anonymous function (as well as a local function and an object expression) can access its closure, which includes the variables declared in the outer scope. The variables captured in the closure can be modified within the lambda.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-197","post","type-post","status-publish","format-standard","hentry","category-develop"],"_links":{"self":[{"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=\/wp\/v2\/posts\/197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=197"}],"version-history":[{"count":1,"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=\/wp\/v2\/posts\/197\/revisions"}],"predecessor-version":[{"id":198,"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=\/wp\/v2\/posts\/197\/revisions\/198"}],"wp:attachment":[{"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bexonbai.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}