[swift-evolution] Proposal: Extend unnamed let variable scope

Jason Pollack jlpollack at gmail.com
Thu Dec 10 10:46:40 CST 2015


I was experimenting in Swift with RAII, a technique I find very useful from
C++.  In general it works well, but there is something I found unexpected.

Consider this class:

class RAII {
    init() {
        print("some resource acquired")
    }
    deinit {
        print("some resource released")
    }
}

And this sample code:

        let b = true
        if (b)
        {
            print("Entered scope")
            let raii = RAII()
            print("Going out of scope")
        }
        print("Left scope")

As expected, I see the output:

Entered scope
some resource acquired
Going out of scope
some resource released
Left scope

However, the compiler gives me a warning on the 'let raii = ' line:
 "Initialization of immutable value 'raii' was never used; consider
replacing with assignment to '_' or removing it"

If I change that line to:

let _ = RAII()

the warning goes away, but I get the unexpected output:

Entered scope
some resource acquired
some resource released
Going out of scope
Left scope

It appears the object instance is being destroyed immediately after being
created, because the compiler correctly sees that nobody needs it.

Therefore, I propose that such instances stay referenced until the end of
the scope they are declared in.

(As an aside, I would like to see a deinit on a struct to support this
technique as well.)

Thanks,
-Jason-
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151210/930a5efe/attachment.html>


More information about the swift-evolution mailing list