# Chapter 6 Linked Lists: Conformance to collection, subscript method seems problematic with a force unwrap

**URL:** <https://forums.kodeco.com/t/chapter-6-linked-lists-conformance-to-collection-subscript-method-seems-problematic-with-a-force-unwrap/205192>\
**Category:** Data Structures & Algorithms in Swift\
**Created:** [June 26, 2024, 10:26pm UTC](https://forums.kodeco.com/t/chapter-6-linked-lists-conformance-to-collection-subscript-method-seems-problematic-with-a-force-unwrap/205192 "2024-06-26T22:26:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chaugen55](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@chaugen55](https://forums.kodeco.com/u/chaugen55)\
**Post date:** [June 26, 2024, 10:26pm UTC](https://forums.kodeco.com/t/chapter-6-linked-lists-conformance-to-collection-subscript-method-seems-problematic-with-a-force-unwrap/205192/1 "2024-06-26T22:26:37Z")

</div>

In chapter 6, the authors propose a great solution to make LinkedList conform to the Collection protocol. This makes perfect sense to use a linked list with a familiar set of APIs.

I have a concern though regarding the `subscript` method declaration, which can be found here:

> <https://github.com/kodecocodes/alg-materials/blob/editions/4.0/06-linked-list/projects/finished/LinkedList.playground/Sources/LinkedList.swift>

```swift
public subscript(position: Index) -> Value {
  position.node!.value
}

```

This seems problematic because the `Index.node` has a type of `Node<Value>?`. If the node value is `nil`, then this force unwrapping will crash. Take this example:

```auto
let emptyList = LinkedList<Int>()
print(emptyList[0]) // crash, since no nodes exist yet 

```

Should we just be extra careful when using linked list subscripts, and ensure that they aren’t empty before trying to subscript? This would mirror the behavior of array subscripting out of range, but seems like a non-optimal solution for users.

---

<div class="post-metadata">

**Author:** ![robertomachorro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/robertomachorro/32/111603_2.png) [@robertomachorro](https://forums.kodeco.com/u/robertomachorro)\
**Post date:** [June 27, 2024, 12:41am UTC](https://forums.kodeco.com/t/chapter-6-linked-lists-conformance-to-collection-subscript-method-seems-problematic-with-a-force-unwrap/205192/2 "2024-06-27T00:41:58Z")

</div>

This is a good point, in general it’s good to avoid ! unless it’s with a constant.  
Hopefully the author can add to the rationale here.

---

<div class="post-metadata">

**Author:** ![exchessplayerh](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/exchessplayerh/32/110937_2.png) [@exchessplayerh](https://forums.kodeco.com/u/exchessplayerh)\
**Post date:** [July 3, 2025, 11:52am UTC](https://forums.kodeco.com/t/chapter-6-linked-lists-conformance-to-collection-subscript-method-seems-problematic-with-a-force-unwrap/205192/3 "2025-07-03T11:52:09Z")

</div>

Not the author, but as far as I am aware it is how Swift collections work in general. The default subscript is not safe - If you try to access an element at invalid index - it will crash (same as with array). But may be instead of force unwrapping, it should throw fatal error - index out of range.
