Lambda argument should be moved out of parentheses












1














IntelliJ gives the following complaint:




Lambda argument should be moved out of parentheses




val profile = loadProfiles()
profile.sortedWith(Comparator({ profile1, profile2 ->
if (profile1.age > profile2.age) return@Comparator 1
if (profile1.age < profile2.age) return@Comparator -1
return@Comparator 0
}))

data class Developer(var age: Int)

fun loadProfiles(): List<Developer> {
val listOfNumber = listOf<Developer>(Developer(2), Developer(5), Developer(3))

return listOfNumber
}


How should I format the above to get rid of the complaint? Also, the sorting code doesn't sort. What is causing the problem?










share|improve this question


















  • 1




    you may also be interested in profile.sortedBy { it.age } instead... Note also that it is not sorting the underlying list, but rather returning a new sorted list.
    – Roland
    Nov 19 '18 at 13:22












  • ... and I meant: Alt + Enter or click on the light bulb to let Intellij solve that issue for you...
    – Roland
    Nov 19 '18 at 13:25
















1














IntelliJ gives the following complaint:




Lambda argument should be moved out of parentheses




val profile = loadProfiles()
profile.sortedWith(Comparator({ profile1, profile2 ->
if (profile1.age > profile2.age) return@Comparator 1
if (profile1.age < profile2.age) return@Comparator -1
return@Comparator 0
}))

data class Developer(var age: Int)

fun loadProfiles(): List<Developer> {
val listOfNumber = listOf<Developer>(Developer(2), Developer(5), Developer(3))

return listOfNumber
}


How should I format the above to get rid of the complaint? Also, the sorting code doesn't sort. What is causing the problem?










share|improve this question


















  • 1




    you may also be interested in profile.sortedBy { it.age } instead... Note also that it is not sorting the underlying list, but rather returning a new sorted list.
    – Roland
    Nov 19 '18 at 13:22












  • ... and I meant: Alt + Enter or click on the light bulb to let Intellij solve that issue for you...
    – Roland
    Nov 19 '18 at 13:25














1












1








1







IntelliJ gives the following complaint:




Lambda argument should be moved out of parentheses




val profile = loadProfiles()
profile.sortedWith(Comparator({ profile1, profile2 ->
if (profile1.age > profile2.age) return@Comparator 1
if (profile1.age < profile2.age) return@Comparator -1
return@Comparator 0
}))

data class Developer(var age: Int)

fun loadProfiles(): List<Developer> {
val listOfNumber = listOf<Developer>(Developer(2), Developer(5), Developer(3))

return listOfNumber
}


How should I format the above to get rid of the complaint? Also, the sorting code doesn't sort. What is causing the problem?










share|improve this question













IntelliJ gives the following complaint:




Lambda argument should be moved out of parentheses




val profile = loadProfiles()
profile.sortedWith(Comparator({ profile1, profile2 ->
if (profile1.age > profile2.age) return@Comparator 1
if (profile1.age < profile2.age) return@Comparator -1
return@Comparator 0
}))

data class Developer(var age: Int)

fun loadProfiles(): List<Developer> {
val listOfNumber = listOf<Developer>(Developer(2), Developer(5), Developer(3))

return listOfNumber
}


How should I format the above to get rid of the complaint? Also, the sorting code doesn't sort. What is causing the problem?







kotlin






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 13:06









AndroidDev

9,9992391166




9,9992391166








  • 1




    you may also be interested in profile.sortedBy { it.age } instead... Note also that it is not sorting the underlying list, but rather returning a new sorted list.
    – Roland
    Nov 19 '18 at 13:22












  • ... and I meant: Alt + Enter or click on the light bulb to let Intellij solve that issue for you...
    – Roland
    Nov 19 '18 at 13:25














  • 1




    you may also be interested in profile.sortedBy { it.age } instead... Note also that it is not sorting the underlying list, but rather returning a new sorted list.
    – Roland
    Nov 19 '18 at 13:22












  • ... and I meant: Alt + Enter or click on the light bulb to let Intellij solve that issue for you...
    – Roland
    Nov 19 '18 at 13:25








1




1




