Kotlin Lambda Syntax Explanation
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
add a comment |
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
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
add a comment |
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
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
lambda kotlin
edited Nov 23 '18 at 6:41
Saikrishna Rajaraman
1,298318
1,298318
asked Nov 23 '18 at 6:19
januprasadjanuprasad
1,5141025
1,5141025
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 23 '18 at 6:57
s1m0nw1s1m0nw1
26.7k646104
26.7k646104
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 23 '18 at 6:47
DeepanDeepan
364
364
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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