Are we allowed to overload a property or not?

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

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:

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 }
}
func test(s:S) -> Int?
{
    if let r:Int = s.readConcern
    {
        return r
    }
    else
    {
        return s.readConcern
    }
}
1 Like

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?

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

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

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! :slight_smile:

3 Likes

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. :slight_smile: