I’m encountering issues while trying to use internal or private types in Swift protocol conformance. Here’s a simplified version of my code:
internal protocol RATIONAL {
associatedtype rational
static func makeFrac(_ x: Int, _ y: Int) throws -> rational
static func add(_ r1: rational, _ r2: rational) -> rational
static func toString(_ r: rational) -> String
}
public struct Rational1: RATIONAL {
// error: Type alias cannot be declared public because its underlying type uses an internal type
public typealias rational = Rational
internal enum Rational {
case Whole(Int)
case Frac(Int, Int)
}
// error: Method cannot be declared public because its result uses an internal type
public static func makeFrac(_ x: Int, _ y: Int) throws -> Rational {
<#code#>
}
// error: ...
public static func foo(_ r1: Rational, _ r2: Rational) -> Rational {
<#code#>
}
// error: ...
public static func boo(_ r: Rational) -> String {
<#code#>
}
fileprivate func foo2(_ x: Int) {
<#code#>
}
private func boo2(_ x: Int) -> Int {
<#code#>
}
}
I want the Rational enum to be kept only within the module or even to be private, but the rest of the methods that return or use a Rational to be public. If I change the enum declaration to public, everything works fine. How can I achieve this while keeping the enum internal or private?
At this point protocol could be whatever, if that is needed for this code to work. Thanks
BTW, new to this, so any advice is welcome.