Many of the options include the Force value option:
Add or remove space between nested parens
sp_paren_paren { Ignore, Add, Remove, Force }
What does it mean? How is it different than Add?
Add means "add if not already present", meaning that if something's already there, leave it (and the formatting alone). Force means add if not present, and reformat if it is present:
// Original
if (cond) {
func();
}
// Add curly braces (already present, leaves formatting alone)
if (cond) {
func();
}
// Force curly braces
if (cond) {
func();
}
Or another example:
// Original
if (cond)
func();
// Add curly braces
if (cond) {
func();
}
// Force curly braces (behaves just like add in this case)
if (cond) {
func();
}
Add adds if it is not there.
Remove removes if it is there.
Force does a remove then an add.
As "Add or remove X between A and B"
Add: only adds a X when there is no X appeared
AB -> AXB
AXB -> AXB
AXXB -> AXXB
Remove: removes all appeared X
AB -> AB
AXB -> AB
AXXB -> AB
Force: as edwinc said Remove then Add -> Removes all (any) X first and
adds a X finally
AB -> AXB
AXB -> AB -> AXB
AXXB -> AB -> AXB
But sometimes 'add X' may be defined as add some number X elsewhere, so Force will like a "reformat" as Chris said.
Related
How does the Kotlin compiler decide whether an expression enclosed in { } is a block or a lambda?
Consider this:
val a: Int
if (cond)
a = 1
else
a = 2
can be written more succinctly as:
val a =
if (cond)
1
else
2
Similarly, one might think that this:
val a: () -> Int
if (cond)
a = { 1 }
else
a = { 2 }
should be able to be written more succinctly like this:
val a =
if (cond)
{ 1 }
else
{ 2 }
But this is not the same: a is now an Int, and not of type () -> Int, because now the { 1 } is no longer a lambda. What are the rules that say whether something is a lambda or a block?
I didn't look into the Kotlin lexer, but I guess there are few possible places in the code where the compiler expects either a single expression or a block of code. That includes the code immediately following most of control flow statements like: if, else, while, when (one of its cases), etc. If you put { in one of these places, it will be interpreted as a start of the block of code related to this control flow statement and not as a lambda.
This is as simple as that. Note that even if you hint the compiler about the type, it still won't work:
// compile error
val a: () -> Int = if (cond)
{ 1 }
else
{ 2 }
It will be interpreted more like this:
// compile error
val a: () -> Int = if (cond) {
1
} else {
2
}
{ after if condition is always interpreted as a start of block of code. You need to put double {, } in cases like this:
// works fine
val a: () -> Int = if (cond) {
{ 1 }
} else {
{ 2 }
}
To put it very succinctly and easy to remember, the first opening brace after an if/when/else/for is always assumed to be the opening of a block. Use double braces if you want a lambda in there.
This is specified in the Kotlin Language Specification, section 1.2: Syntax Grammar:
The grammar defines a block as statements between { and }. In most cases (such as function bodies) the syntax is different between a block and a lambda. Where it is the same is in the usage of controlStructureBody - these are the places where the block has a value, or where you could put a non-block expression in its place. If you search the whole spec document for "controlStructureBody", you'll find it's used in the following places:
For statement
While statement
Do-while statement
If expression
When expression
When entry
In every other place where a value is required, a '{' signifies the start of a lambda.
For example, Char has a member function isLetter(). Is there any way to call it in a case? The code below does not work.
var ch:Char = null;
when(ch)
{
'*' -> print("You typed an asterisk.");
isLetter() -> print("You typed a letter.");
else -> print("You typed something.");
}
The uses of the parameter to when are quite restrictive: equality, order with < &c, inclusion with in, type checking with is, and that's about it. So for anything more complex, it's usually easier not to specify a parameter. In this case, the following isn't much less concise or clear:
when {
ch == '*' -> print("You typed an asterisk.")
ch.isLetter() -> print("You typed a letter.")
else -> print("You typed something.")
}
Some points worth noting:
Semicolons are not needed in Kotlin (except for a few rare ambiguous cases such as putting multiple statements on a line — which is rarely a good idea).
print() does not add a trailing newline, which can cause problems; println() is more common.
var ch: Char = null won't compile, because the type Char is not nullable. Either give a non-null default value, or make it nullable by specifying the type as Char?. However, in the latter case, you won't be able to call ch.isLetter(), because that would risk a NullPointerException. So you'd either need to add a null case before that, or handle the null in that check, either with ch != null && ch.isLetter(), or ch?.isLetter() == true — both of which are ugly.
There is, but there's also the possibility of it not being applicable in your situation.
when can be used without an argument, like this:
fun main() {
val ch: Char = 'd'
when {
ch.isLetter() -> println("It's a letter")
ch.isDigit() -> println("It's a number")
else -> println("It's neither a letter, nor a number")
}
}
But if you make ch nullable like var ch: Char? = null, you won't be able to call member functions in that when clause.
You can check whether ch is in a specific CharCategory. For isLetter() those are the following five:
when(ch)
{
'*' -> print("You typed an asterisk.")
in CharCategory.UPPERCASE_LETTER,
in CharCategory.LOWERCASE_LETTER,
in CharCategory.TITLECASE_LETTER,
in CharCategory.MODIFIER_LETTER,
in CharCategory.OTHER_LETTER -> print("You typed a letter.")
else -> print("You typed something.")
}
first in your code var ch:Char = null
it must be Char?
the solution is simple don't put argument:
var ch:Char = 'c'
when {
ch == '*' -> println("*")
ch.isLetter() -> println("letter")
ch.isDigit() -> println("digit")
}
hope my answer helped you
Lets assume the following when-statement:
when(a)
{
x -> doNothing()
y -> doSomething()
else -> doSomethingElse()
}
Now i'm looking to eliminate the boilerplate-function "doNothing()", e.g.:
x -> //doesn't compile
x -> null //Android Studio warning: Expression is unused
x -> {} //does work, but my corporate codestyle places each '{‘ in a new line, looking terrible
//also, what is this actually doing?
Any better ideas?
I can't just eliminate x -> completely, as that would lead to else -> doSthElse()
Edit: directly after writing this Question, i figured out a possible answer x -> Unit. Any shortcomings with that?
Kotlin has two existing possibilities to express a "do nothing" construct in when statements. Either Unit or an empty pair of braces. An empty block will just execute nothing.
There's nothing else planned in that regard (see here).
To answer your question regarding "also, what is this actually doing?" for the empty block, looking at the bytecode and translating it into Java helps:
val x = 33
when(x)
{
1 -> {}
2 -> Int
3 -> Unit
else -> Double
}
Translates to
int x = 33;
switch(x) {
case 1:
case 3:
break;
case 2:
IntCompanionObject var10000 = IntCompanionObject.INSTANCE;
break;
default:
DoubleCompanionObject var1 = DoubleCompanionObject.INSTANCE;
}
What is it about Iterable.fold(...) in Kotlin that allows me to put the operation function after the closing parentheses?
val numbers = listOf(5, 2, 10, 4)
// operation function passed as the second param of fold
val sumDoubled1 = numbers.fold(0, { sum, n -> sum + n * 2 })
println(sumDoubled1)
// operation function after the closing paren of fold
val sumDoubled2 = numbers.fold(0) { sum, n -> sum + n * 2 }
println(sumDoubled2)
Further to Pavneet's answer, the rationale behind this that it allows you to write what look like language extensions. For example:
repeat (10) {
// Do something
}
That looks like a new type of loop structure; but it's really just a function called repeat() that takes two parameters; an integer, and a lambda.
Also, if the lambda is the only parameter, you can omit the parens entirely, e.g.:
repeatForever {
// Do something
}
(repeat() is in the standard library; repeatForever() is left as an exercise for the reader :-)
The ability to neaten some inline method calls, such as someValue.takeIf{ it > 0 } is just a nice side-effect of that.
It is called Passing trailing lambdas means, if a methods takes last parameter input as function(aka method literal) then it can be placed outside that method call though you can also place it inside the brackets as well. A simple example would be:
fun main() {
processInput("Lambda", { println(it) })
processInput("Passing trailing lambda") { println(it) }
processInput("Passing trailing lambda with named param") { input -> println(input) }
}
fun processInput(input:String, method:(str:String)->Unit){
method(input.toUpperCase()) // additional logic
}
Output:
LAMBDA
PASSING TRAILING LAMBDA
PASSING TRAILING LAMBDA WITH NAMED PARAM
I have some code that roughly looks like this:
val myObject = myObjectRepository.findById(myObjectId);
when {
matchesSomething(myObject) -> doSomethingWithMyObject(myObject)
matchesSomethingElse(myObject) -> doSomethingElseWithMyObject(myObject)
else -> log.warn("No match, aborting");
}
While this works I would think that the following (which doesn't work) would be an improvement if I only need access to myObject inside the scope of when:
when(myObjectRepository.findById(myObjectId)) { myObject ->
matchesSomething(myObject) -> doSomethingWithMyObject(myObject)
matchesSomethingElse(myObject) -> doSomethingElseWithMyObject(myObject)
else -> log.warn("No match, aborting");
}
The error I get here is:
Unresolved reference: myObject
Can you do something like this in Kotlin and if so how? If not, is there a particular reason for why this shouldn't be allowed?
As shown in the documentation, the proper syntax would be
val myObject = myObjectRepository.findById(myObjectId);
when {
matchesSomething(myObject) -> doSomethingWithMyObject(myObject)
matchesSomethingElse(myObject) -> doSomethingElseWithMyObject(myObject)
else -> log.warn("myObject not found, aborting")
}
Or, to actually match what your first snippet does:
val myObject = myObjectRepository.findById(myObjectId);
when(myObject) {
null -> log.warn("myObject not found, aborting");
matchesSomething(myObject) -> doSomethingWithMyObject(myObject)
matchesSomethingElse(myObject) -> doSomethingElseWithMyObject(myObject)
}
You have to be careful about the syntax. In a while we use an arrow -> which has nothing to do with lambdas. I think this is what you were trying in your example.
The only valid syntax for when is this:
when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> { // Note the block
print("x is neither 1 nor 2")
}
On the left side of the arrow -> you declare what the object (x) is being matched against, whereas on the right side you tell what will be executed in that case. Read about this here.
In your example you tried to chain multiple -> which does not work.
This is supported as of Kotlin 1.3. It's referred to as "Capturing when subject in a variable" and looks like this (taken from their documentation):
fun Request.getBody() =
when (val response = executeRequest()) {
is Success -> response.body
is HttpError -> throw HttpException(response.status)
}