Does Swift support number suffixes?

In C++, I can write 123457890ull to imply it's an unsigned long long integer. Does Swift provide similar language construct?

Answered by DTS Engineer in 773427022
auto n = 12345ull;

Well, n is unsigned long long, which isn’t necessarily uint64_t in C-based languages (-:

Real world example (unit test)

That’s a bit tricky because you rely on the tuple labels. I’d probably give the array a type:

let tests: [(s: _, v: UInt64)] = [
    ("1234",  1234),
    ("1kb",   1024),
    ("1234k", 1234 * 1024),
    ("3mb",   3 * 1024 * 1024),
    ("2GB",   2 * 1024 * 1024 * 1024),
    ("7tb",   7 * 1024 * 1024 * 1024 * 1024),
]

Note that I’m using _ as a type placeholder for s (per SE-0315). I’m also relying on Swift’s built-in conversion from unlabelled to labelled tuples. This yields a value with the right type with a reasonably small amount of ceremony.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

No.

The ‘correct’ way to do this is to write something like 42 as UInt64. The most common way that folks actually write this is with UInt64(42). Since SE-0213 these have been mostly equivalent.

Having said that, most of the time you don’t need to do this. If you find yourself doing it a lot, post an example here and we can see if there’s a better way.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Actually I don't have 'a lot' examples. I just started real app development using Swift a couple of months ago.

For this specific scenario, I really like to have what I have in C/C++.

Advantages:

  1. It can save a few key strokes and makes me as a developer feel better; and even more, makes Swift feels more swift.
auto n = 12345ull;

vs

var n: UInt64 = 12345
  1. Real world example (unit test):
func testByteSize() {
        let tests = [
            (s: "1234", v: 1234),
            (s: "1kb", v: 1024),
            (s: "1234k", v: 1234 * 1024),
            (s: "3mb", v: 3 * 1024 * 1024),
            (s: "2GB", v: 2 * 1024 * 1024 * 1024),
            (s: "7tb", v: 7 * 1024 * 1024 * 1024 * 1024),
        ]

        for test in tests {
            let n = test.s.parseByteSize()
            XCTAssertNotNil(n)
            XCTAssertEqual(UInt64(test.v), n!)
        }
    }

If Swift allows me to code a number as 1234ull, I won't have to do the UInt64(test.v) cast. Moreover, I really like the tuple's v field be UInt64 instead of default Int.

Accepted Answer
auto n = 12345ull;

Well, n is unsigned long long, which isn’t necessarily uint64_t in C-based languages (-:

Real world example (unit test)

That’s a bit tricky because you rely on the tuple labels. I’d probably give the array a type:

let tests: [(s: _, v: UInt64)] = [
    ("1234",  1234),
    ("1kb",   1024),
    ("1234k", 1234 * 1024),
    ("3mb",   3 * 1024 * 1024),
    ("2GB",   2 * 1024 * 1024 * 1024),
    ("7tb",   7 * 1024 * 1024 * 1024 * 1024),
]

Note that I’m using _ as a type placeholder for s (per SE-0315). I’m also relying on Swift’s built-in conversion from unlabelled to labelled tuples. This yields a value with the right type with a reasonably small amount of ceremony.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Does Swift support number suffixes?
 
 
Q