Prettier: how to force 1 spacing line between sections? - line

so lets say my code is:
function test(){
}
function test2(){
}
and after prettier, i want it to look like:
function test(){
}
function test2(){
}
is it possible and if so, how/where?

Related

Undetected throw declaration (Kotlin)

Let's have a function which just computes something in try-catch and returns the result:
private fun compute(): String {
return try {
// do some computation
// ...
"result"
} catch(t: Throwable) {
throw RuntimeException("Uups") // <-- GOAL: extract this to a dedicated method
}
}
I would like to extract the throw declaration to a separate function (which contains the my boilerplate code).
However, I'm unable to compile such setup in Kotlin.
A simplistic (and still uncompilable) version of the described problem:
private fun compute(): String {
return try {
// do some computation
// ...
"result"
} catch(t: Throwable) {
justThrow() // <-- NOT COMPILABLE, STRING EXPECTED
}
}
#Throws(RuntimeException::class)
private fun justThrow() {
// some boilerplate code
// ...
throw RuntimeException("Uups")
}
How write justThrow() method in Kotlin so that the whole code is compilable?
In Java this case would be detected by a compiler (I suppose).
Kotlin version: 1.4.21
You can declare the return type of your method as Nothing. This type can be used for any method that does not return normally. That might be because it will always throw an exception, or simply never returns at all, for instance because it contains an infinite loop.
private fun justThrow(): Nothing {
// some boilerplate code
// ...
throw RuntimeException("Uups")
}

How to verify whether a function is called on a mock object who parameter has a lambda expression

Let's say I've a function named
fun doSomething() {
customObject.callThis(10, 20)
}
And customObject is an object of class CustomObject and it's mocked using Mockito. The below is the Unit test one will write for the above code.
#Test
fun doSomething() {
// Call the doSomething() function
verify(customObject).callThis(10, 20)
}
This works perfectly. Now my doubt comes when we write doSomething function as below.
fun doSomething() {
customObject.callThis(10, { navigation.finish() })
}
Where navigation is a mock object of class Navigation. Now, how do I test the above function?
If you are using mockito-kotlin, you could do something like the following:
import com.nhaarman.mockitokotlin2.inOrder
import com.nhaarman.mockitokotlin2.eq
import com.nhaarman.mockitokotlin2.any
inOrder {
verify(customObject).callThis(eq(10), any())
verify(navigation).finish()
}

How to access a function from within itself in Kotlin?

In java I can write something like this
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
handler.post(this);
}
});
When I write something like this in Kotlin I get a compilation error. this inside of the lambda expression doesn't work like I expect. What can I do?
val handler = Handler()
handler.post{
handler.post(this) // this line throws a compilation error
}
val handler = Handler()
fun doAction() {
handler.post(::doAction) // like `this`
}
handler.post(::doAction)
Although technically Tim's solution also works, but he should post it as an answer, as that one is easier. If he does, accept that one instead.
val handler = Handler()
handler.post(object : Runnable {
override fun run() {
handler.post(this)
}
})

Convert Java to Kotlin (With a function as an argument)

I want this code in Kotlin:
public class LobbySwitcherGui extends AGUI
{
public LobbySwitcherGui()
{
super(27, "someTitle");
setItem(0, new ItemStack(Material.NETHER_STAR), player -> {
player.sendMessage("Some message");
});
}
}
but if I convert it in IntellIJ its doesnt work for me.
class LobbySwitcherGui : AGUI(27, "someTitle")
{
init {
setItem(0, ItemStack(Material.NETHER_STAR), { player -> player.sendMessage("Some message") })
}
}
The last Argument istn working and I cant fix it..
AGUI class is this: https://ghostbin.com/paste/gjdzo
kotlin can create Runnables and Function, BiFunction, etc that way..
you seem to create something called a YourGUIAction, check if that might be replaced with action: (Player) -> Unit
or in general (ArgumentTypes) -> ReturnType

How can I `return` from inside of a call to `use`?

In Kotlin, this code compiles:
private fun bar(): Boolean = TODO()
fun works(): Int {
while (true) {
if (bar()) {
return 5
}
}
}
(This is a pared down example of my real code to illustrate the issue I'm running into.)
I actually need to use a file during this loop, and close on exit:
fun openFile(): InputStream = TODO()
fun doesnt_work(): Int {
openFile().use { input ->
while (true) {
if (bar()) {
return 5
}
}
}
} // line 42
This doesn't compile. I get the error:
Error:(42, 5) Kotlin: A 'return' expression required in a function with a block body ('{...}')
I've found two ways to work around this, but both are kind of awkward.
One way is to use a variable to hold the result, and break from the loop right when it's set:
fun works_but_awkward(): Int {
openFile().use { input ->
val result: Int
while (true) {
if (bar()) {
result = 5
break
}
}
return result
}
}
This is especially awkward in my real code, as I have a nested loop, and so I need to use a labelled break.
The other way to work around this is to have a named function for the loop:
fun workaround_with_named_function(): Int {
fun loop(input: InputStream): Int {
while (true) {
if (bar()) {
return 5
}
}
}
return openFile().use { loop(it) }
}
This seems a bit better, but I'm still surprised that the use abstraction is so leaky that I can't do an early return from within a loop. Is there a way to use use with an early return in a loop that's less awkward?
Cause Kotlin compiler isn't smart enough to undestand that use with code inside will return something from the function. The reason of such behavior is inability to guarantee compiler that lambda will be called exactly once.
Another way to workaround this is throwing exception in the end of the function:
fun doesnt_work(): Int {
openFile().use { input ->
while (true) {
if (bar()) {
return 5
}
}
}
throw IllegalStateException("Something goes wrong")
}
P.S. I am not sure, but seems it can be compiled without any hacks when contract system will be added to Kotlin. And it is probably going to be in version 1.3
This should work.
fun openFile(): InputStream = TODO()
fun doesnt_work(): Int {
return openFile().use { input ->
while (true) {
if (bar()) {
return#use 5
}
}
-1 // unreachable return value
// just to help Kotlin infer the return type
}
}
Remember, use is a function whose return value is exactly the same with the return value of the lambda. So returning the value (here it's 5) in the lambda and return the return value of use should work.
Also, if I were you, I'll write the function like this:
fun doesnt_work() = openFile().use { input ->
while (true) if (bar()) return#use 5
-1
}