# It's 2025, can we have a “lazy let”?

**URL:** <https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403>\
**Category:** Using Swift\
**Created:** [April 20, 2025, 2:35pm UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403 "2025-04-20T14:35:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![miku1958](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/miku1958/32/11936_2.png) [@miku1958](https://forums.swift.org/u/miku1958)\
**Post date:** [April 20, 2025, 2:35pm UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/1 "2025-04-20T14:35:05Z")

</div>

As the title suggests, I don't understand why it's so difficult to implement, but my understanding is that the compiler only needs to add the rule to disable the setter on “lazy let” for developers and the rest is just consistent with “lazy var”.

---

<div class="post-metadata">

**Author:** ![harlanhaskins](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/harlanhaskins/32/333_2.png) [@harlanhaskins](https://forums.swift.org/u/harlanhaskins)\
**Post date:** [April 20, 2025, 3:03pm UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/2 "2025-04-20T15:03:13Z")

</div>

One of the things that's somewhat implied by a `var` of a struct is the concept that a mutation of part of a struct is considered a mutation of the entire value. Ergo, a `lazy let` mutating would cause `didSet`s to execute where the struct is stored.

Consider:

```swift
struct MyStruct {
    lazy let a = Something()
}

```

Passing that to a function may cause `a` to be realized, which would be considered a mutation. As such, all uses of a `lazy` let would be considered `mutating` and need to be marked as such. This struct would need to be passed `inout` still.

I could see it for classes though. Arguably, this should be done in a property wrapper nowadays instead of a special modifier applied to the property.

---

<div class="post-metadata">

**Author:** ![miku1958](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/miku1958/32/11936_2.png) [@miku1958](https://forums.swift.org/u/miku1958)\
**Post date:** [April 20, 2025, 3:31pm UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/3 "2025-04-20T15:31:53Z")

</div>

I'm open to any way to implement it, lazy and @lazy are fine, but given the consistency it's definitely better to be lazy.

As the topic says, lazy let should be implemented the same as lazy var, so the restrictions on sturct should be the same and Since developers are prohibited from using setters, willSet and didSet should also be prohibited.

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [April 20, 2025, 7:03pm UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/4 "2025-04-20T19:03:41Z")

</div>

This is very closely related to [the recent discussion about `weak let`](https://forums.swift.org/t/pitch-weak-let/79271).

Swift has evolved such that `let` and `var` really refer to ownership, not mutability. A `let` variable/property only exposes operations that can be done with shared ownership. A `var` variable/property can also expose operations which require exclusive ownership. Since a `lazy` variable mutates its storage on first access, it must be a `var`.

Atomics are the exception that proves this rule: they must be declared `let`, but they support mutating operations. This is because a mutating atomic operation is defined to not require exclusive ownership. Shared mutability is the whole point of atomics, after all. But it does mean if your type contains a public atomic property, you can no longer rely on the law of exclusivity to protect that value’s in-memory representation.

`lazy` is not currently thread-safe, but a hypothetical thread-safe version of `lazy` could theoretically be declared `let`. But that would significantly expand the aforementioned hole in the law of exclusivity. The Language Workgroup argued that anyone writing an atomic variable is opting into the paradox of mutability under non-exclusive ownership. I doubt this would be considered true of `lazy let`.

---

<div class="post-metadata">

**Author:** ![miku1958](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/miku1958/32/11936_2.png) [@miku1958](https://forums.swift.org/u/miku1958)\
**Post date:** [April 20, 2025, 11:05pm UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/5 "2025-04-20T23:05:07Z")

</div>

Hi @ksluder, you're missing my point, which was: why can't a lazy let be equivalent to a lazy var that disables setters?

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [April 20, 2025, 11:05pm UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/6 "2025-04-20T23:05:46Z")

</div>

One difference between ‘weak let’ and a hypothetical ‘lazy let’ is that the latter requires mutable ownership over the value to read the property while the former does not. So this might be more confusing to teach, for sure.

At the implementation level, we could make ‘lazy let’ work easily enough; it would still desugar to a computed property with a mutable getter, and it would continue to behave as one, so nothing would violate the law of exclusivity.

However, in my opinion, this use case would be better serviced by macros, instead of generalizing either lazy properties or property wrappers.

---

<div class="post-metadata">

**Author:** ![duan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/duan/32/40252_2.png) [@duan](https://forums.swift.org/u/duan)\
**Post date:** [April 20, 2025, 11:14pm UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/7 "2025-04-20T23:14:48Z")

</div>

> [@Slava\_Pestov](#):
>
> it would still desugar to a computed property with a mutable getter

I wonder why the OP needs a `let` in the first place. If the complier allows `lazy let` the spelling, but accessing the property is still considered mutating, then what does this syntax unlock? Would this property be accepted in any call site that wouldn't have been accepted if it was a `var`? Perhaps OP can provide some examples to motivate this change.

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [April 20, 2025, 11:19pm UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/8 "2025-04-20T23:19:39Z")

</div>

> [@miku1958](#):
>
> Hi @ksluder, you're missing my point, which was: why can't a lazy let be equivalent to a lazy var that disables setters?

I directly addressed that question, but I’ll restate it more succinctly: Even without exposing a setters, a lazy _getter_ modifies the containing storage on first access. This requires exclusive access to the storage, which Swift models as `var` for all types except atomics.

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [April 20, 2025, 11:25pm UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/9 "2025-04-20T23:25:05Z")

</div>

> [@Slava\_Pestov](#):
>
> At the implementation level, we could make ‘lazy let’ work easily enough; it would still desugar to a computed property with a mutable getter, and it would continue to behave as one, so nothing would violate the law of exclusivity.

I view the Law of Exclusivity as encompassing more than just the prohibition of more than one simultaneous exclusive access, but also what you are allowed to do with exclusive or non-exclusive accesses. Perhaps those are officially separate rules or corollaries.

Either way, it was originally not permissible to call a mutating method via non-exclusive access. (Methods on classes are never mutating.) That changed with the introduction of atomics. Permitting `lazy let` would require changing it yet again.

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [April 21, 2025, 12:04am UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/10 "2025-04-21T00:04:01Z")

</div>

> [@ksluder](#):
>
> Either way, it was originally not permissible to call a mutating method via non-exclusive access. (Methods on classes are never mutating.) That changed with the introduction of atomics. Permitting `lazy let` would require changing it yet again.

A ‘lazy let’ would still require exclusive access to call the getter. You can think of the law of exclusivity as a statement about the immutable “platonic ideal” world of language semantics, whereas the ‘let’ keyword is just sugar that we can interpret in any way we want (as long as it’s self-consistent at the very least, but hopefully also easy to explain).

---

<div class="post-metadata">

**Author:** ![miku1958](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/miku1958/32/11936_2.png) [@miku1958](https://forums.swift.org/u/miku1958)\
**Post date:** [April 21, 2025, 12:40am UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/11 "2025-04-21T00:40:33Z")

</div>

My understanding is that a lazy let is more of a way to tell the developer not to modify the variable under any circumstances, and that it is up to the compiler to figure out how to implement it.

The effect of a lazy let on the developer should be the same as a lazy var that disables (or hides) the setter. Currently we can do this with a private(set) lazy var, but that doesn't disable modifying the variable in the type it's in.

So to get back to my question, why can't we make a lazy let equivalent to a lazy var with the setter disabled at the compiler level?

---

<div class="post-metadata">

**Author:** ![miku1958](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/miku1958/32/11936_2.png) [@miku1958](https://forums.swift.org/u/miku1958)\
**Post date:** [May 5, 2025, 12:27am UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/12 "2025-05-05T00:27:54Z")

</div>

> <https://github.com/swiftlang/swift-evolution/blob/main/proposals/0481-weak-let.md>

I realized that weak let is in swift-evolution, does that mean that lazy let is also a possible thing?

---

<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:** [May 5, 2025, 12:35am UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/13 "2025-05-05T00:35:21Z")

</div>

> [@ksluder](#):
>
> Either way, it was originally not permissible to call a mutating method via non-exclusive access. (Methods on classes are never mutating.) That changed with the introduction of atomics.

Atomic operations are not formally mutating, either. They behave like methods on classes.

> **[add(\_:ordering:) | Apple Developer Documentation](https://developer.apple.com/documentation/synchronization/atomic/add(_:ordering:)-1k1sq)**
>
> Perform an atomic add operation and return the old and new value, applying the specified memory ordering.

Or do you mean something else?

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [May 5, 2025, 12:51am UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/14 "2025-05-05T00:51:13Z")

</div>

> [@Slava\_Pestov](#):
>
> However, in my opinion, this use case would be better serviced by macros, instead of generalizing either lazy properties or property wrappers.

Related Q: is there any reason why the current `lazy` keyword couldn't be re-implemented as a property wrapper or macro?

---

<div class="post-metadata">

**Author:** ![miku1958](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/miku1958/32/11936_2.png) [@miku1958](https://forums.swift.org/u/miku1958)\
**Post date:** [May 5, 2025, 1:59am UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/16 "2025-05-05T01:59:26Z")

</div>

I think it's because none of them support capturing themselves in the initial stages now. There would be more work needed to implement lazy with them?

---

<div class="post-metadata">

**Author:** ![Ling\_Wang](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ling_wang/32/2691_2.png) [@Ling\_Wang](https://forums.swift.org/u/Ling_Wang)\
**Post date:** [July 31, 2025, 4:25pm UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/17 "2025-07-31T16:25:09Z")

</div>

I could understand there are challenges in implementing `lazy let` and I think most of us will be ok that `lazy let` is not the same as `let` and will have limitations such as it has same data race issues as `var`.

What most Swift users care about is: sometimes I need to initialize a property later than the initializer but I still want it to be only initialized once and immutable afterwards.

---

<div class="post-metadata">

**Author:** ![andrekandore](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andrekandore/32/2292_2.png) [@andrekandore](https://forums.swift.org/u/andrekandore)\
**Post date:** [August 3, 2025, 3:55am UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/18 "2025-08-03T03:55:48Z")

</div>

> [@Ling\_Wang](#):
>
> What most Swift users care about is: sometimes I need to initialize a property later than the initializer but I still want it to be only initialized once and immutable afterwards.

something like `deferred let` ?

---

<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:** [August 3, 2025, 9:57am UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/19 "2025-08-03T09:57:21Z")

</div>

> [@Slava\_Pestov](#):
>
> At the implementation level, we could make ‘lazy let’ work easily enough; it would still desugar to a computed property with a mutable getter, and it would continue to behave as one, so nothing would violate the law of exclusivity.

Can we implement computed getter using `dispatch_once`, similar to `static let`? Then it should allow concurrent access and thus be considered immutable. But it still requires identity, so I would allow `lazy let` only inside classes.

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [August 3, 2025, 10:14am UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/20 "2025-08-03T10:14:30Z")

</div>

> [@Nickolas\_Pohilets](#):
>
> Can we implement computed getter using `dispatch_once`, similar to `static let`? Then it should allow concurrent access and thus be considered immutable. But it still requires identity, so I would allow `lazy let` only inside classes.

You can probably cook this up with property wrappers or macros already, there’s no real reason to bake it into the language.

---

<div class="post-metadata">

**Author:** ![michelf](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michelf/32/1180_2.png) [@michelf](https://forums.swift.org/u/michelf)\
**Post date:** [August 3, 2025, 11:27am UTC](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403/21 "2025-08-03T11:27:32Z")

</div>

Implementing it with a property wrapper makes it hard to access `self` in the initializer, the main reason why I use `lazy var`. Migrating to Swift Concurrency would be easier with `lazy let` for the same reasons `weak let` was added recently.

While I'm not in a rush to get that feature, I do think there is a need here.

> [@Slava\_Pestov](#):
>
> You can probably cook this up with property wrappers or macros already, there’s no real reason to bake it into the language.

"Build your own synchronization primitives" is not a very encouraging answer for a language that wants to do away with concurrency bugs. Having a safe and language-approved solution for `dispatch_once` (one that is simply not tied to a global like `static let` has to be) seems like a basic building block that is missing.

[Next page](https://forums.swift.org/t/its-2025-can-we-have-a-lazy-let/79403.md?page=2)
