public fun findLine(i: Int, required: Boolean): LineContext?
Can I describe that when
required
is hardcoded to
true
the return type is not-null with contracts? E.g.
Copy code
val result = findLine(0, true)
// result is of type LineContext. Note the missing ?
🚫 1
y
Youssef Shoaib [MOD]
01/09/2024, 12:30 PM
Seems like you should use 2 methods instead:
Copy code
public fun findLineOrNull(i: Int): LineContext?
public fun findLine(i: Int): LineContext
âž• 1
r
Rob Elliot
01/09/2024, 12:31 PM
Wouldn't it be simpler to just overload it anyway?
Copy code
public fun findLine(i: Int): LineContext = findLine(i, true) ?: throw IllegalStateException("result should never be null if required is true")
e
Edoardo Luppi
01/10/2024, 10:57 AM
Hey thanks! This is a bit of a particular use case as it applies to ANTLR.
I didn't want to duplicate methods, mostly to keep the compiled size down.
You can see what I ended up doing here https://github.com/Strumenta/antlr-kotlin/pull/142