Giter VIP home page Giter VIP logo

kotlin-try-with-resources's People

Contributors

denwav avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

uluckas

kotlin-try-with-resources's Issues

`Catcher` can be simplified

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.

Rename project

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).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.