# @FunctionalProtocol – Zero-cost closure wrappers for SAM protocols

**URL:** <https://forums.swift.org/t/functionalprotocol-zero-cost-closure-wrappers-for-sam-protocols/88280>\
**Category:** Related Projects\
**Tags:** protocols, propertywrapper, macros, sam, compile-time\
**Created:** [July 14, 2026, 11:04am UTC](https://forums.swift.org/t/functionalprotocol-zero-cost-closure-wrappers-for-sam-protocols/88280 "2026-07-14T11:04:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodebyCR](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/codebycr/32/34814_2.png) [@CodebyCR](https://forums.swift.org/u/CodebyCR)\
**Post date:** [July 14, 2026, 11:04am UTC](https://forums.swift.org/t/functionalprotocol-zero-cost-closure-wrappers-for-sam-protocols/88280/1 "2026-07-14T11:04:10Z")

</div>

Hey Swift Community,

Based on some other topics:

- [FunctionalProtocols for Swift](https://forums.swift.org/t/functionalprotocols-for-swift/71093)

- [Anonymous Structs](https://forums.swift.org/t/anonymous-structs/31424)

- [Merging Closure into Protocols](https://forums.swift.org/t/merging-closure-into-protocols/71499)

I built a ⁠[@FunctionalProtocol](https://github.com/CodebyCR/Functional-Protocol)⁠ macro that generates zero-cost wrapper structures for Single Abstract Method (SAM) protocols.

Currently, this is a **proof of concept** , so please let me know if you feel like you are missing something, or if you think this specific use case is too much of an edge case to warrant its own macro.

How it can look like:

```swift
@FunctionalProtocol
public protocol Transformer {
    associatedtype Input
    associatedtype Output
    func transform(_ input: Input) -> Output
}

// That's all. Now use it:
processData(use: .transform { $0.uppercased() })

```

I hope this can close a syntax gab and avoid the keyword hell in Parameter lists of some functions.

---

<div class="post-metadata">

**Author:** ![dnadoba](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dnadoba/32/11438_2.png) [@dnadoba](https://forums.swift.org/u/dnadoba)\
**Post date:** [July 15, 2026, 1:33am UTC](https://forums.swift.org/t/functionalprotocol-zero-cost-closure-wrappers-for-sam-protocols/88280/2 "2026-07-15T01:33:50Z")

</div>

This is a convenient syntax but note that It’s not zero cost at all if the closure captures anything other than a swift object as closures heap allocate their storage.  
A struct can store its “context” in stored properties which are all stored inline which can make a huge difference.

---

<div class="post-metadata">

**Author:** ![CodebyCR](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/codebycr/32/34814_2.png) [@CodebyCR](https://forums.swift.org/u/CodebyCR)\
**Post date:** [July 15, 2026, 6:57am UTC](https://forums.swift.org/t/functionalprotocol-zero-cost-closure-wrappers-for-sam-protocols/88280/3 "2026-07-15T06:57:03Z")

</div>

You're right, but compared to a closure in the parameter list, the wrapper struct can be completely optimized away. This gives you the conformance of a protocol with a closure without the additional overhead.

I had considered using structs as closures, but currently we need the flexibility of the closure. Perhaps someday, after the new lifetimes and zero-cost abstractions, we'll get stack closures like in Rust.
