b

    beholder

    5 years ago
    Is it possible to compiler choose
    AutoClosable.use()
    instead of
    Closable.use()
    when class implements both interfaces? Implementation for AutoClosable is more simple and efficient.
    k

    kingsley

    5 years ago
    Since it’s statically dispatched. This should work:
    (someCloseable as AutoClosable).use {}
    b

    beholder

    5 years ago
    @kingsley, yes, I tried that, but it's rather ugly
    k

    kingsley

    5 years ago
    Well. That’s about the option that you have. You could also do this:
    val foo: AutoCloseable =someCloseable
    foo.use {}
    I don’t really see a reason for picking one over the other anyway. Simple and efficient doesn’t sound compelling. If you are very concerned about the implementation detail, you can always make your own extension function 🙂