# \#subscripts

**URL:** https://forums.swift.org/tag/subscripts/228.md

[Latest](https://forums.swift.org/latest.md) · [Categories](https://forums.swift.org/categories.md) · [Tags](https://forums.swift.org/tags.md)

---

## [About the multi-element subscript operator for collections](https://forums.swift.org/t/about-the-multi-element-subscript-operator-for-collections/79222)

<div class="topic-metadata">

**Author:** [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Replies:** 0\
**Last updated:** [April 10, 2025, 9:59pm UTC](https://forums.swift.org/t/about-the-multi-element-subscript-operator-for-collections/79222 "2025-04-10T21:59:21Z")

</div>

This is about collections that conform to MutableCollection. If the collection type does not conform to RangeReplaceableCollection, does the newValue have to have the same length as subscripted range?

---

## [Any reason why \`@discardableResult\` can’t be applied to subscript function?](https://forums.swift.org/t/any-reason-why-discardableresult-can-t-be-applied-to-subscript-function/75553)

<div class="topic-metadata">

**Author:** [@trojan](https://forums.swift.org/u/trojan)\
**Replies:** 7\
**Last updated:** [October 24, 2024, 12:32pm UTC](https://forums.swift.org/t/any-reason-why-discardableresult-can-t-be-applied-to-subscript-function/75553 "2024-10-24T12:32:00Z")

</div>

I tried adding the @discardableResult attribute to a subscript function in my open-source library GitHub - willonboy/ZTChain: ZTChain is a lightweight library designed to facilitate chaining syntax in Swift, making your …

---

## [Throwing subscript setters](https://forums.swift.org/t/throwing-subscript-setters/73224)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 1\
**Last updated:** [July 17, 2024, 5:43am UTC](https://forums.swift.org/t/throwing-subscript-setters/73224 "2024-07-17T05:43:06Z")

</div>

What is the reason behind prohibiting throwing subscript setters? struct S { subscript(c c: Int) -\> Int { get { 0 } set throws {} // 🛑 } subscript(d d: Int) -\> Int { get throws { 0 } …

---

## [Cannot use subscripts to modify @MainActor Dictionaries…?](https://forums.swift.org/t/cannot-use-subscripts-to-modify-mainactor-dictionaries/69522)

<div class="topic-metadata">

**Author:** [@wadetregaskis](https://forums.swift.org/u/wadetregaskis)\
**Replies:** 4\
**Last updated:** [January 19, 2024, 5:45am UTC](https://forums.swift.org/t/cannot-use-subscripts-to-modify-mainactor-dictionaries/69522 "2024-01-19T05:45:14Z")

</div>

@MainActor struct Example { var dict: \[String: String\] = \[:\] mutating func demonstrateBug() { Task.detached { await MainActor.run { self.dict\["Oh"\] = "no" } …

---

## [New keypath subscript](https://forums.swift.org/t/new-keypath-subscript/65495)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 2\
**Last updated:** [June 16, 2023, 2:16pm UTC](https://forums.swift.org/t/new-keypath-subscript/65495 "2023-06-16T14:16:22Z")

</div>

If I override: subscript\<T\>(keyPath keyPath: WritableKeyPath\<Self, T\>) -\> T { ... } it won't be called when I'm getting/setting fields directly. Could we have another subscript in addition, say: subscript\<T\>(proxy ke…

---

## [dynamicMemberLookup bug?](https://forums.swift.org/t/dynamicmemberlookup-bug/65593)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 2\
**Last updated:** [June 15, 2023, 1:14pm UTC](https://forums.swift.org/t/dynamicmemberlookup-bug/65593 "2023-06-15T13:14:52Z")

</div>

I have this struct: struct Foo { var name: String = "hello" var count: Int = 42 } When I add @dynamicMemberLookup on it compiler rightfully complains: So, I've added the relevant subscript implementation: @d…

---

## [Type constraint resolver issue with subscript function in SEMA? or some standard library magic?](https://forums.swift.org/t/type-constraint-resolver-issue-with-subscript-function-in-sema-or-some-standard-library-magic/61832)

<div class="topic-metadata">

**Author:** [@carlos42421](https://forums.swift.org/u/carlos42421)\
**Replies:** 0\
**Last updated:** [December 5, 2022, 1:12pm UTC](https://forums.swift.org/t/type-constraint-resolver-issue-with-subscript-function-in-sema-or-some-standard-library-magic/61832 "2022-12-05T13:12:04Z")

</div>

This is again one of those "what section do I put it in?" questions. I have a modified Array in my standard library that subscripts (and conforms to Collection) with a different Index type called ArrayIndex that's a sim…

---

## [String Extension with Full Subscript Support](https://forums.swift.org/t/string-extension-with-full-subscript-support/35498)

<div class="topic-metadata">

**Author:** [@Peter-Schorn](https://forums.swift.org/u/Peter-Schorn)\
**Replies:** 10\
**Last updated:** [August 28, 2022, 3:40am UTC](https://forums.swift.org/t/string-extension-with-full-subscript-support/35498 "2022-08-28T03:40:25Z")

</div>

I've written an extension for String that provides getter and setter subscripts for indices, open ranges, closed ranges, PartialRangeFrom, PartialRangeThrough, and PartialRangeUpTo. The extension even supports passing in…

---

## [Why the swift compiler behaves this way?](https://forums.swift.org/t/why-the-swift-compiler-behaves-this-way/51216)

<div class="topic-metadata">

**Author:** [@Eyad](https://forums.swift.org/u/Eyad)\
**Replies:** 3\
**Last updated:** [August 14, 2021, 10:30pm UTC](https://forums.swift.org/t/why-the-swift-compiler-behaves-this-way/51216 "2021-08-14T22:30:17Z")

</div>

have code that resembles this I created a custom subscript to do the unwrapping for me to make things easier. I know that there is a safer way to do this by creating a safe subscript that returns a default value instea…

---

## [Mutating \`\[T\]?\` with a default value, or: returning inout, or: extending Any](https://forums.swift.org/t/mutating-t-with-a-default-value-or-returning-inout-or-extending-any/40593)

<div class="topic-metadata">

**Author:** [@jtbandes](https://forums.swift.org/u/jtbandes)\
**Replies:** 9\
**Last updated:** [September 24, 2020, 10:15pm UTC](https://forums.swift.org/t/mutating-t-with-a-default-value-or-returning-inout-or-extending-any/40593 "2020-09-24T22:15:53Z")

</div>

Problem struct Foo { var items: \[Int\]? } var foo = Foo() Now I want to add something to foo.items. But because it's optional, I can't do foo.items.append(3): I first need to provide a default value of \[\]. Workarounds …

---

## [Wait, empty subscripts are legal?](https://forums.swift.org/t/wait-empty-subscripts-are-legal/39409)

<div class="topic-metadata">

**Author:** [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Replies:** 3\
**Last updated:** [August 15, 2020, 6:58am UTC](https://forums.swift.org/t/wait-empty-subscripts-are-legal/39409 "2020-08-15T06:58:42Z")

</div>

Continuing the discussion from SE-0287: Extend implicit member syntax to cover chains of member references: Saw this in example code from the proposal document referenced in the original thread: struct Foo { static…

---

## [Checking out-of-bounds access on compile time in SIL](https://forums.swift.org/t/checking-out-of-bounds-access-on-compile-time-in-sil/27064)

<div class="topic-metadata">

**Author:** [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Replies:** 24\
**Last updated:** [July 23, 2019, 9:09pm UTC](https://forums.swift.org/t/checking-out-of-bounds-access-on-compile-time-in-sil/27064 "2019-07-23T21:09:50Z")

</div>

I was working on diagnosing out-of-bounds subscript access as part of parsing subscripts inside array literals, however @jrose told me it would be better to have a more general, dataflow based check in SIL that could cat…

---

## [Pitch: Static and class subscripts](https://forums.swift.org/t/pitch-static-and-class-subscripts/21850)

<div class="topic-metadata">

**Author:** [@beccadax](https://forums.swift.org/u/beccadax)\
**Replies:** 41\
**Last updated:** [March 28, 2019, 2:10pm UTC](https://forums.swift.org/t/pitch-static-and-class-subscripts/21850 "2019-03-28T14:10:36Z")

</div>

Over the last few days I've put together a prototype of static subscripts, which allow you to add subscripts that are applied to a type, rather than an instance. There's a lot of work to be done, but so far, it's looking…
