denwav / kotlin-try-with-resources Goto Github PK
View Code? Open in Web Editor NEWLicense: Apache License 2.0
License: Apache License 2.0
I think the following lines in the decompiled Java code are incorrect:
kotlin-try-with-resources/readme.md
Lines 146 to 150 in 968782e
This code would always call addSuppressed(null)
. When I decompile the Kotlin code to Java, it shows if (var10000 != null)
which seems to be correct.
I think Catcher
can be simplified by only having one property for the exception and using for example an additional boolean property to track whether the exception was thrown by catch
. This way you avoid the duplication for the exception suppression logic. Additionally if you store the exception property value in a local variable in the finally
function you can avoid the !!
operator (and therefore simplify the bytecode).
Here is the simplified version:
class Catcher(val manager: ResourceManager) {
var t: Throwable? = null
var isOriginalException = true
init {
t = manager.t
}
inline infix fun <reified T : Throwable> catch(block: (T) -> Unit): Catcher {
val t = this.t
if (isOriginalException && t is T) {
this.t = null
isOriginalException = false
try {
block(t as T)
} catch (thrown: Throwable) {
this.t = thrown
}
}
return this
}
inline infix fun finally(block: () -> Unit) {
val t = this.t
try {
block()
} catch (thrown: Throwable) {
if (t == null) {
throw thrown
} else {
t.let {
it.addSuppressed(thrown)
throw it
}
}
}
// At this point the finally block did not throw an exception
// Throw the previous exception, if any
t?.let { throw it }
}
}
Feedback is appreciated! I hope I did not overlook something.
This project provides a try-with resources helper, but this project's name doesn't really indicate this.
It's difficult to remember the exact name, and the name itself is very generic. This makes it difficult to find the project, searches like "kotlin try with resources" and even "kotlin-utils" don't yield this library.
Could you consider changing the name to something like "Kotlin-MultiClosable"? (just a something I quickly came up with, I'm sure there's a better one).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.