Lambda argument should be moved out of parentheses
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
add a comment |
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
1
you may also be interested inprofile.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
add a comment |
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
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
kotlin
asked Nov 19 '18 at 13:06
AndroidDevAndroidDev
10.1k2393167
10.1k2393167
1
you may also be interested inprofile.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
add a comment |
1
you may also be interested inprofile.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
add a comment |
3 Answers
3
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
Remove the
return
statement:
profile.sortedWith(Comparator { profile1, profile2 ->
if (profile1.age > profile2.age) 1
else if (profile1.age < profile2.age) -1
else 0
})
Use
when
instead of anif-else
cascade:
profile.sortedWith(Comparator { profile1, profile2 ->
when {
profile1.age > profile2.age -> 1
profile1.age < profile2.age -> -1
else -> 0
}
})
use
Int.compareTo
:
profile.sortedWith(Comparator { profile1, profile2 ->
profile1.age.compareTo(profile2.age)
}
use
compareBy
:
profile.sortedWith(compareBy(Profile::age))
Don't use the general
sortedWith
when all you need issortedBy
:
profile.sortedBy(Profile::age)
or better (I think).sortedBy { it.age }
– forpas
Nov 19 '18 at 15:06
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%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
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 19 '18 at 13:26
answered Nov 19 '18 at 13:20
forpasforpas
11.9k3424
11.9k3424
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 19 '18 at 14:00
CililingCililing
464211
464211
add a comment |
add a comment |
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.
Remove the
return
statement:
profile.sortedWith(Comparator { profile1, profile2 ->
if (profile1.age > profile2.age) 1
else if (profile1.age < profile2.age) -1
else 0
})
Use
when
instead of anif-else
cascade:
profile.sortedWith(Comparator { profile1, profile2 ->
when {
profile1.age > profile2.age -> 1
profile1.age < profile2.age -> -1
else -> 0
}
})
use
Int.compareTo
:
profile.sortedWith(Comparator { profile1, profile2 ->
profile1.age.compareTo(profile2.age)
}
use
compareBy
:
profile.sortedWith(compareBy(Profile::age))
Don't use the general
sortedWith
when all you need issortedBy
:
profile.sortedBy(Profile::age)
or better (I think).sortedBy { it.age }
– forpas
Nov 19 '18 at 15:06
add a comment |
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.
Remove the
return
statement:
profile.sortedWith(Comparator { profile1, profile2 ->
if (profile1.age > profile2.age) 1
else if (profile1.age < profile2.age) -1
else 0
})
Use
when
instead of anif-else
cascade:
profile.sortedWith(Comparator { profile1, profile2 ->
when {
profile1.age > profile2.age -> 1
profile1.age < profile2.age -> -1
else -> 0
}
})
use
Int.compareTo
:
profile.sortedWith(Comparator { profile1, profile2 ->
profile1.age.compareTo(profile2.age)
}
use
compareBy
:
profile.sortedWith(compareBy(Profile::age))
Don't use the general
sortedWith
when all you need issortedBy
:
profile.sortedBy(Profile::age)
or better (I think).sortedBy { it.age }
– forpas
Nov 19 '18 at 15:06
add a comment |
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.
Remove the
return
statement:
profile.sortedWith(Comparator { profile1, profile2 ->
if (profile1.age > profile2.age) 1
else if (profile1.age < profile2.age) -1
else 0
})
Use
when
instead of anif-else
cascade:
profile.sortedWith(Comparator { profile1, profile2 ->
when {
profile1.age > profile2.age -> 1
profile1.age < profile2.age -> -1
else -> 0
}
})
use
Int.compareTo
:
profile.sortedWith(Comparator { profile1, profile2 ->
profile1.age.compareTo(profile2.age)
}
use
compareBy
:
profile.sortedWith(compareBy(Profile::age))
Don't use the general
sortedWith
when all you need issortedBy
:
profile.sortedBy(Profile::age)
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.
Remove the
return
statement:
profile.sortedWith(Comparator { profile1, profile2 ->
if (profile1.age > profile2.age) 1
else if (profile1.age < profile2.age) -1
else 0
})
Use
when
instead of anif-else
cascade:
profile.sortedWith(Comparator { profile1, profile2 ->
when {
profile1.age > profile2.age -> 1
profile1.age < profile2.age -> -1
else -> 0
}
})
use
Int.compareTo
:
profile.sortedWith(Comparator { profile1, profile2 ->
profile1.age.compareTo(profile2.age)
}
use
compareBy
:
profile.sortedWith(compareBy(Profile::age))
Don't use the general
sortedWith
when all you need issortedBy
:
profile.sortedBy(Profile::age)
answered Nov 19 '18 at 14:59
Marko TopolnikMarko Topolnik
146k19197323
146k19197323
or better (I think).sortedBy { it.age }
– forpas
Nov 19 '18 at 15:06
add a comment |
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
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%2f53375316%2flambda-argument-should-be-moved-out-of-parentheses%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
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