How to refer to outer this on forEach in Kotlin












1















I have the following case



someThing.forEach{
someWidget.setOnClickListener{
//it is an View
//I need foreach it of someObject
}
}


I read this answer but it does not work




kotlin how to refer outer-scope this in multi-layer apply functions











share|improve this question



























    1















    I have the following case



    someThing.forEach{
    someWidget.setOnClickListener{
    //it is an View
    //I need foreach it of someObject
    }
    }


    I read this answer but it does not work




    kotlin how to refer outer-scope this in multi-layer apply functions











    share|improve this question

























      1












      1








      1








      I have the following case



      someThing.forEach{
      someWidget.setOnClickListener{
      //it is an View
      //I need foreach it of someObject
      }
      }


      I read this answer but it does not work




      kotlin how to refer outer-scope this in multi-layer apply functions











      share|improve this question














      I have the following case



      someThing.forEach{
      someWidget.setOnClickListener{
      //it is an View
      //I need foreach it of someObject
      }
      }


      I read this answer but it does not work




      kotlin how to refer outer-scope this in multi-layer apply functions








      android lambda kotlin






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 16:33









      Erick Daniel Juarez GilErick Daniel Juarez Gil

      104




      104
























          3 Answers
          3






          active

          oldest

          votes


















          2














          The problem is that you are not dealing with this here.



          forEach has a parameter and for simplicity you can leave it away and just use it instead. Not using it is the same as using _ -> instead... you just discard it.



          So your example written with named lambda parameters instead:



          someThing.forEach{ some -> // 'it' was available here too, but will not be accessible from within the next setOnClickListener...
          someWidget.setOnClickListener{
          // some contains one of the someThings now and 'it' is still your View
          }
          }





          share|improve this answer































            0














            You can name the variable in the forEach.



            things.forEach { thing ->
            someWidget.setOnClickListener {
            thing.doSomething()
            }
            }





            share|improve this answer































              0














              I think you mean something like this:



              someThing.forEach{ x->
              someWidget.setOnClickListener{
              //use x
              //I need foreach it of someObject
              }
              }


              just use another name like x, you don't have to use it.

              Here is an example:



              val a = mutableListOf<Int>(1, 3)
              val b = mutableListOf<Int>(2, 4)

              a.forEach { x ->
              b.forEach {
              println("" + x + " " + it)
              }
              }


              here x is each item from list a

              and it is each item from list b






              share|improve this answer

























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285525%2fhow-to-refer-to-outer-this-on-foreach-in-kotlin%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                2














                The problem is that you are not dealing with this here.



                forEach has a parameter and for simplicity you can leave it away and just use it instead. Not using it is the same as using _ -> instead... you just discard it.



                So your example written with named lambda parameters instead:



                someThing.forEach{ some -> // 'it' was available here too, but will not be accessible from within the next setOnClickListener...
                someWidget.setOnClickListener{
                // some contains one of the someThings now and 'it' is still your View
                }
                }





                share|improve this answer




























                  2














                  The problem is that you are not dealing with this here.



                  forEach has a parameter and for simplicity you can leave it away and just use it instead. Not using it is the same as using _ -> instead... you just discard it.



                  So your example written with named lambda parameters instead:



                  someThing.forEach{ some -> // 'it' was available here too, but will not be accessible from within the next setOnClickListener...
                  someWidget.setOnClickListener{
                  // some contains one of the someThings now and 'it' is still your View
                  }
                  }





                  share|improve this answer


























                    2












                    2








                    2







                    The problem is that you are not dealing with this here.



                    forEach has a parameter and for simplicity you can leave it away and just use it instead. Not using it is the same as using _ -> instead... you just discard it.



                    So your example written with named lambda parameters instead:



                    someThing.forEach{ some -> // 'it' was available here too, but will not be accessible from within the next setOnClickListener...
                    someWidget.setOnClickListener{
                    // some contains one of the someThings now and 'it' is still your View
                    }
                    }





                    share|improve this answer













                    The problem is that you are not dealing with this here.



                    forEach has a parameter and for simplicity you can leave it away and just use it instead. Not using it is the same as using _ -> instead... you just discard it.



                    So your example written with named lambda parameters instead:



                    someThing.forEach{ some -> // 'it' was available here too, but will not be accessible from within the next setOnClickListener...
                    someWidget.setOnClickListener{
                    // some contains one of the someThings now and 'it' is still your View
                    }
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 13 '18 at 16:44









                    RolandRoland

                    9,61911141




                    9,61911141

























                        0














                        You can name the variable in the forEach.



                        things.forEach { thing ->
                        someWidget.setOnClickListener {
                        thing.doSomething()
                        }
                        }





                        share|improve this answer




























                          0














                          You can name the variable in the forEach.



                          things.forEach { thing ->
                          someWidget.setOnClickListener {
                          thing.doSomething()
                          }
                          }





                          share|improve this answer


























                            0












                            0








                            0







                            You can name the variable in the forEach.



                            things.forEach { thing ->
                            someWidget.setOnClickListener {
                            thing.doSomething()
                            }
                            }





                            share|improve this answer













                            You can name the variable in the forEach.



                            things.forEach { thing ->
                            someWidget.setOnClickListener {
                            thing.doSomething()
                            }
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 13 '18 at 16:42









                            CristanCristan

                            2,9432630




                            2,9432630























                                0














                                I think you mean something like this:



                                someThing.forEach{ x->
                                someWidget.setOnClickListener{
                                //use x
                                //I need foreach it of someObject
                                }
                                }


                                just use another name like x, you don't have to use it.

                                Here is an example:



                                val a = mutableListOf<Int>(1, 3)
                                val b = mutableListOf<Int>(2, 4)

                                a.forEach { x ->
                                b.forEach {
                                println("" + x + " " + it)
                                }
                                }


                                here x is each item from list a

                                and it is each item from list b






                                share|improve this answer






























                                  0














                                  I think you mean something like this:



                                  someThing.forEach{ x->
                                  someWidget.setOnClickListener{
                                  //use x
                                  //I need foreach it of someObject
                                  }
                                  }


                                  just use another name like x, you don't have to use it.

                                  Here is an example:



                                  val a = mutableListOf<Int>(1, 3)
                                  val b = mutableListOf<Int>(2, 4)

                                  a.forEach { x ->
                                  b.forEach {
                                  println("" + x + " " + it)
                                  }
                                  }


                                  here x is each item from list a

                                  and it is each item from list b






                                  share|improve this answer




























                                    0












                                    0








                                    0







                                    I think you mean something like this:



                                    someThing.forEach{ x->
                                    someWidget.setOnClickListener{
                                    //use x
                                    //I need foreach it of someObject
                                    }
                                    }


                                    just use another name like x, you don't have to use it.

                                    Here is an example:



                                    val a = mutableListOf<Int>(1, 3)
                                    val b = mutableListOf<Int>(2, 4)

                                    a.forEach { x ->
                                    b.forEach {
                                    println("" + x + " " + it)
                                    }
                                    }


                                    here x is each item from list a

                                    and it is each item from list b






                                    share|improve this answer















                                    I think you mean something like this:



                                    someThing.forEach{ x->
                                    someWidget.setOnClickListener{
                                    //use x
                                    //I need foreach it of someObject
                                    }
                                    }


                                    just use another name like x, you don't have to use it.

                                    Here is an example:



                                    val a = mutableListOf<Int>(1, 3)
                                    val b = mutableListOf<Int>(2, 4)

                                    a.forEach { x ->
                                    b.forEach {
                                    println("" + x + " " + it)
                                    }
                                    }


                                    here x is each item from list a

                                    and it is each item from list b







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 13 '18 at 16:48

























                                    answered Nov 13 '18 at 16:43









                                    forpasforpas

                                    10.4k1421




                                    10.4k1421






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285525%2fhow-to-refer-to-outer-this-on-foreach-in-kotlin%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        List item for chat from Array inside array React Native

                                        Thiostrepton

                                        Caerphilly