# \[Pitch\] Compile-Time Constant Values

**URL:** <https://forums.swift.org/t/pitch-compile-time-constant-values/53606>\
**Category:** Pitches\
**Created:** [November 19, 2021, 10:22pm UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606 "2021-11-19T22:22:41Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![kiel](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kiel/32/1392_2.png) [@kiel](https://forums.swift.org/u/kiel)\
**Post date:** [November 21, 2021, 8:36am UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/21 "2021-11-21T08:36:43Z")

</div>

> [@Dmitriy\_Ignatyev](#):
>
> `static const let` looks very strange in Swift, because let already has meaning of constant.  
> One more thing, that `static` also mean something fixed, unchanging. `static var` currently is a bit strange, but it is ok. At least it is not misleading at all.

I have quite a bit of sympathy for this line of thinking, too. I wonder if alternative keywords could take some inspiration from the way it is described or taught:

> [@ArtemC](#):
>
> values that can be known or computed during compilation and are guaranteed to not change after compilation

Perhaps keywords like `known`, `verified`, `proven` or `revealed` might (or might not!) be some alternatives?

```swift
static known let title = "Bar"
static verified let title = "Bar"
static proven let title = "Bar"
static revealed let title = "Bar"

```

I don't want to distract much from the deeper and more important discussion about the building blocks etc. But some initial ideas about a precise keyword or naming might help frame discussion and thinking about the problems compile time constants are attempting to solve.

* * *

Unrelated question. Does this pitch finally mean I can build programs with source code like this will fail to compile if the URL cannot be initialised?

```swift
static const let websiteUrl: URL = URL(string: "https://swift.org")

```

---

<div class="post-metadata">

**Author:** ![Dmitriy\_Ignatyev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dmitriy_ignatyev/32/11340_2.png) [@Dmitriy\_Ignatyev](https://forums.swift.org/u/Dmitriy_Ignatyev)\
**Post date:** [November 21, 2021, 1:51pm UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/22 "2021-11-21T13:51:42Z")

</div>

> [@ArtemC](#):
>
> values that can be known or computed during compilation and are guaranteed to not change after compilation

From this point pint of view, `static let` fulfills the requirement "guaranteed to not change".  
So new keyword adds some additional meanings: "known or computed during compilation" and "not change after compilation".  
As for "not change after compilation" – it can be omitted, because you know, changing value during compilation is something strange and useless. So "not change after compilation" and "guaranteed to not change" are equal meanings in this context.

As for aleternatives, I like `static known let` very much.

So, at least, we have 3 excellent alternatives to `const let`:  
`static known let`  
`static predefined let`  
`static compiletime let`

and several good ones:  
`static verified let`  
`static compilevalue let`  
`static compilesythesized let`  
`static proven let`  
`static revealed let`

What do others think?

---

<div class="post-metadata">

**Author:** ![Mordil](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mordil/32/5840_2.png) [@Mordil](https://forums.swift.org/u/Mordil)\
**Post date:** [November 22, 2021, 2:03am UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/23 "2021-11-22T02:03:24Z")

</div>

I would imagine anything conforming to the `ExpressibleBy*Literal` class of protocols could be compile-time enforced

---

<div class="post-metadata">

**Author:** ![Nobody1707](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nobody1707/32/2169_2.png) [@Nobody1707](https://forums.swift.org/u/Nobody1707)\
**Post date:** [November 22, 2021, 6:48am UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/24 "2021-11-22T06:48:01Z")

</div>

I think there was some talk of moving away from those protocols entirely, but certainly the existing ones should be allowed to be implemented as a compile time function.

---

<div class="post-metadata">

**Author:** ![Nickolas\_Pohilets](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nickolas_pohilets/32/10659_2.png) [@Nickolas\_Pohilets](https://forums.swift.org/u/Nickolas_Pohilets)\
**Post date:** [November 22, 2021, 10:56am UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/25 "2021-11-22T10:56:42Z")

</div>

Are there any constraints that prevent any type from being usable for compile-time values?

One of the challenges here is heap allocation. But in theory is should be possible to emit all the objects that were heap-allocated and not deallocated during compile-time interpretation as a static variable in `.data` segment with immortal ref count.

```swift
class Foo {
    init() {
        print("Init") // Printed as a note in compiler output
    }

   deinit() {
        print("Deinit") // Never executed
   }
}

static known let x = Foo()

```

---

<div class="post-metadata">

**Author:** ![AlexisQapa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexisqapa/32/7159_2.png) [@AlexisQapa](https://forums.swift.org/u/AlexisQapa)\
**Post date:** [November 22, 2021, 11:02am UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/26 "2021-11-22T11:02:44Z")

</div>

> [@kiel](#):
>
> Unrelated question. Does this pitch finally mean I can build programs with source code like this will fail to compile if the URL cannot be initialised?
> 
> ```swift
> static const let websiteUrl: URL = URL(string: "https://swift.org")
> 
> ```

Came to post this, that would be neat !

---

<div class="post-metadata">

**Author:** ![mfilonen2](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mfilonen2/32/20284_2.png) [@mfilonen2](https://forums.swift.org/u/mfilonen2)\
**Post date:** [November 22, 2021, 2:31pm UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/27 "2021-11-22T14:31:53Z")

</div>

> [@Nickolas\_Pohilets](#):
>
> Are there any constraints that prevent any type from being usable for compile-time values?

As I understood, the first step in the proposal is just to store compile-time constants (that are assigned to literals) in the special type of variables, not to execute anything at compile-time (including any initializers or functions).

---

<div class="post-metadata">

**Author:** ![JuneBash](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/junebash/32/19323_2.png) [@JuneBash](https://forums.swift.org/u/JuneBash)\
**Post date:** [November 23, 2021, 4:08am UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/28 "2021-11-23T04:08:34Z")

</div>

Is there any reason we couldn't drop the `let` entirely?

```swift
enum Thing {
  static const thing: String = "hello"
}

```

---

<div class="post-metadata">

**Author:** ![mickeyl](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mickeyl/32/12258_2.png) [@mickeyl](https://forums.swift.org/u/mickeyl)\
**Post date:** [November 23, 2021, 7:43am UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/29 "2021-11-23T07:43:35Z")

</div>

I like that. The triple `var`, `let`, and `const` would clearly reveal the intention in my book.

---

<div class="post-metadata">

**Author:** ![Dmitriy\_Ignatyev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dmitriy_ignatyev/32/11340_2.png) [@Dmitriy\_Ignatyev](https://forums.swift.org/u/Dmitriy_Ignatyev)\
**Post date:** [November 23, 2021, 8:53am UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/30 "2021-11-23T08:53:55Z")

</div>

This is a great option in my opinion. The only thing is that `let` has already a meaning of something constant, immutable.  
But using of some new `const_keyword` instead of `let` & `var` make syntax clearer.

---

<div class="post-metadata">

**Author:** ![jazzbox](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jazzbox/32/4817_2.png) [@jazzbox](https://forums.swift.org/u/jazzbox)\
**Post date:** [November 23, 2021, 9:24am UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/31 "2021-11-23T09:24:24Z")

</div>

Just curious: in case of a library func with a const parameter, what does compile time even mean here?

---

<div class="post-metadata">

**Author:** ![Finagolfin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/finagolfin/32/34388_2.png) [@Finagolfin](https://forums.swift.org/u/Finagolfin)\
**Post date:** [November 23, 2021, 9:47am UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/32 "2021-11-23T09:47:44Z")

</div>

It would depend if the source for the library is available or not. If available, no problem, simply compile it with your own const parameters. If a binary-only library, I think you'd have to make sure to provide a public API without any const parameters.

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [November 23, 2021, 11:35pm UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/33 "2021-11-23T23:35:57Z")

</div>

literals are completely unrelated to the concept of being known at compile-time or not. for example, `String` and `Character` can only be instantiated at run time, since they depend on the ICU library. more generally, types imported from other modules can implement their own literal-initializers, which cannot run until the module has been loaded (possibly dynamically). this has important implications for types like `Decimal`s and `BigInt`s, which are often imported as shared dependencies, and may or may not be appropriately inlinable.

---

<div class="post-metadata">

**Author:** ![noahemmet](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/noahemmet/32/986_2.png) [@noahemmet](https://forums.swift.org/u/noahemmet)\
**Post date:** [November 23, 2021, 11:45pm UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/34 "2021-11-23T23:45:29Z")

</div>

Any chance we can keep the `#` as a compile-time signifier, ala `#if` and `#available`? Does `#let` read well to people?

```swift
static #let foo = “foo”
func foo(#let param: String)

```

The func parameter looks a little weird to me in light of the [deprecated](https://github.com/apple/swift-evolution/blob/master/proposals/0003-remove-var-parameters.md) `var` func parameters, but its meaning as an immutable-at-compile-time constant is otherwise clear IMO.

---

<div class="post-metadata">

**Author:** ![Jean-Daniel](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@Jean-Daniel](https://forums.swift.org/u/Jean-Daniel)\
**Post date:** [November 25, 2021, 9:48am UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/35 "2021-11-25T09:48:31Z")

</div>

> [@taylorswift](#):
>
> `String` and `Character` can only be instantiated at run time, since they depend on the ICU library.

And why this dependency on ICU prevent instantiation at compile time ? The swift compiler can also depend on ICU and create a const representation.

This is what clang does with CFString literal. The C strings are converted to UTF-16 at compile time and stored as const data.

---

<div class="post-metadata">

**Author:** ![jayton](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jayton/32/5592_2.png) [@jayton](https://forums.swift.org/u/jayton)\
**Post date:** [November 25, 2021, 11:44am UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/36 "2021-11-25T11:44:33Z")

</div>

It gets problematic if you allow compile-time operations at an (extended) grapheme cluster level (including things like getting the length of a string), because the compile-time result may be different from the runtime result. This is especially true if, like C++, there are user-defined functions that can be executed both at compile time and at runtime; the same code might produce different results depending on whether the argument passed to it is a constant.

(Note that similar differences can happen between runs of the same compiled code on different versions of ICU.)

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [November 25, 2021, 2:57pm UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/37 "2021-11-25T14:57:11Z")

</div>

Unicode is only a problem if the data tables are part of a system dependency (ICU or the standard library on Apple platforms). If you statically link those things, your view of Unicode characters is also fixed at compile-time and could in theory also be evaluated by the compiler. I believe Unicode also makes forward compatibility guarantees, although I haven't examined them in much detail and they may/may not be sufficient for everything we'd want.

One issue that I've found with Swift is that it won't statically allocate objects (i.e. serialise them in the binary); `StaticString` is basically the only exception, and even simple arrays of `RawOptionSet`s get initialised at start-time in the main function.

For example, take the percent-encoding table used by WebURL ([Godbolt](https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AKA7nioFSS%2BsgJ4BlRugDCqWgFcWDPU4AyeBkwAOU8AI0xiCQA2AFZSAAdUBUJ7BlcPLz1E5LsBf0CQlnDIrlirTBtchiECJmICdM9vLnLK1Jq6gnzgsIjouIVa%2BsbMlsHO7sLi/oBKK1R3YmR2DgBSDQBBAAF3BSZQ%2BgAxYlQWAEkGWgDMdY2AggjmWgBqQeJ3W2e5ACVfbAY0FghJhhCCQM8APLxKrAgjPVYAdgAQrdbs9njs9gdMMdThcroE0c97o8xM8AG51Z7EJgmABqYnc7C%2BFwIAA5UZt0VsAgT9vQiSTiE9iQxCBAafTGcy5Ky2TN4ciiejrFQAHSShkeTDwgDMABFqbStUyiYj9ZyNtzeQF%2BTqhSLxnZkBSqQwBMzgbQqOjfX7EUjnl6qBLjdLwRoFeaiTzLrbsaKHsKyU68C7KcRnsgNODg378%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XyBKBqAiW6AtAGBGAgBaUWD6B4DnrwBzDPZVDeW%2BQQjiUVBARVCOBxwjDNCkB%2BDXA64lAJBJApACA9VZAjVVCTC9BDXWAdXtBDANBuBNB6BzW2ALUTADUzWrWLXjVjCLXTXtYlBzDkWLDLB6CopaIBWYXYXCUOVEUcCqBshRC%2BRRAxxVUEAug6Vw4znPAQAkWjLHS4CEA0arDUUtAtRMzKVUU0UzC8DSVaAzBMUgCxBqgIgsIKnKFvXllcAsIzECUcBCWkB4UEUPXiWSV0UMVI0E1Ph3Wk1iWU0yXU1bnlogCSBAA%3D)). If you check out the `main` function, you'll see that the compiler evaluated the array enough to reduce it to a series of magic numbers, but still initialises it at runtime. For us, part of the goal of the project is to be 100% Swift and not use any C shims, and it's only 256 bytes so the overhead is small, but it is a significant reason why [the standard library's own Unicode support](https://github.com/apple/swift/pull/38922#issuecomment-908537363) has to be written in C rather than Swift.

So what I wonder is: will we be able to support compile-time constant values that allocate memory? It doesn't seem like we can today. I'm not just talking about Arrays; also user-defined types built with `ManagedBuffer` etc.

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [November 25, 2021, 5:27pm UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/38 "2021-11-25T17:27:34Z")

</div>

_Moved to a [separate thread](https://forums.swift.org/t/consistency-of-the-objectoutliner-siloptimizer-pass/53764) per request._

> **Original comment**
>
> There definitely are situations where the compiler can outline complex values directly into the data segment of the binary, but I don't know what the limitations are. For example, [this example (godbolt)](https://godbolt.org/z/n1jsv5E5e) with a simple `Int` and `String` struct gets converted to a data blob:
> 
> ```swift
> output.staticArray : [output.Foo]:
> .zero 8
> 
> mainTv_:
> .zero 8
> .zero 16
> .quad 6
> .quad 12
> .quad 10
> .quad 7234932
> .quad -2089670227099910144
> .quad 20
> .quad 133540975310708
> .quad -1873497444986126336
> .quad 30
> .quad 133541042677876
> .quad -1873497444986126336
> .quad 100
> .quad 7236850741311532655
> .quad -1513209474789907086
> .quad 1000
> .quad 8462097072821464687
> .quad -1441151879073603213
> .quad 100000
> .quad -3458764513820540908
> .quad .L__unnamed_1+9223372036854775776
> 
> .L__unnamed_1:
> .asciz "one hundred thousand"
> 
> ```
> 
> ...where the code to initialize it consists only of a type metadata lookup and then a call to `swift_initStaticObject`, which appears to be fast—it just populates the runtime metadata pointer at the beginning of the inlined data.
> 
> One cool thing about this is that I was able to use `String`s instead of `StaticString`s and it still worked—and in fact, the strings that could fit into the small string representation used that instead of using a pointer to separate character data.
> 
> I'm not sure what it is about `OptionSet`s that prevents this from working, though. I think this happens as part of the [ObjectOutliner SILOptimizer pass](https://github.com/apple/swift/blob/main/lib/SILOptimizer/Transforms/ObjectOutliner.cpp) in the compiler, so that would be the place to improve.
> 
> The other drawback about this is that since it's an optimization pass, unoptimized builds will still get the slow code path that initializes everything at runtime. I wonder if this transformation could happen as a mandatory pass, so that debug builds could still rely on static data.

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [November 29, 2021, 4:09pm UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/39 "2021-11-29T16:09:03Z")

</div>

_Moved to a [separate thread](https://forums.swift.org/t/consistency-of-the-objectoutliner-siloptimizer-pass/53764) per request._

> **Original comment**
>
> I poked at this a little bit more and the outcome was interesting (and beyond my understanding of how the optimizer works).
> 
> It turns out `OptionSet`s by themselves aren't a problem; some of them are able to outline into static objects fine. Your WebURL case does if we stop the array [after element `0x67`](https://godbolt.org/z/cj77M7zz1). But once we add [element `0x68`](https://godbolt.org/z/PnYbr1e9f), or anything after that, it stops outlining.
> 
> I looked more closely at the generated post-opt SIL and it looks like in the `0x00-0x67` case, the ObjectOutliner pass outlines the whole array into a single SIL value `mainTv_`, which is what we want.
> 
> In the `0x00-0x68` case, it looks like the individual arrays inside the larger array (the option set unions) are outlined separately (`mainTv_...mainTv7_`), and the overall array is never outlined.
> 
> Maybe the inner array outlining is an intermediate step to outlining the whole thing, and the `0x68`th element pushes the number of basic blocks or instructions past some limit that's coded into an optimizer pass that makes it claim it's too complex to analyze, causing it to break down after that?
> 
> Hopefully someone like @Erik_Eckstein might have some more insight here. I've been looking into taking advantage of `swift_allocStaticObject`-based values for other projects but if they're sensitive to small perturbations in the input like this, then that gets a lot trickier.

---

<div class="post-metadata">

**Author:** ![ben-cohen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ben-cohen/32/296_2.png) [@ben-cohen](https://forums.swift.org/u/ben-cohen)\
**Post date:** [November 29, 2021, 5:05pm UTC](https://forums.swift.org/t/pitch-compile-time-constant-values/53606/40 "2021-11-29T17:05:38Z")

</div>

This is great info to dig into but let's move this sub-thread into a topic under Compiler Dev rather than this pitch.

[Previous page](https://forums.swift.org/t/pitch-compile-time-constant-values/53606.md?page=1)

[Next page](https://forums.swift.org/t/pitch-compile-time-constant-values/53606.md?page=3)
