Why can't computed properties be overloaded (like methods can)?

That's not exactly true. As shown in the OP, the following multiple parameterless things (with or without static) compiles:

struct S {
    static func foo() { } 
    static func foo() -> Int { 123 } // Compiles.
    static func foo() -> Bool { true } // Compiles.
    static func foo() -> Float { 1.23 } // Compiles.
}

Changing those to computed properties does not compile (as shown in the OP), but there actually are scenarios where computed properties can be overloaded too:

struct S<A> {
    static var foo: Int { 123 }
    // static var foo: Bool { true } // Does not compile (as expected).
}
extension S where A == UInt16 {
    static var foo: Bool { true } // Compiles, but should it?
}
extension S where A == UInt32 {
    static var foo: String { "abc" } // Compiles, but should it?
}
extension S where A: UnsignedInteger {
    static var foo: Double { 1.23 } // Compiles, but should it?
}
extension S where A: FixedWidthInteger {
    static var foo: Double { 3.21 } // Compiles, but should it?
}

func bar1<T: UnsignedInteger>(_: T.Type) -> Double { return S<T>.foo }
func bar2<T: FixedWidthInteger>(_: T.Type) -> Double { return S<T>.foo }

func test() {
    print(S<UInt16>.foo as Int)    // 123
    print(S<UInt16>.foo as Bool)   // true
    print(S<UInt32>.foo as String) // abc
    print(bar1(UInt16.self))       // 1.23
    print(bar2(UInt16.self))       // 3.21
}
test()
2 Likes