Interaction between optional protocol's requirenment, default implementation and non-optional implementation

Is this expected behavior?

import Foundation

protocol Provider {
    var value: String? { get }
}

extension Provider {
    var value: String? { nil }
}

struct Struct: Provider {
    // Depending on property's type printValue prints either "123" or nil.
    // For `String` output is "nil", for `String?` output is "123".
    var value: String
}

func printValue(_ value: some Provider) {
    print(value.value ?? "nil")
}

let s = Struct(value: "123")
printValue(s)

Looks fair to me, if the type is different you are overloading "value" with something unrelated which is not used when accessing the protocol member, and given there's default implementation there is no compilation error.

Somehow I thought non-optional property should satisfy optional property requirement :thinking: It seems, It is not the case.

Yes. Overloading is a compile-time fiction, so let’s just manually rename your declarations to encode the type as part of the name:

protocol Provider {
    var valueOptional: String? { get }
}

extension Provider {
    var valueOptional: String? { nil }
}

struct Struct: Provider {
    var valueNonOptional: String
}

func printValue(_ value: some Provider) {
    print(value.valueOptional ?? "nil")
}

The behavior is now clear.

3 Likes