I have an issue with the following piece of code: ...
# announcements
k
I have an issue with the following piece of code:
Copy code
inline fun StringReader.tryTo(block: StringReader.TryTo.() -> Unit){
	contract {
		callsInPlace(block, kotlin.contracts.InvocationKind.EXACTLY_ONCE)
	}
	try {
		it.block()
	} catch (e: Exception) {
		it.reset()
	}
}
And
Copy code
override fun parse(reader: StringReader): String? {
	reader.tryTo {
		throw Exception("hi")
	}
	return null // Marked as unreachable code
}
On a inline lambda seems to not realise that an exception that is thrown in the lambda can be caught in the inlined function. This gets even worse when the return is not nullable and the user doesn't add a return as it is marked as unreachable and as a result at runtime the function will return null.
Thinking about it, this might be my fault. I should throw the exception again or mark it as
AT_MOST_ONCE
.
Otherwise I might get partially initialised values.
k
Either it's a bug or the requirements of
callsInPlace
need to be clarified, there's nothing in the docs. Definitely report this on YouTrack.
k
Ok
k
And post the link because now I'm interested simple smile
k
Found this in similar issues: https://youtrack.jetbrains.com/issue/KT-26523
If this problem occurs only with runCatching I'd say that we should remove contract in 1.3
Maybe someone should have actually tested this =P
Can you add a related issue?
k
If you click the "open command dialog" button on the top left you can type "relates to <id>", I'm not sure if that actually works though, I've never tried that.
k
It gave a msg like you are not allowed to or something
k
Makes sense, we aren't allowed to edit most properties of the issues.