# Are we allowed to overload a property or not?

**URL:** <https://forums.swift.org/t/are-we-allowed-to-overload-a-property-or-not/62167>\
**Category:** Using Swift\
**Created:** [December 21, 2022, 9:38pm UTC](https://forums.swift.org/t/are-we-allowed-to-overload-a-property-or-not/62167 "2022-12-21T21:38:58Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [December 21, 2022, 9:38pm UTC](https://forums.swift.org/t/are-we-allowed-to-overload-a-property-or-not/62167/1 "2022-12-21T21:38:58Z")

</div>

i had always assumed we cannot overload a property because this is a compiler error:

```swift
public
struct S
{
    public
    var readConcern:Int { 1 }
    public
    var readConcern:Int? { 1 }
}
// error: invalid redeclaration of 'readConcern'

```

and yet i am able to provide overloads in a generic context, and access those overloads in a concretely-typed context:

```swift
public
protocol SessionCommand
{
    var readConcern:Int? { get }
}
public
protocol ReadCommand:SessionCommand
{
    var readConcern:Int { get }
}
extension ReadCommand
{
    public
    var readConcern:Int?
    {
        .some(self.readConcern)
    }
}

public
struct S:ReadCommand
{
    public
    var readConcern:Int { 1 }
}

```

```swift
func test(s:S) -> Int?
{
    if let r:Int = s.readConcern
    {
        return r
    }
    else
    {
        return s.readConcern
    }
}

```

---

<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 21, 2022, 9:49pm UTC](https://forums.swift.org/t/are-we-allowed-to-overload-a-property-or-not/62167/2 "2022-12-21T21:49:14Z")

</div>

> [@taylorswift](#):
>
> i had always assumed we cannot overload a property because this is a compiler error:
> 
> ```swift
> public
> struct S
> {
> public
> var readConcern:Int { 1 }
> public
> var readConcern:Int? { 1 }
> }
> // error: invalid redeclaration of 'readConcern'
> 
> ```

Assuming overload by return value is a good thing (which is questionable) this behaviour is already strange: I can do it with a function and with a subscript, but not with a variable?

---

<div class="post-metadata">

**Author:** ![ibex](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ibex/32/30439_2.png) [@ibex](https://forums.swift.org/u/ibex)\
**Post date:** [December 21, 2022, 10:16pm UTC](https://forums.swift.org/t/are-we-allowed-to-overload-a-property-or-not/62167/3 "2022-12-21T22:16:47Z")

</div>

I am sorry but I could not contact you privately, about the code style.

> [@taylorswift](#):
>
> ```swift
> public
> struct S
> {
> public
> var readConcern:Int { 1 }
> public
> var readConcern:Int? { 1 }
> }
> // error: invalid redeclaration of 'readConcern'
> 
> ```

It looks visually and cognitively pretty jarring, compared to this:

```swift
public struct S {
    public var readConcern:Int { 1 }
    public var readConcern:Int? { 1 }
}
// error: invalid redeclaration of 'readConcern'

```

Is there a compelling reason for this, apart from following some brutal coding standards?

I understand that when very long names and signatures are involved breaking the lines make sense, but why do it when the names are very short.

Merry Christmas and a happy new year! 🙂

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [December 21, 2022, 10:20pm UTC](https://forums.swift.org/t/are-we-allowed-to-overload-a-property-or-not/62167/4 "2022-12-21T22:20:43Z")

</div>

> [@ibex](#):
>
> Is there a compelling reason for this, apart from following some brutal coding standards?

it makes it easier to add and remove the access modifiers (`public`, `private`, etc.) using editor line operations, which i do quite often. also i personally find it easier to scan for the property names if they all begin at n + 4 columns from the base indent of the type. 🙂

---

<div class="post-metadata">

**Author:** ![aley](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/aley/32/10002_2.png) [@aley](https://forums.swift.org/u/aley)\
**Post date:** [September 16, 2024, 8:41pm UTC](https://forums.swift.org/t/are-we-allowed-to-overload-a-property-or-not/62167/5 "2024-09-16T20:41:12Z")

</div>

I've just run into this, too. The following code compiles fine in Swift 5.10:

```swift
protocol Fruit {
    var name: String? { get }
}

extension Fruit {
    var name: String? { nil }
}

struct Pear : Fruit {
    var name: Int = 3
}

```

I would have expected `Pear` to not conform to `Fruit` because `name` is declared as an `Int`.

Is this expected behavoir?

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [September 16, 2024, 9:58pm UTC](https://forums.swift.org/t/are-we-allowed-to-overload-a-property-or-not/62167/6 "2024-09-16T21:58:58Z")

</div>

> [@aley](#):
>
> I would have expected `Pear` to not conform to `Fruit` because `name` is declared as an `Int`.

If while evaluating a conformance we find multiple candidates with the same name as a protocol requirement, we pick the one with the matching type, if possible. So in this case it’s going to be the default implementation in the protocol extension.

---

<div class="post-metadata">

**Author:** ![aley](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/aley/32/10002_2.png) [@aley](https://forums.swift.org/u/aley)\
**Post date:** [September 16, 2024, 10:16pm UTC](https://forums.swift.org/t/are-we-allowed-to-overload-a-property-or-not/62167/7 "2024-09-16T22:16:24Z")

</div>

Thanks for the explanation.

It's still weird to me that I'm not getting `Invalid redeclaration of 'name'` in `Pear` and that this is valid:

```swift
var pear = Pear()
let nameAsString: String? = pear.name // nil
let nameAsInt: Int = pear.name // 3

```

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [September 16, 2024, 10:20pm UTC](https://forums.swift.org/t/are-we-allowed-to-overload-a-property-or-not/62167/8 "2024-09-16T22:20:29Z")

</div>

If Swift didn’t allow this, how would it handle the case where either 1) `Pear.name` exists from the start, but `Fruit.name` is added in a future release, or 2) `Pear` gains a retroactive conformance to `Fruit`?

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [September 16, 2024, 10:38pm UTC](https://forums.swift.org/t/are-we-allowed-to-overload-a-property-or-not/62167/9 "2024-09-16T22:38:15Z")

</div>

> [@aley](#):
>
> It's still weird to me that I'm not getting `Invalid redeclaration of 'name'` in `Pear` and that this is valid:

To answer the question directly—yes, this is the expected behavior.

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [September 16, 2024, 10:41pm UTC](https://forums.swift.org/t/are-we-allowed-to-overload-a-property-or-not/62167/10 "2024-09-16T22:41:53Z")

</div>

> [@aley](#):
>
> It's still weird to me that I'm not getting `Invalid redeclaration of 'name'` in `Pear` and that this is valid:

The redeclaration checking rules are meant to reject situations such as duplicate overloads that cannot be distinguished by name or type, but they don’t cover all potentially confusing situations.

---

<div class="post-metadata">

**Author:** ![aley](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/aley/32/10002_2.png) [@aley](https://forums.swift.org/u/aley)\
**Post date:** [September 16, 2024, 10:50pm UTC](https://forums.swift.org/t/are-we-allowed-to-overload-a-property-or-not/62167/11 "2024-09-16T22:50:22Z")

</div>

Thanks everyone!  
I never doubted that there are good reasons why Swift does it like this; it just didn't match my mental model.

My previous assumption:

- A property has an unambiguous type

Corrected to:

- A **stored** property has an unambiguous type (but overloads may exists in the form of computed properties with the same name)

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [September 16, 2024, 11:09pm UTC](https://forums.swift.org/t/are-we-allowed-to-overload-a-property-or-not/62167/12 "2024-09-16T23:09:44Z")

</div>

It’s not even about stored properties per se, just that we allow overloading of property declarations when extensions are involved. Stored properties cannot appear in extensions so you cannot have two stored properties with the same name as a consequence of those two behaviors.

> [@aley](#):
>
> I never doubted that there are good reasons why Swift does it like this; it just didn't match my mental model.

Certainly not every behavior of the implementation has a logical explanation :) But in this case I think it’s pretty reasonable.

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [September 16, 2024, 11:15pm UTC](https://forums.swift.org/t/are-we-allowed-to-overload-a-property-or-not/62167/13 "2024-09-16T23:15:03Z")

</div>

> [@ksluder](#):
>
> If Swift didn’t allow this, how would it handle the case where either 1) `Pear.name` exists from the start, but `Fruit.name` is added in a future release, or 2) `Pear` gains a retroactive conformance to `Fruit`?

You can also just have two constrained extensions that declare computed properties with the same name and type. If the generic signatures are distinct it’s not considered to be a redeclaration, even though you might still end up in a situation where some concrete generic arguments satisfy both signatures, at which point it would be diagnosed as an ambiguity at the call site.

---

<div class="post-metadata">

**Author:** ![ibex](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ibex/32/30439_2.png) [@ibex](https://forums.swift.org/u/ibex)\
**Post date:** [September 17, 2024, 1:35am UTC](https://forums.swift.org/t/are-we-allowed-to-overload-a-property-or-not/62167/14 "2024-09-17T01:35:11Z")

</div>

> [@aley](#):
>
> It's still weird to me that I'm not getting `Invalid redeclaration of 'name'` in `Pear` and that this is valid:
> 
> ```swift
> var pear = Pear()
> let nameAsString: String? = pear.name // nil
> let nameAsInt: Int = pear.name // 3
> 
> ```

The second statement is guiding `Swift` to pick the declaration from the protocol by providing additional contextual type information.

This feature proves quite useful, for example, when you want to specify the type of a generic function's return value.

```swift
func peek <T> (_ u: Any) -> T? {
   u as? T
}

```

Someone with C++-hat firmly on would automatically write this:

```swift
let u = peek <Int> (bar ()) // Cannot explicitly specialize a generic function

```

And they are relieved when they discover that the following actually works.

```swift
for _ in 0..<3 {
    if let u: Int = peek (bar ()) {
        print (u)
    }
    
    if let u: String = peek (bar ()) {
        print (u)
    }
    
    if let u: [Int] = peek (bar ()) {
        print (u)
    }
}

```

```swift

func bar () -> Any {
    let u: [Any] = [3, "three", [1, 1, 1] ]
    return u [Int.random(in: 0..<u.count)]
}

```
