I think this would be pretty neat to have! Some thoughts reading through the pitch:
Propagation
The propagation rules seem backwards to me. I realize after re-reading the earlier pitches that they were initially the other way around, but I'd like to re-propose that the compile-time guarantees flow forwards rather than backwards, giving the role of @const
to be a constraint where it is needed instead:
@const let a = 42
let b = a // implicitly @const
This can be further simplified if we enable any literal value (or collection of literal values?) to be implicitly known at compile time:
let a = 42 // implicitly @const
let b = a // implicitly @const
@const let c = b // allowed because b is implicitly @const
Seen another way, this makes future compile-time computations implicitly supported, and marking something with the constraint either reserves the possibility for this in the future, or allows propagation into specialized function scopes.
Collections
For myself, the biggest benefit to annotate my code with something like @const
would be for data pages and lookup tables that could be baked in at compile time. I know there is a section in future directions for this, but it honestly feels like we need a pre-pitch of that to go along and inform this one to make sure all the details are hashed out.
Expressions
How will something like this compile? Will it warn that the conditional is superfluous? Can we make it explicit?
@const let condition = true
...
@const let a = 1
@const let b = 2
@const let c = condition ? a : b
Name
I'd much rather a more explicit name such as compiletime
that indicates to anyone coming across this for the first time what it does. const
is already used in many languages to mean what let
means in swift, and doesn't carry with it the same weight as a compile-time constant constraint. Especially for those that don't follow the evolution process, I imagine the first time they encounter const
in optimized code won't actually be met with clarity, but with confusion that something like completive
can avoid completely.
Placement
I'm not a fan of this being a (yet another) attribute. To me, the following would be much easier to read and remember:
compiletime let a = 42
func clamp(_ value: Int, min: compiletime Int, max: compiletime Int) {}