Tuple element name mystry: old previously create Xcode Playground project is different from newly created Playground

Edit: Xcode Version 12.5 beta (12E5220o), the Playground project is created with iOS template. Not the iPad Playground app!

This

extension Sequence where Element: Comparable {
    public var runLength: [(element: Element, count: Int)] { ... }
}

when called with [Int], the tuple elements has names, with String, tuple elements have no names.

When I reduce down, the same call signature works for both String and [Int]: both have tuple elementals names.

I've used (element: Element, count: Int) (with element name) everywhere (though I don't believe I need to do so) but still, same problem.

Here is my code:

// For use with Sequence.reduce(into:) to scan each element to keep count of each element
private struct RunLength<T: Comparable> {
    private struct ElementCount {
        let element: T
        var count = 1
    }

    // keep track the count of each element in this array
    private var elementCounts = [ElementCount]()

    // scan each element, call this to count it
    mutating func process(element: T) {
        // element is a different value, so it's start of a new run
        // or this is the very first run when elementCounts.isEmpty so on left side is nil
        if elementCounts.last?.element != element {
            elementCounts.append(ElementCount(element: element))
        } else {
            elementCounts[elementCounts.count - 1].count += 1
        }
    }

    // the result as an array of tuples to avoid leeking out implementation type details
    var result: [(element: T, count: Int)] {
        elementCounts.map { (element: $0.element, count: $0.count) }
    }
}


extension Sequence where Element: Comparable {
    public var runLength: [(element: Element, count: Int)] {
        reduce(into: RunLength()) { $0.process(element: $1) }
            .result
    }
}




// with array, the result tuple element is anotated with name
print([1, 1, 1, 1, 2, 2, 2, 3, 3, 1].runLength)
// prints out: [(element: 1, count: 4), (element: 2, count: 3), (element: 3, count: 2), (element: 1, count: 1)]

// with String, the result tuple element is not annotated with name
print("aaaabbbcca".runLength)
// prints out: [("a", 4), ("b", 3), ("c", 2), ("a", 1)]

FWIW, on iOS Playground 3.4, I got

[(element: "a", count: 4), (element: "b", count: 3), (element: "c", count: 2), (element: "a", count: 1)]

You are right, on Xcode 12.5b, just created a new iOS playground and it works fine. Even when I remove all tuple element names in:

    // the result as an array of tuples to avoid leeking out implementation type details
    var result: [(T, Int)] {
        elementCounts.map { ($0.element, $0.count) }
    }

How can I see this version info?

So it has this problem on my existing iOS Playground created long long time ago.

Now I want to know what cause the problem on this old playground? And how to get my old playground project up to date so it runs like a newly created one? I look for some playground project settings in Xcode, but couldn't find any.

On this newly created Playground, the first page

import UIKit

the rest of the pages:

import Foundation

I run this in the old playground project:

#if swift(>=5.5)
print("Hello, Swift 5.5")
#elseif swift(>=5.4)
print("Hello, Swift 5.4")
#elseif swift(>=5.3)
print("Hello, Swift 5.3")
#elseif swift(>=5.2)
print("Hello, Swift 5.2")
#elseif swift(>=5.1)
print("Hello, Swift 5.1")
#elseif swift(>=5.0)
print("Hello, Swift 5.0")
#elseif swift(>=4.1)
print("Hello, Swift 4.1")
#elseif swift(>=4.0)
print("Hello, Swift 4.0")
#elseif swift(>=3.0)
print("Hello, Swift 3.x")
#else
print("Hello, Swift 2.2")
#endif

it prints out Hello, Swift 5.4

App store :smirk:. Or Setting->Playground.

No idea. Usually, I'll just create a new project and copy the code over.

I'm pretty sure it's tied to (iOS) Playground file. My old file is still using Swift 5.2.

However, my old playground project is using Swift 5.4 as the little test above did show. Also, it show:

_functionBuilder is now resultBuilder

So it's compiling with 5.4. Just don't know why the difference I'm seeing. Anyway, no big problem. I now know need to re-create playground as new.....

Thanks!!

Just in case, note though that I meant Playground running on iOS, not Xcode Playground for iOS devices (talk about confusing naming...).

1 Like

:ok_hand::pray:

And I'm talking about Xcode Version 12.5 beta (12E5220o), the Playground project is created with iOS template: my project was created when I installed the first Big Sur beta time. I've verified it's using Swift 5.4.

Terms of Service

Privacy Policy

Cookie Policy