you may also be interested in profile.sortedBy { it.age } instead... Note also that it is not sorting the underlying list, but rather returning a new sorted list.
– Roland
Nov 19 '18 at 13:22






you may also be interested in profile.sortedBy { it.age } instead... Note also that it is not sorting the underlying list, but rather returning a new sorted list.
– Roland
Nov 19 '18 at 13:22














... and I meant: Alt + Enter or click on the light bulb to let Intellij solve that issue for you...
– Roland
Nov 19 '18 at 13:25




... and I meant: Alt + Enter or click on the light bulb to let Intellij solve that issue for you...
– Roland
Nov 19 '18 at 13:25












3 Answers
3






active

oldest

votes


















1















sortedWith(): Returns a list of all elements sorted according to the
specified [comparator]




So to sort profile list you have to assign the list returned by sortedWith() to profile (also change its declaration from val to var)



var profile = loadProfiles()
profile = profile.sortedWith(Comparator { profile1, profile2 ->
if (profile1.age > profile2.age) return@Comparator 1
if (profile1.age < profile2.age) return@Comparator -1
return@Comparator 0
})

profile.forEach { println(it.age) }


or



val profile = loadProfiles().sortedWith(Comparator { profile1, profile2 ->
if (profile1.age > profile2.age) return@Comparator 1
if (profile1.age < profile2.age) return@Comparator -1
return@Comparator 0
})


For the warning: press Alt+Enter and let InteliJ make the change.






