Kotlin Lambda Syntax Explanation





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm a Kotlin beginner, eager to know about the behaviour of the lambda expression for println.unfortunately both functions are doing same job.



    val printFunction1:(String) -> Unit = {
println("Hello, $it!")
}

val printFunction2 = {
user: String ->
println("Hello, $user!")
}


I can call the functions like this, It would be good if someone can explain this.



 printFunction1("Bini")
printFunction2("Jenu")









share|improve this question




















  • 1





    Why do you say "unfortunately" both functions are doing the same job? What exactly did you expect?

    – Jesper
    Nov 23 '18 at 7:35











  • which one to use actually ?

    – januprasad
    Nov 26 '18 at 5:30


















0















I'm a Kotlin beginner, eager to know about the behaviour of the lambda expression for println.unfortunately both functions are doing same job.



    val printFunction1:(String) -> Unit = {
println("Hello, $it!")
}

val printFunction2 = {
user: String ->
println("Hello, $user!")
}


I can call the functions like this, It would be good if someone can explain this.



 printFunction1("Bini")
printFunction2("Jenu")









share|improve this question




















  • 1





    Why do you say "unfortunately" both functions are doing the same job? What exactly did you expect?

    – Jesper
    Nov 23 '18 at 7:35











  • which one to use actually ?

    – januprasad
    Nov 26 '18 at 5:30














0












0








0


1






I'm a Kotlin beginner, eager to know about the behaviour of the lambda expression for println.unfortunately both functions are doing same job.



    val printFunction1:(String) -> Unit = {
println("Hello, $it!")
}

val printFunction2 = {
user: String ->
println("Hello, $user!")
}


I can call the functions like this, It would be good if someone can explain this.



 printFunction1("Bini")
printFunction2("Jenu")









share|improve this question
















I'm a Kotlin beginner, eager to know about the behaviour of the lambda expression for println.unfortunately both functions are doing same job.



    val printFunction1:(String) -> Unit = {
println("Hello, $it!")
}

val printFunction2 = {
user: String ->
println("Hello, $user!")
}


I can call the functions like this, It would be good if someone can explain this.



 printFunction1("Bini")
printFunction2("Jenu")






lambda kotlin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 6:41









Saikrishna Rajaraman

1,383420




1,383420










asked Nov 23 '18 at 6:19









januprasadjanuprasad

1,5191025




1,5191025








  • 1





    Why do you say "unfortunately" both functions are doing the same job? What exactly did you expect?

    – Jesper
    Nov 23 '18 at 7:35











  • which one to use actually ?

    – januprasad
    Nov 26 '18 at 5:30














  • 1





    Why do you say "unfortunately" both functions are doing the same job? What exactly did you expect?

    – Jesper
    Nov 23 '18 at 7:35











  • which one to use actually ?

    – januprasad
    Nov 26 '18 at 5:30








1




1





Why do you say "unfortunately" both functions are doing the same job? What exactly did you expect?

– Jesper
Nov 23 '18 at 7:35





Why do you say "unfortunately" both functions are doing the same job? What exactly did you expect?

– Jesper
Nov 23 '18 at 7:35













which one to use actually ?

– januprasad
Nov 26 '18 at 5:30





which one to use actually ?

– januprasad
Nov 26 '18 at 5:30












2 Answers
2






active

oldest

votes


















1














What would you expect the functions to behave like?



The first one has an explicit function type (String) -> Unit. That way, you don't need to specify the argument type String inside the lambda. You can just use it (implicit name for single arguments of lambdas) as a String.



The second one does not specify a type and you need to tell the compiler what type your lambda parameter has, which you did with user: String ->. Note that it's more idiomatic to move this part to the line with the opening bracket:



val printFunction2 = { user: String ->
println("Hello, $user!")
}


Otherwise I don't see anything fancy going on here. Let me know if you need further clarification.






