Private operators?

for some reason, an operator with fileprivate or greater visibility works and can be called from other methods on the same type:

extension Stem
{
    fileprivate static
    func += (self:inout Self, characters:String)
    {
        self.rawValue += characters
    }
}

but if the operator is made private, it cannot be called even in places where a corresponding private method could be called. why?

Can you give an example where a private operator function can’t be called but a private static method can?

here is a small example:

struct S
{
    private static
    func += (self:inout Self, characters:some StringProtocol)
    {
    }

    private static
    func add(to self:inout Self, characters:some StringProtocol)
    {
    }
}
extension S
{
    mutating
    func x()
    {
        Self.add(to: &self, characters: "abc")
    }
    mutating
    func y()
    {
        self += "abc"
    }
}
error: referencing operator function '+=' on 'RangeReplaceableCollection' requires that 'S' conform to 'RangeReplaceableCollection'
        self += "abc"
             ^
Swift.RangeReplaceableCollection:1:11: note: where 'Self' = 'S'
extension RangeReplaceableCollection {
          ^
2 Likes

Methinks that is a bug.

2 Likes

filed cannot call private operators · Issue #67293 · apple/swift · GitHub

1 Like