Not sure if my e-mail didn't go through or if discussion just fizzled out; one 
other benefit if we ever move to a proper message board is we might gain the 
ability to bump topics. Anyway, I'll resend my message just in case:



Just to add my thoughts, as I like the idea of adding the variables to the 
start somehow, but was wondering if might make sense to have a keyword such as 
"using", but allow it on all block statements, like-so:

        // Original use-case of repeat … while
        repeat using (var i = 0) {
                // Do something
        } while (i < 20)

        // for … in demonstrating combination of using and where
        for eachItem in theItems using (var i = 0) where (i < 20) {
                // Do something either until theItems run out or i reaches 20
        }

        // Standard while loop
        while let eachItem = it.next() using (var i = 0) where (i < 20) {
                // As above, but with an iterator and a while loop and 
conditional binding to also stop on nil
        }

        // Closure with its own captured variable
        let myClosure:(Int) -> Int = using (var i = 0) { i += 1; return i * $0 }

        // If statements as well
        if somethingIsTrue() using (var i = 0) where (i < 20) {
                // Do something
        }

        // Or even a do block; while it does nothing functionally new, I quite 
like it aesthetically
        do using (var i = 0) {
                // Do something
        }

Unifying principle here is that anything created in the using clause belongs to 
the loop, conditional branch etc. only, but exists outside the block itself 
(thus persisting in the case of loops and closures). I quite like the possible 
interaction with where clauses here as a means to avoid simple inner 
conditionals as well.

Basically the two clauses can work nicely together to avoid some common inner 
and outer boilerplate, as well as reducing pollution from throwaway variables.

Only one I'm a bit iffy on is the closure; I'm trying to avoid declaring the 
captured variable externally, but I'm not convinced that having using on its 
own is clear enough?

Anyway, just an idea!

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to