'should' is inaccessible due to 'internal' protection level

Hi


I created the FluentSwiftAssertions framework to improve readability of test assertions. (You can find it on githup)


The problem I'm struggeling with is somehow strange. In order to be able to write an assertion in the following way:


aBoolVariable.should.beTrue()


I implemented an extension:


extension Bool {

    public var should: Bool {
        return self
    }

    public func beTrue(_ message: @autoclosure () -> .......

(this is just the first part of the extension)


The strange tning now is, if I include the extension on code level the following two statements are compiled without errors:


        aBoolVaraible.beTrue()
        aBoolVaraible.should.beTrue()


However, when I use the framework instead of the source code the first line is ok but the second line creates the following compile error.

'should' is inaccessible due to 'internal' protection level

I have no clue how to solve this issue.

>I have no clue how to solve this issue.


Posting a question in Swift code relating to the FluentSwiftAssertions framework on the Objective-C forum will most likely not provide that clue you are seeking.

FWIW, I believe the access controls of class members defined in extensions is a little bit of a gray area (and I believe there are changes coming in Swift 4 that address or change this).


Currently, extensions can have access modifiers, such as:


public extension Bool {
     …
}


and this defaults to "internal" if unspecified, which I guess is the ultimate cause of your error. This ought to be irrelevant, because you declare your methods "public" explicitly, but I think that's where you get lost in the gray area. The actual access level might depend on whether these methods satisfy a protocol or not, or whether anything is marked @testable or not, or whether this is a unit test target. Also, there is some difference when the extension is defined in a different file from the type — which is necessarily so in this case.


I would try declaring the extension "public" as above, and see if that helps, or adding @testable to the method definitions, if you can. If not, this is probably a bug or deficiency in Swift 3.1, and you should submit a bug report.

Thanks for the answer.


I also tried the public in front of the extions, but this doesn't change anything.

The accessbility of a extension is derived from the defining class/struct.

If the class/struct is public, the extension is public as well and you can decide whether you define public fuctions or not.


The strange thin is, that the functions are accessable hile the property is not, although all of them are public.

'should' is inaccessible due to 'internal' protection level
 
 
Q