Lambda argument should be moved out of parentheses
up vote
1
down vote
favorite
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 |
up vote
1
down vote
favorite
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 at 13:22
... and I meant:Alt+Enteror click on the light bulb to let Intellij solve that issue for you...
– Roland
Nov 19 at 13:25
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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 at 13:06
AndroidDev
9,8752391163
9,8752391163
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 at 13:22
... and I meant:Alt+Enteror click on the light bulb to let Intellij solve that issue for you...
– Roland
Nov 19 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 at 13:22
... and I meant:Alt+Enteror click on the light bulb to let Intellij solve that issue for you...
– Roland
Nov 19 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 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 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 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 at 13:25
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
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 |
up vote
2
down vote
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 |
up vote
1
down vote
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
returnstatement:
profile.sortedWith(Comparator { profile1, profile2 ->
if (profile1.age > profile2.age) 1
else if (profile1.age < profile2.age) -1
else 0
})
Use
wheninstead of anif-elsecascade:
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
sortedWithwhen all you need issortedBy:
profile.sortedBy(Profile::age)
or better (I think).sortedBy { it.age }
– forpas
Nov 19 at 15:06
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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 |
up vote
1
down vote
accepted
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 |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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 at 13:26
answered Nov 19 at 13:20
forpas
4,1561215
4,1561215
add a comment |
add a comment |
up vote
2
down vote
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 |
up vote
2
down vote
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 |
up vote
2
down vote
up vote
2
down vote
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 at 14:00
Cililing
380110
380110
add a comment |
add a comment |
up vote
1
down vote
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
returnstatement:
profile.sortedWith(Comparator { profile1, profile2 ->
if (profile1.age > profile2.age) 1
else if (profile1.age < profile2.age) -1
else 0
})
Use
wheninstead of anif-elsecascade:
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
sortedWithwhen all you need issortedBy:
profile.sortedBy(Profile::age)
or better (I think).sortedBy { it.age }
– forpas
Nov 19 at 15:06
add a comment |
up vote
1
down vote
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
returnstatement:
profile.sortedWith(Comparator { profile1, profile2 ->
if (profile1.age > profile2.age) 1
else if (profile1.age < profile2.age) -1
else 0
})
Use
wheninstead of anif-elsecascade:
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
sortedWithwhen all you need issortedBy:
profile.sortedBy(Profile::age)
or better (I think).sortedBy { it.age }
– forpas
Nov 19 at 15:06
add a comment |
up vote
1
down vote
up vote
1
down vote
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
returnstatement:
profile.sortedWith(Comparator { profile1, profile2 ->
if (profile1.age > profile2.age) 1
else if (profile1.age < profile2.age) -1
else 0
})
Use
wheninstead of anif-elsecascade:
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
sortedWithwhen 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
returnstatement:
profile.sortedWith(Comparator { profile1, profile2 ->
if (profile1.age > profile2.age) 1
else if (profile1.age < profile2.age) -1
else 0
})
Use
wheninstead of anif-elsecascade:
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
sortedWithwhen all you need issortedBy:
profile.sortedBy(Profile::age)
answered Nov 19 at 14:59
Marko Topolnik
144k18193320
144k18193320
or better (I think).sortedBy { it.age }
– forpas
Nov 19 at 15:06
add a comment |
or better (I think).sortedBy { it.age }
– forpas
Nov 19 at 15:06
or better (I think)
.sortedBy { it.age }– forpas
Nov 19 at 15:06
or better (I think)
.sortedBy { it.age }– forpas
Nov 19 at 15:06
add a comment |
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 at 13:22
... and I meant:
Alt+Enteror click on the light bulb to let Intellij solve that issue for you...– Roland
Nov 19 at 13:25