[Pitch] Compile-Time Constant Values

I agree with this. I have used templates with value components in Metal Shading Language (very similar to C++) to enforce compile-time constants. The const keyword would provide an alternative version of such in Swift without corrupting the intention of generics.

Furthermore, this could be a solution to the use of values instead of types in C++ templates when doing C++ interop. Potentially, the value components of a C++ function could be const parameters, while the typename components map to a Swift generic argument.

You aren't allowed to explicitly put brackets around a generic function call in Swift, as you must incorporate the type in a way the compiler can type-infer. I have experienced a related constraint in Metal. Combined with const being used in the way described above, we could overcome an otherwise impossible barrier to C++ interop. It may not be idiomatic, but it's the only solution that would get the job done.

I am planning to explore a DirectX backend for either a Swift for TensorFlow resurrection or a spinoff project, and good C++ interop in Swift 5.6 would be very helpful. For more on the S4TF stuff, I have a post on it that was just freed from the spam filter: Swift for TensorFlow Resurrection: Differentiation running on iOS

Also, I have used Swift Atomics before while trying to parallelize MultiPendulum and the @_semantics stuff indeed looks wierd.

4 Likes