share|improve this answer































    1














    Lambdas behave exactly like normal functions do in both cases.
    accept input(parameter) as string and the function executes println()
    Normal function:



    fun funName(parameters):ReturnType{FunBody}


    Lambda function tied to variable:



    var varFunName:(ParameterType) ->Unit={FunBody}
    or
    var varFunName = {
    parameters -> {FunBody}
    }


    Note: since there is no parameter name in the first type it automatically maps to it variable/expression
    for more understanding do look at grammar for functionLiterals the page does have grammar for all Kotlin language constructs might need some amount of time to get to understand all grammars are links so if you want to understand that part just follow the link






    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%2f53441500%2fkotlin-lambda-syntax-explanation%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      What would you expect the functions to behave like?



      The first one has an explicit function type (String) -> Unit. That way, you don't need to specify the argument type String inside the lambda. You can just use it (implicit name for single arguments of lambdas) as a String.



      The second one does not specify a type and you need to tell the compiler what type your lambda parameter has, which you did with user: String ->. Note that it's more idiomatic to move this part to the line with the opening bracket:



      val printFunction2 = { user: String ->
      println("Hello, $user!")
      }


      Otherwise I don't see anything fancy going on here. Let me know if you need further clarification.






      share|improve this answer




























        1














        What would you expect the functions to behave like?



        The first one has an explicit function type (String) -> Unit. That way, you don't need to specify the argument type String inside the lambda. You can just use it (implicit name for single arguments of lambdas) as a String.



        The second one does not specify a type and you need to tell the compiler what type your lambda parameter has, which you did with user: String ->. Note that it's more idiomatic to move this part to the line with the opening bracket:



        val printFunction2 = { user: String ->
        println("Hello, $user!")
        }


        Otherwise I don't see anything fancy going on here. Let me know if you need further clarification.






        share|improve this answer


























          1












          1








          1







          What would you expect the functions to behave like?



          The first one has an explicit function type (String) -> Unit. That way, you don't need to specify the argument type String inside the lambda. You can just use it (implicit name for single arguments of lambdas) as a String.



          The second one does not specify a type and you need to tell the compiler what type your lambda parameter has, which you did with user: String ->. Note that it's more idiomatic to move this part to the line with the opening bracket:



          val printFunction2 = { user: String ->
          println("Hello, $user!")
          }


          Otherwise I don't see anything fancy going on here. Let me know if you need further clarification.






          share|improve this answer













          What would you expect the functions to behave like?



          The first one has an explicit function type (String) -> Unit. That way, you don't need to specify the argument type String inside the lambda. You can just use it (implicit name for single arguments of lambdas) as a String.



          The second one does not specify a type and you need to tell the compiler what type your lambda parameter has, which you did with user: String ->. Note that it's more idiomatic to move this part to the line with the opening bracket:



          val printFunction2 = { user: String ->
          println("Hello, $user!")
          }


          Otherwise I don't see anything fancy going on here. Let me know if you need further clarification.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 6:57









          s1m0nw1s1m0nw1

          30.4k656111




          30.4k656111

























              1














              Lambdas behave exactly like normal functions do in both cases.
              accept input(parameter) as string and the function executes println()
              Normal function:



              fun funName(parameters):ReturnType{FunBody}


              Lambda function tied to variable:



              var varFunName:(ParameterType) ->Unit={FunBody}
              or
              var varFunName = {
              parameters -> {FunBody}
              }


              Note: since there is no parameter name in the first type it automatically maps to it variable/expression
              for more understanding do look at grammar for functionLiterals the page does have grammar for all Kotlin language constructs might need some amount of time to get to understand all grammars are links so if you want to understand that part just follow the link






              share|improve this answer




























                1














                Lambdas behave exactly like normal functions do in both cases.
                accept input(parameter) as string and the function executes println()
                Normal function:



                fun funName(parameters):ReturnType{FunBody}


                Lambda function tied to variable:



                var varFunName:(ParameterType) ->Unit={FunBody}
                or
                var varFunName = {
                parameters -> {FunBody}
                }


                Note: since there is no parameter name in the first type it automatically maps to it variable/expression
                for more understanding do look at grammar for functionLiterals the page does have grammar for all Kotlin language constructs might need some amount of time to get to understand all grammars are links so if you want to understand that part just follow the link






                share|improve this answer


























                  1












                  1








                  1







                  Lambdas behave exactly like normal functions do in both cases.
                  accept input(parameter) as string and the function executes println()
                  Normal function:



                  fun funName(parameters):ReturnType{FunBody}


                  Lambda function tied to variable:



                  var varFunName:(ParameterType) ->Unit={FunBody}
                  or
                  var varFunName = {
                  parameters -> {FunBody}
                  }


                  Note: since there is no parameter name in the first type it automatically maps to it variable/expression
                  for more understanding do look at grammar for functionLiterals the page does have grammar for all Kotlin language constructs might need some amount of time to get to understand all grammars are links so if you want to understand that part just follow the link






                  share|improve this answer













                  Lambdas behave exactly like normal functions do in both cases.
                  accept input(parameter) as string and the function executes println()
                  Normal function:



                  fun funName(parameters):ReturnType{FunBody}


                  Lambda function tied to variable:



                  var varFunName:(ParameterType) ->Unit={FunBody}
                  or
                  var varFunName = {
                  parameters -> {FunBody}
                  }


                  Note: since there is no parameter name in the first type it automatically maps to it variable/expression
                  for more understanding do look at grammar for functionLiterals the page does have grammar for all Kotlin language constructs might need some amount of time to get to understand all grammars are links so if you want to understand that part just follow the link







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 6:47









                  DeepanDeepan

                  385




                  385






























                      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%2f53441500%2fkotlin-lambda-syntax-explanation%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

                      mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

                      How to change which sound is reproduced for terminal bell?

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?