在 Swift 协议一致性中使用内部或私有类型的困难

问题描述 投票:0回答:1

我在尝试使用符合 Swift 协议一致性的内部或私有类型时遇到问题。这是我的代码的简化版本:


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 add(_ r1: Rational, _ r2: Rational) -> Rational {
        <#code#>
    }
    
    // error: ...
    public static func toString(_ r: Rational) -> String {
        <#code#>
    }

    fileprivate func foo(_ x: Int) {
        <#code#>
    }
    
    private func boo(_ x: Int) -> Int {
        <#code#>
    }
    
    
}

我希望 Rational 枚举仅保留在模块内,甚至是私有的,但返回或使用 Rational 的其余方法是公共的。如果我将枚举声明更改为公共,则一切正常。如何在保持枚举内部或私有的同时实现这一目标?

此时协议可以是任何协议,只要该代码需要该协议即可工作。谢谢

顺便说一句,新来的,所以欢迎任何建议。

swift struct protocols public internals
1个回答
0
投票
// Define a protocol for rational number operations
internal protocol RATIONAL {
    associatedtype rational
    
    // Method to create a rational number from numerator and denominator
    static func makeFrac(_ x: Int, _ y: Int) throws -> rational
    
    // Method to add two rational numbers
    static func add(_ r1: rational, _ r2: rational) -> rational
    
    // Method to convert a rational number to a string representation
    static func toString(_ r: rational) -> String
}

// A concrete implementation of the RATIONAL protocol
public struct Rational1: RATIONAL {
    // Define a typealias for the rational type
    public typealias rational = (Int, Int)
    
    // Method to create a rational number from numerator and denominator
    public static func makeFrac(_ x: Int, _ y: Int) throws -> rational {
        // Implementation goes here
    }
    
    // Internal method to create a rational number from numerator and denominator
    internal func makeFrac(_ x: Int, _ y: Int) throws -> (Int, Int) {
        // Implementation goes here
    }
    
    // Method to add two rational numbers
    public static func add(_ r1: rational, _ r2: rational) -> rational {
        // Implementation goes here
    }
    
    // Internal method to add two rational numbers
    internal func add(_ r1: (Int, Int), _ r2: (Int, Int)) -> (Int, Int) {
        // Implementation goes here
    }

    // Method to convert a rational number to a string representation
    public static func toString(_ r: rational) -> String {
        // Implementation goes here
    }
    
    // Internal method to convert a rational number to a string representation
    internal func toString(_ r: (Int, Int)) -> String {
        // Implementation goes here
    }
    
    // Fileprivate utility method
    fileprivate func foo(_ x: Int) {
        // Implementation goes here
    }
    
    // Private utility method
    private func boo(_ x: Int) -> Int {
        // Implementation goes here
    }
}

这是我能想到的最好的方法,因为类型别名是不可变的,而且我还删除了枚举。

顺便说一句,这次类型别名可以是内部的,但看起来最初情况并非如此(使用第一种方法)。

© www.soinside.com 2019 - 2024. All rights reserved.