# Stored Property on Protocol Extension

**URL:** <https://forums.swift.org/t/stored-property-on-protocol-extension/4000>\
**Category:** Using Swift\
**Created:** [September 12, 2016, 1:12am UTC](https://forums.swift.org/t/stored-property-on-protocol-extension/4000 "2016-09-12T01:12:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tanner0101](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tanner0101/32/675_2.png) [@tanner0101](https://forums.swift.org/u/tanner0101)\
**Post date:** [September 12, 2016, 1:12am UTC](https://forums.swift.org/t/stored-property-on-protocol-extension/4000/1 "2016-09-12T01:12:53Z")

</div>

Hey Swift Users,

I was wondering how you all work around not being able to add stored properties in extensions (especially protocol extensions).

I ran into an issue recently where I needed an internal stored variable for a protocol, but I didn't want the conformer to worry about implementing the variable.

I ended up using something like this to achieve the effect.

&nbsp;&nbsp;&nbsp;&nbsp;extension MyProtocol {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private var address: String {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mutating get {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var id = ""  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;withUnsafePointer(to: &self) { id = "\($0)"}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return id  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var myStoredVar: Bool {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mutating get {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return \_storage[address] ?? false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_storage[address] = newValue  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;}

Obviously not ideal, but I don't see another way to achieve this besides subclassing (which has its own problems for my situation).

Wondering if anyone has run into this and come up with a better solution or other type of work around.

Thanks!  
Tanner

---

<div class="post-metadata">

**Author:** ![ddunbar](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ddunbar/32/1933_2.png) [@ddunbar](https://forums.swift.org/u/ddunbar)\
**Post date:** [September 12, 2016, 3:55pm UTC](https://forums.swift.org/t/stored-property-on-protocol-extension/4000/2 "2016-09-12T15:55:12Z")

</div>

> Hey Swift Users,
> 
> I was wondering how you all work around not being able to add stored properties in extensions (especially protocol extensions).
> 
> I ran into an issue recently where I needed an internal stored variable for a protocol, but I didn't want the conformer to worry about implementing the variable.
> 
> I ended up using something like this to achieve the effect.
> 
> &nbsp;&nbsp;&nbsp;extension MyProtocol {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private var address: String {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mutating get {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var id = ""  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;withUnsafePointer(to: &self) { id = "\($0)"}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return id

BTW, you can write `return withUnsafePointer(to: &self) { "\($0)" }` instead.

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var myStoredVar: Bool {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mutating get {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return \_storage[address] ?? false  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_storage[address] = newValue  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;}
> 
> Obviously not ideal, but I don't see another way to achieve this besides subclassing (which has its own problems for my situation).
> 
> Wondering if anyone has run into this and come up with a better solution or other type of work around.

I don't have a better answer to this part.

This particular solution only works if it is ok for `_storage[address]` to potentially be reused by a new instance, though (if the old instance is deallocated and the new one is allocated in its place).

- Daniel

> **···**
>
> > On Sep 11, 2016, at 6:12 PM, Tanner Nelson via swift-users \<swift-users@swift.org\> wrote:
> 
> > Thanks!  
> > Tanner  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)

---

<div class="post-metadata">

**Author:** ![Zhao\_Xin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/zhao_xin/32/8394_2.png) [@Zhao\_Xin](https://forums.swift.org/u/Zhao_Xin)\
**Post date:** [September 12, 2016, 10:50pm UTC](https://forums.swift.org/t/stored-property-on-protocol-extension/4000/3 "2016-09-12T22:50:55Z")

</div>

​I don't know why the limit exists. Technical or on purpose. I hope someone  
inside can answer this.

Zhaoxin​

> **···**
>
> On Mon, Sep 12, 2016 at 11:55 PM, Daniel Dunbar via swift-users \< swift-users@swift.org\> wrote:
> 
> > \> On Sep 11, 2016, at 6:12 PM, Tanner Nelson via swift-users \< \> swift-users@swift.org\> wrote:  
> > \>  
> > \> Hey Swift Users,  
> > \>  
> > \> I was wondering how you all work around not being able to add stored  
> > properties in extensions (especially protocol extensions).  
> > \>  
> > \> I ran into an issue recently where I needed an internal stored variable  
> > for a protocol, but I didn't want the conformer to worry about implementing  
> > the variable.  
> > \>  
> > \> I ended up using something like this to achieve the effect.  
> > \>  
> > \> extension MyProtocol {  
> > \> private var address: String {  
> > \> mutating get {  
> > \> var id = ""  
> > \> withUnsafePointer(to: &self) { id = "\($0)"}  
> > \> return id
> > 
> > BTW, you can write `return withUnsafePointer(to: &self) { "\($0)" }`  
> > instead.
> > 
> > \> }  
> > \> }  
> > \>  
> > \> var myStoredVar: Bool {  
> > \> mutating get {  
> > \> return \_storage[address] ?? false  
> > \> }  
> > \> set {  
> > \> \_storage[address] = newValue  
> > \> }  
> > \> }  
> > \> }  
> > \>  
> > \> Obviously not ideal, but I don't see another way to achieve this besides  
> > subclassing (which has its own problems for my situation).  
> > \>  
> > \> Wondering if anyone has run into this and come up with a better solution  
> > or other type of work around.
> > 
> > I don't have a better answer to this part.
> > 
> > This particular solution only works if it is ok for `_storage[address]` to  
> > potentially be reused by a new instance, though (if the old instance is  
> > deallocated and the new one is allocated in its place).
> > 
> > - Daniel
> > 
> > \>  
> > \> Thanks!  
> > \> Tanner  
> > \> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > \> swift-users mailing list  
> > \> swift-users@swift.org  
> > \> [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)

---

<div class="post-metadata">

**Author:** ![Dan\_Stenmark](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dan_stenmark/32/1589_2.png) [@Dan\_Stenmark](https://forums.swift.org/u/Dan_Stenmark)\
**Post date:** [September 12, 2016, 11:03pm UTC](https://forums.swift.org/t/stored-property-on-protocol-extension/4000/4 "2016-09-12T23:03:32Z")

</div>

There's no good way of using extensions to expand the storage of an external module's type unless you're using Associated Objects underneath, but that has heavy performance implications (especially in threaded cases, which would require global locks on the associated object store). At most, I'd support a proposal for stored properties in extensions that reside in the same module as the class declaration, but it sounds like the team has bigger fish to fry right now.

If possible, consider subclassing to extend a type's storage instead.

Dan

> **···**
>
> Sent from my iPhone
> 
> > On Sep 12, 2016, at 3:50 PM, Zhao Xin via swift-users \<swift-users@swift.org\> wrote:
> > 
> > ​I don't know why the limit exists. Technical or on purpose. I hope someone inside can answer this.
> > 
> > Zhaoxin​
> > 
> > > On Mon, Sep 12, 2016 at 11:55 PM, Daniel Dunbar via swift-users \<swift-users@swift.org\> wrote:
> > > 
> > > \> On Sep 11, 2016, at 6:12 PM, Tanner Nelson via swift-users \<swift-users@swift.org\> wrote:  
> > > \>  
> > > \> Hey Swift Users,  
> > > \>  
> > > \> I was wondering how you all work around not being able to add stored properties in extensions (especially protocol extensions).  
> > > \>  
> > > \> I ran into an issue recently where I needed an internal stored variable for a protocol, but I didn't want the conformer to worry about implementing the variable.  
> > > \>  
> > > \> I ended up using something like this to achieve the effect.  
> > > \>  
> > > \> extension MyProtocol {  
> > > \> private var address: String {  
> > > \> mutating get {  
> > > \> var id = ""  
> > > \> withUnsafePointer(to: &self) { id = "\($0)"}  
> > > \> return id
> > > 
> > > BTW, you can write `return withUnsafePointer(to: &self) { "\($0)" }` instead.
> > > 
> > > \> }  
> > > \> }  
> > > \>  
> > > \> var myStoredVar: Bool {  
> > > \> mutating get {  
> > > \> return \_storage[address] ?? false  
> > > \> }  
> > > \> set {  
> > > \> \_storage[address] = newValue  
> > > \> }  
> > > \> }  
> > > \> }  
> > > \>  
> > > \> Obviously not ideal, but I don't see another way to achieve this besides subclassing (which has its own problems for my situation).  
> > > \>  
> > > \> Wondering if anyone has run into this and come up with a better solution or other type of work around.
> > > 
> > > I don't have a better answer to this part.
> > > 
> > > This particular solution only works if it is ok for `_storage[address]` to potentially be reused by a new instance, though (if the old instance is deallocated and the new one is allocated in its place).
> > > 
> > > - Daniel
> > > 
> > > \>  
> > > \> Thanks!  
> > > \> Tanner  
> > > \> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > \> swift-users mailing list  
> > > \> swift-users@swift.org  
> > > \> [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-users mailing list  
> > > swift-users@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)

---

<div class="post-metadata">

**Author:** ![inso](https://avatars.discourse-cdn.com/v4/letter/i/bb73d2/32.png) [@inso](https://forums.swift.org/u/inso)\
**Post date:** [July 10, 2020, 10:42am UTC](https://forums.swift.org/t/stored-property-on-protocol-extension/4000/5 "2020-07-10T10:42:24Z")

</div>

I have made an attempt to re-implement Associated Object in pure swift.

> **[GitHub - inso-/SwiftAssociatedObject: Pure Swift Implementation of Associated...](https://github.com/inso-/SwiftAssociatedObject/)**
>
> Pure Swift Implementation of Associated Object. Contribute to inso-/SwiftAssociatedObject development by creating an account on GitHub.

The performance implications are similar to Objective-C runtime Associated Object I think.  
I'd will support a proposal for stored properties in extensions that reside in the same module too, hope it's something the team can still consider for a future version.

Thomas
