SE-0457: Expose attosecond representation of Duration

You are correct, it is off-topic. It was just a (too much detailed) warning.

3 Likes

I don’t think this API is motivated by a desire to actually store attoseconds. The desire is to store higher-resolution durations than can be stored in an Int64, and Int128 is the next size up that computers generally make available. It might seem “wasteful” in that most real-world instances of this type the several least significant bits will be zero, but alternatives like (Int64, UInt8) are more cumbersome to work with and possibly even less efficient at runtime.

5 Likes

Yes, that sounds reasonable to me.

On the more general issue of Int128 support in Swift, it's perhaps worth reiterating in this 128-bit-focused thread that 1_000_000_000_000_000_000_000_000_000_000_000_000 is a preposterous thing to see in code, even if it were to yield a BinaryInteger of appropriate width.

In theory the underbars are there in contemplation of the fact that someone might need to count up the zeros by hand. But I'm not sure I could even write this correctly without underbars.

As is no doubt clear to everyone already, this number should be spelled 1E36. I've found myself regretting the lack of exponential notation for integer constants on multiple occasions.

6 Likes

Just out of curiosity, why is var attoseconds: Int128 read-only?
We'll have to access directly var _low: UInt64 and var _high: Int64 to mutate an instance even if this proposal is realized.

2 Likes

Interesting. I can‘t think of a reason why not to do this but also no reasons why we should do it.

Do you have specific use cases in mind where this is helpful? I guess it is slightly shorter:
duration.attoseconds + 1
vs
duration + Duration(attoseconds: 1)
But on the other hand it looks odd having one addend without scale, no?

Curious what others think about this.

1 Like

duration.attoseconds += 1 came to my mind, but, to be honest, the reason why I wondered about setter missing was just because _low and _high were writable.

2 Likes

This is one of the few proposals I've read recently that seems unambiguously sensible. +1.

1 Like

Hi all, this proposal has been accepted. Thanks for your attention!

6 Likes