# How do you get labeled associated values from Mirror children?

**URL:** <https://forums.swift.org/t/how-do-you-get-labeled-associated-values-from-mirror-children/36377>\
**Category:** Using Swift\
**Created:** [May 11, 2020, 5:20pm UTC](https://forums.swift.org/t/how-do-you-get-labeled-associated-values-from-mirror-children/36377 "2020-05-11T17:20:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon9791410](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon9791410](https://forums.swift.org/u/anon9791410)\
**Post date:** [May 11, 2020, 5:20pm UTC](https://forums.swift.org/t/how-do-you-get-labeled-associated-values-from-mirror-children/36377/1 "2020-05-11T17:20:56Z")

</div>

Every schoolchild knows that you `Cannot create a single-element tuple with an element label`.

But this returns `(label: ()).Type`. How do we cast that?!

```swift
( Mirror(
    reflecting: Enum.labeled( label: () )
  ).children.first?.value
).map(Mirror.init)?.subjectType

```

You can dig into that [with another mirror](https://stackoverflow.com/a/61734859/652038). Can that be avoided?

```swift
enum Enum {
  case unlabeled(Void)
  case tuple(label0: Void, label1: Void)
  case labeled(label: Void)

  func getAssociatedValue<AssociatedValue>(_: AssociatedValue.Type) -> AssociatedValue? {
    Mirror(reflecting: self).children.first?.value as? AssociatedValue
  }
}

Enum.unlabeled( () ).getAssociatedValue(Void.self) // ()
Enum.tuple( label0: (), label1: () ).getAssociatedValue( (Void, Void).self ) // ((), ())
Enum.labeled( label: () ).getAssociatedValue(Void.self) // nil

```

---

<div class="post-metadata">

**Author:** ![Jens](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jens/32/12903_2.png) [@Jens](https://forums.swift.org/u/Jens)\
**Post date:** [May 11, 2020, 8:17pm UTC](https://forums.swift.org/t/how-do-you-get-labeled-associated-values-from-mirror-children/36377/2 "2020-05-11T20:17:26Z")

</div>

Ah, `Mirror` is giving us a value of a type that is not meant to exist in Swift, thus we cannot cast it from `Any` to its actual / runtime type (a labeled single element tuple).

```swift
enum E {
  case foo(bar: Int)
}

let e: E = .foo(bar: 123)

let m = Mirror(reflecting: e)
let v = m.children.first!.value
print(v) // (bar: 123) <--. This value and its type is
print(type(of: v)) // (bar: Int) <--' not meant to exist in Swift!

```

So this is a bug.  
But how should it be fixed?

Should labeled associated values of one element be represented without their label while the ones of two or more elements keep their labels?

---

<div class="post-metadata">

**Author:** ![anon9791410](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon9791410](https://forums.swift.org/u/anon9791410)\
**Post date:** [May 12, 2020, 1:15am UTC](https://forums.swift.org/t/how-do-you-get-labeled-associated-values-from-mirror-children/36377/3 "2020-05-12T01:15:32Z")

</div>

I see that you've put a lot of good thought into this! [Roadmap for and state of single element tuple types?](https://forums.swift.org/t/roadmap-for-and-state-of-single-element-tuple-types/13711)

I'd be fine with this as is, as long as it could be cast to not have the label, the same as other tuples.

---

<div class="post-metadata">

**Author:** ![Jens](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jens/32/12903_2.png) [@Jens](https://forums.swift.org/u/Jens)\
**Post date:** [May 12, 2020, 4:02am UTC](https://forums.swift.org/t/how-do-you-get-labeled-associated-values-from-mirror-children/36377/4 "2020-05-12T04:02:30Z")

</div>

> [@anon9791410](#):
>
> I'd be fine with this as is, as long as it could be cast to not have the label, the same as other tuples.

I'm not sure what you mean. You'd be fine with keeping the bug as is, as long as another bug is introduced (the ability to cast a non-existing type to an existing type)?

---

<div class="post-metadata">

**Author:** ![anon9791410](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon9791410](https://forums.swift.org/u/anon9791410)\
**Post date:** [May 12, 2020, 5:08am UTC](https://forums.swift.org/t/how-do-you-get-labeled-associated-values-from-mirror-children/36377/5 "2020-05-12T05:08:07Z")

</div>

I bet you actually are sure what I mean, and you just don't agree with me. 😝

It doesn't matter if we can't use the type, if we can cast it to something we can use. It's the same as throwing out argument labels when you store a function as a closure.

As I said in the other thread, I hate that we don't have single-element labeled tuples across the board, but I'm happy that they're in there somewhere. It shows that they're useful and will inspire somebody to fix the situation someday.

---

<div class="post-metadata">

**Author:** ![Jens](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jens/32/12903_2.png) [@Jens](https://forums.swift.org/u/Jens)\
**Post date:** [May 12, 2020, 5:44am UTC](https://forums.swift.org/t/how-do-you-get-labeled-associated-values-from-mirror-children/36377/6 "2020-05-12T05:44:38Z")

</div>

No, I wasn't actually sure what you meant, thanks for clarifying.

* * *

> [@anon9791410](#):
>
> It doesn't matter if we can't use the type, if we can cast it to something we can use. It's the same as throwing out argument labels when you store a function as a closure.

I simply think that if single element tuples are banned (as they currently are), they shouldn't be in the language.

* * *

> [@anon9791410](#):
>
> I hate that we don't have single-element labeled tuples across the board, but I'm happy that they're in there somewhere. It shows that they're useful and will inspire somebody to fix the situation someday.

OK, yes I agree that single element tuples (labeled and unlabeled) _should_ be allowed. Banning them is just an exception that causes confusion (and creates unfortunate situations like the one in the OP).

Allowing them doesn't seem totally impossible:

> [@Single-element labeled tuples?](https://forums.swift.org/t/single-element-labeled-tuples/9797/21):
>
> They were banned because the “classic” function-argument-tuple model and tuple conversion rules caused type system problems, particularly because every `T` was implicitly convertible to `(label: T)` and back. We’ve since shored up the argument label model, and various people have discussed limiting the tuple conversion rules to be less unpredictable and less straining on the type checker. If we solve that problem, then there’s no technical reason remaining for single-element tuples to be banned.

* * *

Since tuples are not the only things in Swift which happen to be written using parentheses, I guess unlabeled single element tuples would have to be written as eg:

```swift
let foo = (_: 123)
print(type(of: foo)) // (_: Int)

```

or

```swift
let foo = (123,)
print(type(of: foo)) // (Int,)

```
