https://kotlinlang.org logo
#announcements
Title
# announcements
p

poohbar

06/25/2019, 1:49 PM
I don't understand why does the compiler think i am trying to pass a second lambda to
run
Copy code
run {
  // some code
}

{ /*my unrelated block here*/ } shouldNotThrow AnyException
j

Jacques Smuts

06/25/2019, 1:51 PM
lint warning: “LonelyLambda. Your lambda is lonely. Bring it closer.”
p

poohbar

06/25/2019, 1:52 PM
@Shawn
val x = { my code }
is valid
s

streetsofboston

06/25/2019, 1:52 PM
🙂 It is not java. A statement that starts with a
{
and ends with a
}
is an expression that results in a lambda. This lambda is not run and just sits there. It is lonely 🙂
p

poohbar

06/25/2019, 1:52 PM
so
{ my code }
is valid too
m

marstran

06/25/2019, 1:52 PM
Because
{ }
is not a block. It's a lambda.
👆 1
You could do
run { ... }
s

Shawn

06/25/2019, 1:52 PM
like I said, doesn’t do what it does in Java
like sure you can have a bare lambda without assigning it to anything
p

poohbar

06/25/2019, 1:53 PM
this compiles perfectly fine Shawn:
Copy code
if (true) {
  // nothing
}

{ myMock.method(arg) } shouldNotThrow AnyException
not sure why are we talking about Java here, this is valid Kotlin code
s

Shawn

06/25/2019, 1:54 PM
1. does
myMock.method()
even run 2. the compiler knows not to treat a bare lambda as an argument to an `if`…
s

streetsofboston

06/25/2019, 1:54 PM
It is valid, but it doesn’t do anything. In java, similar syntax will run the code in that block. In Kotlin, it won’t run that code.
s

Shawn

06/25/2019, 1:54 PM
valid != useful
p

poohbar

06/25/2019, 1:54 PM
yes it will run the code
because
shouldNotThrow
has
this.invoke()
in it
s

streetsofboston

06/25/2019, 1:55 PM
Ah… I didn’t know that was code.
p

poohbar

06/25/2019, 1:55 PM
it is an infix function
s

streetsofboston

06/25/2019, 1:55 PM
You didn’t say it was… it looked like some comment or something 🙂
👆 1
p

poohbar

06/25/2019, 1:56 PM
my complaint is that the compiler should not treat it as a second lambda when it knows the previous code can't accept another lambda
i think there should be room in the grammar to handle this correctly
s

streetsofboston

06/25/2019, 1:57 PM
What is the signature of
shouldNotThrow
?
p

poohbar

06/25/2019, 1:57 PM
Copy code
infix fun <T : Throwable> (() -> Any?).shouldNotThrow(expectedException: KClass<T>)
s

streetsofboston

06/25/2019, 2:02 PM
I think you find an issue. I can imagine that putting the lambda of your
shouldNotThrow
on the same line as the closing bracket of the `run`’s lambda generates a compiler error, but you put it on a new line. This should not give a compiler issue, I think.
p

poohbar

06/25/2019, 2:04 PM
Exactly! Makes no sense to me.
s

streetsofboston

06/25/2019, 2:05 PM
I would file an issue and see what JetBrains has to say 🙂
p

poohbar

06/25/2019, 2:05 PM
a workaround:
Copy code
run {
 // some code
}

val call = { myMock.method(arg) }
call shouldNotThrow AnyException
d

Dias

06/25/2019, 2:06 PM
a workaround would also be putting semicolon at the end run block
👆 2
now I see why kotlin still allows you to have semicolons 😛
🚛 1
p

poohbar

06/25/2019, 2:07 PM
nice! that is concise!
still annoying though 😄
d

Dias

06/25/2019, 2:08 PM
tho I don't see any issue with your workaround, I wouldn't event call it a workaround, I just do it most of time
p

poohbar

06/25/2019, 2:09 PM
if i have a lot of calls to verify, i prefer one line per each
☝️ 1
l

LeoColman

06/25/2019, 4:23 PM
perhaps you can invert this call?
In #kotlintest we do
shouldThrow { call() }
Or
shouldNotThrow { }
4 Views