# Closure obtained from reflection cannot properly address input parameters

**URL:** <https://forums.swift.org/t/closure-obtained-from-reflection-cannot-properly-address-input-parameters/54304>\
**Category:** Standard Library\
**Tags:** bug, reflection\
**Created:** [December 27, 2021, 12:48pm UTC](https://forums.swift.org/t/closure-obtained-from-reflection-cannot-properly-address-input-parameters/54304 "2021-12-27T12:48:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nalexn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nalexn/32/9624_2.png) [@nalexn](https://forums.swift.org/u/nalexn)\
**Post date:** [December 27, 2021, 12:48pm UTC](https://forums.swift.org/t/closure-obtained-from-reflection-cannot-properly-address-input-parameters/54304/1 "2021-12-27T12:48:08Z")

</div>

Given this trivial code:

```swift
struct Container {
    let closure: (Int) -> Void
}

let container = Container { param in
    print("\(param)")
}

```

One can assume calling `container.closure(42)` will print out `42`, which is true.

However, if this same closure is retrieved from the `container`'s `Mirror`:

```swift
let mirror = Mirror(reflecting: container)
let closure = mirror.children
    .first(where: { $0.label == "closure" })!
    .value as! ((Int) -> Void)

```

... then calling `closure(42)` distorts the parameter's value, and it prints out `6166589480`.

The same thing happens if you use `String` instead of `Int`, and I assume with other types too. If I pass a reference to an object, expectedly that reference gets messed up as well and I get `EXC_BAD_ACCESS` when trying to access the object.

Is this a bug in the Standard Library or am I missing something?

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [December 27, 2021, 1:18pm UTC](https://forums.swift.org/t/closure-obtained-from-reflection-cannot-properly-address-input-parameters/54304/2 "2021-12-27T13:18:20Z")

</div>

Looks like a bug. As a workaround wrapping closure into its own type works for me:

```
struct Closure {
    let proc: (Int) -> Void
}

struct Container {
    let closure: Closure
}

let container = Container(closure: Closure { param in
    print("\(param)")
})

container.closure.proc(42)
let mirror = Mirror(reflecting: container)
let closure = mirror.children
    .first(where: { $0.label == "closure" })!
    .value as! Closure
closure.proc(42)
print()

```

---

<div class="post-metadata">

**Author:** ![nalexn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nalexn/32/9624_2.png) [@nalexn](https://forums.swift.org/u/nalexn)\
**Post date:** [December 27, 2021, 4:52pm UTC](https://forums.swift.org/t/closure-obtained-from-reflection-cannot-properly-address-input-parameters/54304/3 "2021-12-27T16:52:15Z")

</div>

Unfortunately, I don't have access to the `Container`'s sources, which is the main reason I had to appeal to reflection in the first place. Submitted a [radar](https://openradar.appspot.com/FB9823642).

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [December 27, 2021, 5:21pm UTC](https://forums.swift.org/t/closure-obtained-from-reflection-cannot-properly-address-input-parameters/54304/4 "2021-12-27T17:21:42Z")

</div>

I see. If you have a good understanding what Container type has you can create your version of it, typecast and call through your copy without the need of using mirroring API:

```
struct Container {
    let closure: (Int) -> Void
}

var container = Container { param in
    print("\(param)")
}

container.closure(42)

struct ContainerCopy {
    let closure: (Int) -> Void
}

withUnsafeBytes(of: &container) { p in
    let r = p.baseAddress!.assumingMemoryBound(to: ContainerCopy.self).pointee
    r.closure(42)
}
print()

```

Workarounds aside it would be interesting if someone from standard library / compiler team comment on this bug.