share|improve this answer































    3














    This warning is caused because in Kotlin labda parameters can (and actually should be) outside parentheses.



    See this:



    fun onClick(action: () -> Unit) { ... }


    When you use function like this you can use:



    view.onClick({ toast(it.toString())} )
    view.onClick() { toast(it.toString()) }
    view.onClick { toast(it.toString()) }


    All of those forms are correct (compiler will not fail), but in Kotlin Style Guide you'll find following statement:




    If a call takes a single lambda, it should be passed outside of
    parentheses whenever possible.




    @see https://kotlinlang.org/docs/reference/coding-conventions.html#lambda-formatting



    That's why IntellJ shows warning. You can click alt + enter and IntellJ should show correct solution, or just move lambda out of parentheses. And if labda is only argument remove parentheses also.
    When lambda have to be in parentheses? Only when it is not last parameter in function.






    share|improve this answer





























      1














      As for your immediate problem, you just have to write it like this:



      profile.sortedWith(Comparator { profile1, profile2 ->
      if (profile1.age > profile2.age) return@Comparator 1
      if (profile1.age < profile2.age) return@Comparator -1
      return@Comparator 0
      }
      )


      However, the code still has several layers of unneeded verbosity. Here are some ways to make it both more concise and more readable.





      1. Remove the return statement:



        profile.sortedWith(Comparator { profile1, profile2 ->
        if (profile1.age > profile2.age) 1
        else if (profile1.age < profile2.age) -1
        else 0
        })



      2. Use when instead of an if-else cascade:



        profile.sortedWith(Comparator { profile1, profile2 ->
        when {
        profile1.age > profile2.age -> 1
        profile1.age < profile2.age -> -1
        else -> 0
        }
        })



      3. use Int.compareTo:



        profile.sortedWith(Comparator { profile1, profile2 ->
        profile1.age.compareTo(profile2.age)
        }



      4. use compareBy:



        profile.sortedWith(compareBy(Profile::age))



      5. Don't use the general sortedWith when all you need is sortedBy:



        profile.sortedBy(Profile::age)







      share|improve this answer





















      • or better (I think) .sortedBy { it.age }
        – forpas
        Nov 19 '18 at 15:06











      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%2f53375316%2flambda-argument-should-be-moved-out-of-parentheses%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









      1















      sortedWith(): Returns a list of all elements sorted according to the
      specified [comparator]




      So to sort profile list you have to assign the list returned by sortedWith() to profile (also change its declaration from val to var)



      var profile = loadProfiles()
      profile = profile.sortedWith(Comparator { profile1, profile2 ->
      if (profile1.age > profile2.age) return@Comparator 1
      if (profile1.age < profile2.age) return@Comparator -1
      return@Comparator 0
      })

      profile.forEach { println(it.age) }


      or



      val profile = loadProfiles().sortedWith(Comparator { profile1, profile2 ->
      if (profile1.age > profile2.age) return@Comparator 1
      if (profile1.age < profile2.age) return@Comparator -1
      return@Comparator 0
      })


      For the warning: press Alt+Enter and let InteliJ make the change.






      share|improve this answer




























        1















        sortedWith(): Returns a list of all elements sorted according to the
        specified [comparator]




        So to sort profile list you have to assign the list returned by sortedWith() to profile (also change its declaration from val to var)



        var profile = loadProfiles()
        profile = profile.sortedWith(Comparator { profile1, profile2 ->
        if (profile1.age > profile2.age) return@Comparator 1
        if (profile1.age < profile2.age) return@Comparator -1
        return@Comparator 0
        })

        profile.forEach { println(it.age) }


        or



        val profile = loadProfiles().sortedWith(Comparator { profile1, profile2 ->
        if (profile1.age > profile2.age) return@Comparator 1
        if (profile1.age < profile2.age) return@Comparator -1
        return@Comparator 0
        })


        For the warning: press Alt+Enter and let InteliJ make the change.






        share|improve this answer


























          1












          1








          1







          sortedWith(): Returns a list of all elements sorted according to the
          specified [comparator]




          So to sort profile list you have to assign the list returned by sortedWith() to profile (also change its declaration from val to var)



          var profile = loadProfiles()
          profile = profile.sortedWith(Comparator { profile1, profile2 ->
          if (profile1.age > profile2.age) return@Comparator 1
          if (profile1.age < profile2.age) return@Comparator -1
          return@Comparator 0
          })

          profile.forEach { println(it.age) }


          or



          val profile = loadProfiles().sortedWith(Comparator { profile1, profile2 ->
          if (profile1.age > profile2.age) return@Comparator 1
          if (profile1.age < profile2.age) return@Comparator -1
          return@Comparator 0
          })


          For the warning: press Alt+Enter and let InteliJ make the change.






          share|improve this answer















          sortedWith(): Returns a list of all elements sorted according to the
          specified [comparator]




          So to sort profile list you have to assign the list returned by sortedWith() to profile (also change its declaration from val to var)



          var profile = loadProfiles()
          profile = profile.sortedWith(Comparator { profile1, profile2 ->
          if (profile1.age > profile2.age) return@Comparator 1
          if (profile1.age < profile2.age) return@Comparator -1
          return@Comparator 0
          })

          profile.forEach { println(it.age) }


          or



          val profile = loadProfiles().sortedWith(Comparator { profile1, profile2 ->
          if (profile1.age > profile2.age) return@Comparator 1
          if (profile1.age < profile2.age) return@Comparator -1
          return@Comparator 0
          })


          For the warning: press Alt+Enter and let InteliJ make the change.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 '18 at 13:26

























          answered Nov 19 '18 at 13:20









          forpas

          8,6141421




          8,6141421

























              3














              This warning is caused because in Kotlin labda parameters can (and actually should be) outside parentheses.



              See this:



              fun onClick(action: () -> Unit) { ... }


              When you use function like this you can use:



              view.onClick({ toast(it.toString())} )
              view.onClick() { toast(it.toString()) }
              view.onClick { toast(it.toString()) }


              All of those forms are correct (compiler will not fail), but in Kotlin Style Guide you'll find following statement:




              If a call takes a single lambda, it should be passed outside of
              parentheses whenever possible.




              @see https://kotlinlang.org/docs/reference/coding-conventions.html#lambda-formatting



              That's why IntellJ shows warning. You can click alt + enter and IntellJ should show correct solution, or just move lambda out of parentheses. And if labda is only argument remove parentheses also.
              When lambda have to be in parentheses? Only when it is not last parameter in function.






              share|improve this answer


























                3














                This warning is caused because in Kotlin labda parameters can (and actually should be) outside parentheses.



                See this:



                fun onClick(action: () -> Unit) { ... }


                When you use function like this you can use:



                view.onClick({ toast(it.toString())} )
                view.onClick() { toast(it.toString()) }
                view.onClick { toast(it.toString()) }


                All of those forms are correct (compiler will not fail), but in Kotlin Style Guide you'll find following statement:




                If a call takes a single lambda, it should be passed outside of
                parentheses whenever possible.




                @see https://kotlinlang.org/docs/reference/coding-conventions.html#lambda-formatting



                That's why IntellJ shows warning. You can click alt + enter and IntellJ should show correct solution, or just move lambda out of parentheses. And if labda is only argument remove parentheses also.
                When lambda have to be in parentheses? Only when it is not last parameter in function.






                share|improve this answer
























                  3












                  3








                  3






                  This warning is caused because in Kotlin labda parameters can (and actually should be) outside parentheses.



                  See this:



                  fun onClick(action: () -> Unit) { ... }


                  When you use function like this you can use:



                  view.onClick({ toast(it.toString())} )
                  view.onClick() { toast(it.toString()) }
                  view.onClick { toast(it.toString()) }


                  All of those forms are correct (compiler will not fail), but in Kotlin Style Guide you'll find following statement:




                  If a call takes a single lambda, it should be passed outside of
                  parentheses whenever possible.




                  @see https://kotlinlang.org/docs/reference/coding-conventions.html#lambda-formatting



                  That's why IntellJ shows warning. You can click alt + enter and IntellJ should show correct solution, or just move lambda out of parentheses. And if labda is only argument remove parentheses also.
                  When lambda have to be in parentheses? Only when it is not last parameter in function.






                  share|improve this answer












                  This warning is caused because in Kotlin labda parameters can (and actually should be) outside parentheses.



                  See this:



                  fun onClick(action: () -> Unit) { ... }


                  When you use function like this you can use:



                  view.onClick({ toast(it.toString())} )
                  view.onClick() { toast(it.toString()) }
                  view.onClick { toast(it.toString()) }


                  All of those forms are correct (compiler will not fail), but in Kotlin Style Guide you'll find following statement:




                  If a call takes a single lambda, it should be passed outside of
                  parentheses whenever possible.




                  @see https://kotlinlang.org/docs/reference/coding-conventions.html#lambda-formatting



                  That's why IntellJ shows warning. You can click alt + enter and IntellJ should show correct solution, or just move lambda out of parentheses. And if labda is only argument remove parentheses also.
                  When lambda have to be in parentheses? Only when it is not last parameter in function.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 '18 at 14:00









                  Cililing

                  415110




                  415110























                      1














                      As for your immediate problem, you just have to write it like this:



                      profile.sortedWith(Comparator { profile1, profile2 ->
                      if (profile1.age > profile2.age) return@Comparator 1
                      if (profile1.age < profile2.age) return@Comparator -1
                      return@Comparator 0
                      }
                      )


                      However, the code still has several layers of unneeded verbosity. Here are some ways to make it both more concise and more readable.





                      1. Remove the return statement:



                        profile.sortedWith(Comparator { profile1, profile2 ->
                        if (profile1.age > profile2.age) 1
                        else if (profile1.age < profile2.age) -1
                        else 0
                        })



                      2. Use when instead of an if-else cascade:



                        profile.sortedWith(Comparator { profile1, profile2 ->
                        when {
                        profile1.age > profile2.age -> 1
                        profile1.age < profile2.age -> -1
                        else -> 0
                        }
                        })



                      3. use Int.compareTo:



                        profile.sortedWith(Comparator { profile1, profile2 ->
                        profile1.age.compareTo(profile2.age)
                        }



                      4. use compareBy:



                        profile.sortedWith(compareBy(Profile::age))



                      5. Don't use the general sortedWith when all you need is sortedBy:



                        profile.sortedBy(Profile::age)







                      share|improve this answer





















                      • or better (I think) .sortedBy { it.age }
                        – forpas
                        Nov 19 '18 at 15:06
















                      1














                      As for your immediate problem, you just have to write it like this:



                      profile.sortedWith(Comparator { profile1, profile2 ->
                      if (profile1.age > profile2.age) return@Comparator 1
                      if (profile1.age < profile2.age) return@Comparator -1
                      return@Comparator 0
                      }
                      )


                      However, the code still has several layers of unneeded verbosity. Here are some ways to make it both more concise and more readable.





                      1. Remove the return statement:



                        profile.sortedWith(Comparator { profile1, profile2 ->
                        if (profile1.age > profile2.age) 1
                        else if (profile1.age < profile2.age) -1
                        else 0
                        })



                      2. Use when instead of an if-else cascade:



                        profile.sortedWith(Comparator { profile1, profile2 ->
                        when {
                        profile1.age > profile2.age -> 1
                        profile1.age < profile2.age -> -1
                        else -> 0
                        }
                        })



                      3. use Int.compareTo:



                        profile.sortedWith(Comparator { profile1, profile2 ->
                        profile1.age.compareTo(profile2.age)
                        }



                      4. use compareBy:



                        profile.sortedWith(compareBy(Profile::age))



                      5. Don't use the general sortedWith when all you need is sortedBy:



                        profile.sortedBy(Profile::age)







                      share|improve this answer





















                      • or better (I think) .sortedBy { it.age }
                        – forpas
                        Nov 19 '18 at 15:06














                      1












                      1








                      1






                      As for your immediate problem, you just have to write it like this:



                      profile.sortedWith(Comparator { profile1, profile2 ->
                      if (profile1.age > profile2.age) return@Comparator 1
                      if (profile1.age < profile2.age) return@Comparator -1
                      return@Comparator 0
                      }
                      )


                      However, the code still has several layers of unneeded verbosity. Here are some ways to make it both more concise and more readable.





                      1. Remove the return statement:



                        profile.sortedWith(Comparator { profile1, profile2 ->
                        if (profile1.age > profile2.age) 1
                        else if (profile1.age < profile2.age) -1
                        else 0
                        })



                      2. Use when instead of an if-else cascade:



                        profile.sortedWith(Comparator { profile1, profile2 ->
                        when {
                        profile1.age > profile2.age -> 1
                        profile1.age < profile2.age -> -1
                        else -> 0
                        }
                        })



                      3. use Int.compareTo:



                        profile.sortedWith(Comparator { profile1, profile2 ->
                        profile1.age.compareTo(profile2.age)
                        }



                      4. use compareBy:



                        profile.sortedWith(compareBy(Profile::age))



                      5. Don't use the general sortedWith when all you need is sortedBy:



                        profile.sortedBy(Profile::age)







                      share|improve this answer












                      As for your immediate problem, you just have to write it like this:



                      profile.sortedWith(Comparator { profile1, profile2 ->
                      if (profile1.age > profile2.age) return@Comparator 1
                      if (profile1.age < profile2.age) return@Comparator -1
                      return@Comparator 0
                      }
                      )


                      However, the code still has several layers of unneeded verbosity. Here are some ways to make it both more concise and more readable.





                      1. Remove the return statement:



                        profile.sortedWith(Comparator { profile1, profile2 ->
                        if (profile1.age > profile2.age) 1
                        else if (profile1.age < profile2.age) -1
                        else 0
                        })



                      2. Use when instead of an if-else cascade:



                        profile.sortedWith(Comparator { profile1, profile2 ->
                        when {
                        profile1.age > profile2.age -> 1
                        profile1.age < profile2.age -> -1
                        else -> 0
                        }
                        })



                      3. use Int.compareTo:



                        profile.sortedWith(Comparator { profile1, profile2 ->
                        profile1.age.compareTo(profile2.age)
                        }



                      4. use compareBy:



                        profile.sortedWith(compareBy(Profile::age))



                      5. Don't use the general sortedWith when all you need is sortedBy:



                        profile.sortedBy(Profile::age)








                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 19 '18 at 14:59









                      Marko Topolnik

                      145k19194321




                      145k19194321












                      • or better (I think) .sortedBy { it.age }
                        – forpas
                        Nov 19 '18 at 15:06


















                      • or better (I think) .sortedBy { it.age }
                        – forpas
                        Nov 19 '18 at 15:06
















                      or better (I think) .sortedBy { it.age }
                      – forpas
                      Nov 19 '18 at 15:06




                      or better (I think) .sortedBy { it.age }
                      – forpas
                      Nov 19 '18 at 15:06


















                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53375316%2flambda-argument-should-be-moved-out-of-parentheses%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

                      android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                      SQL update select statement

                      'app-layout' is not a known element: how to share Component with different Modules