# \`toggled\` or \`isFalse\` property/method on bool for use with KeyPath APIs

**URL:** <https://forums.swift.org/t/toggled-or-isfalse-property-method-on-bool-for-use-with-keypath-apis/51464>\
**Category:** Discussion\
**Created:** [August 24, 2021, 7:44pm UTC](https://forums.swift.org/t/toggled-or-isfalse-property-method-on-bool-for-use-with-keypath-apis/51464 "2021-08-24T19:44:28Z")\
**Posts on this page:** 10\
**Page:** 3

<div class="post-metadata">

**Author:** ![Ilias\_Karim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ilias_karim/32/2339_2.png) [@Ilias\_Karim](https://forums.swift.org/u/Ilias_Karim)\
**Post date:** [September 1, 2021, 2:27pm UTC](https://forums.swift.org/t/toggled-or-isfalse-property-method-on-bool-for-use-with-keypath-apis/51464/42 "2021-09-01T14:27:36Z")

</div>

> [@Saklad5](#):
>
> Personally, I think every declaration in Swift should be accessible via key path, unless there is a compelling reason they should not be. **Operators are glorified functions, and they should be treated as such when it comes to key paths. Same with subscript methods, which would be _really_ nice for drilling down into dictionaries via key path.**

@Saklad5 you very precisely explicate the core of the issue here (emphasis mine). Your language needs to go into a proposal. I personally applaud your perspective to not only mention the benefits that adding support for operators in key paths would grant by expanding the discussion to the potential benefits of key path support for subscript methods.

---

<div class="post-metadata">

**Author:** ![ensan-hcl](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ensan-hcl/32/15186_2.png) [@ensan-hcl](https://forums.swift.org/u/ensan-hcl)\
**Post date:** [September 1, 2021, 2:48pm UTC](https://forums.swift.org/t/toggled-or-isfalse-property-method-on-bool-for-use-with-keypath-apis/51464/43 "2021-09-01T14:48:36Z")

</div>

What are _subscript methods_? I think key paths for subscripts are already supported as ` ["Swift": 5.5][keyPath: \.["Swift"]]` works.

---

<div class="post-metadata">

**Author:** ![Ilias\_Karim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ilias_karim/32/2339_2.png) [@Ilias\_Karim](https://forums.swift.org/u/Ilias_Karim)\
**Post date:** [September 1, 2021, 3:28pm UTC](https://forums.swift.org/t/toggled-or-isfalse-property-method-on-bool-for-use-with-keypath-apis/51464/45 "2021-09-01T15:28:44Z")

</div>

Yes, key path subscripts works as expected.

I believe the author @danhalliday was referring to these when mentioning subscript methods.

```swift
extension Collection where Indices.Iterator.Element == Index {

  subscript (safe index: Index) -> Iterator.Element? {
      return indices.contains(index) ? self[index] : nil
  }
}

let x = [1, 2, 3]

print(x[keyPath: <#...#>])

```

I always forget that replies don't appear as replies when they are first posted, thus the deleted previous comment. Someone link me to the discussion forum JIRA I'll file a ticket. 😃

---

<div class="post-metadata">

**Author:** ![ensan-hcl](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ensan-hcl/32/15186_2.png) [@ensan-hcl](https://forums.swift.org/u/ensan-hcl)\
**Post date:** [September 1, 2021, 3:35pm UTC](https://forums.swift.org/t/toggled-or-isfalse-property-method-on-bool-for-use-with-keypath-apis/51464/46 "2021-09-01T15:35:23Z")

</div>

I'm still confused, sorry. Isn't `x[keyPath: \.[safe: 2]]` enough? Or else, are you talking about unapplied subscript as object, like `x[keyPath: \.[safe:]](2)` (not possible now)?

---

<div class="post-metadata">

**Author:** ![Saklad5](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Saklad5](https://forums.swift.org/u/Saklad5)\
**Post date:** [September 1, 2021, 8:02pm UTC](https://forums.swift.org/t/toggled-or-isfalse-property-method-on-bool-for-use-with-keypath-apis/51464/47 "2021-09-01T20:02:31Z")

</div>

I believe it is only possible to use subscript methods in key paths terminally.

---

<div class="post-metadata">

**Author:** ![Saklad5](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Saklad5](https://forums.swift.org/u/Saklad5)\
**Post date:** [September 1, 2021, 8:04pm UTC](https://forums.swift.org/t/toggled-or-isfalse-property-method-on-bool-for-use-with-keypath-apis/51464/48 "2021-09-01T20:04:27Z")

</div>

What you want is the `!` operator, which already exists. You should not be using anything else for negation.

---

<div class="post-metadata">

**Author:** ![Ilias\_Karim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ilias_karim/32/2339_2.png) [@Ilias\_Karim](https://forums.swift.org/u/Ilias_Karim)\
**Post date:** [September 3, 2021, 6:02pm UTC](https://forums.swift.org/t/toggled-or-isfalse-property-method-on-bool-for-use-with-keypath-apis/51464/49 "2021-09-03T18:02:33Z")

</div>

You are correct. The first expression you posted works. Here it is in an example,

```swift
struct Struct {
  var value: Int
}

let x = [Struct(value: 1), Struct(value: 2), Struct(value: 3)]

x[keyPath: \.[safe: 2]?.value] // 3

```

@Saklad5 does this not provide your need? Or were you looking for an expression like, to quote @ensan, ` x[keyPath: \.[safe:]](2)` to work?

---

<div class="post-metadata">

**Author:** ![Saklad5](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Saklad5](https://forums.swift.org/u/Saklad5)\
**Post date:** [September 3, 2021, 7:00pm UTC](https://forums.swift.org/t/toggled-or-isfalse-property-method-on-bool-for-use-with-keypath-apis/51464/50 "2021-09-03T19:00:22Z")

</div>

I’m clearly confused about where you can use subscript key paths. I’m fairly sure you can’t, for instance, reference a value inside a dictionary that is itself a value inside a dictionary. Not in one expression, at least.

---

<div class="post-metadata">

**Author:** ![ensan-hcl](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ensan-hcl/32/15186_2.png) [@ensan-hcl](https://forums.swift.org/u/ensan-hcl)\
**Post date:** [September 4, 2021, 12:06am UTC](https://forums.swift.org/t/toggled-or-isfalse-property-method-on-bool-for-use-with-keypath-apis/51464/51 "2021-09-04T00:06:47Z")

</div>

`["Swift": ["5.5": 5.5]][keyPath: \.["Swift"]?["5.5"]]` works.  
But I know there are cases subscript key paths cause compile error saying "subscript index of type 'T in a key path must be `Hashable`". Maybe this?

```swift
class A { 
    subscript(_ a: A) -> Int { 42 } 
}
A()[keyPath: \.[A()]]

```

---

<div class="post-metadata">

**Author:** ![Ilias\_Karim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ilias_karim/32/2339_2.png) [@Ilias\_Karim](https://forums.swift.org/u/Ilias_Karim)\
**Post date:** [September 8, 2021, 7:51pm UTC](https://forums.swift.org/t/toggled-or-isfalse-property-method-on-bool-for-use-with-keypath-apis/51464/52 "2021-09-08T19:51:57Z")

</div>

You can reference a value inside a dictionary that is itself a value inside a dictionary.

```swift
let dictionary = ["x": ["y": "z"]]
dictionary[keyPath: \.["x"]?["y"]] // z

```

As for @ensan's example, you can get around the error by using a standard library type like NSObject.

```swift

class A: NSObject {
    subscript(_ a: A) -> Int { 42 }
}
A()[keyPath: \.[A()]] // 42

```

This was due to the implementation of [SE-0188](https://github.com/apple/swift-evolution/blob/main/proposals/0188-stdlib-index-types-hashable.md).

[Previous page](https://forums.swift.org/t/toggled-or-isfalse-property-method-on-bool-for-use-with-keypath-apis/51464.md?page=2)
