with

Close resources automatically when the block exits.

Example.kalm
pub fun firstLine() -> Text?:
    with reader = java.util.Scanner("hello\nworld"):
        if reader.hasNextLine():
            return reader.nextLine()

    return null
Example.kalm
pub fun firstLine() -> Text? {
    with reader = java.util.Scanner("hello\nworld") {
        if reader.hasNextLine() {
            return reader.nextLine()
        }
    }
    return null
}
Example.kalms
fun firstLine() -> Text?:
    with reader = java.util.Scanner("hello\nworld"):
        if reader.hasNextLine():
            return reader.nextLine()

    return null
Example.kalms
fun firstLine() -> Text? {
    with reader = java.util.Scanner("hello\nworld") {
        if reader.hasNextLine() {
            return reader.nextLine()
        }
    }
    return null
}
Example.java
import java.util.Scanner;

public final class Example {
    public static String firstLine() {
        try (var reader = new Scanner("hello\nworld")) {
            if (reader.hasNextLine()) return reader.nextLine();
        }
        return null;
    }
}

Learn more.