为什么Swift库使用"enum CommandLine"而不是"struct CommandLine"?

Rau*_*nak 2 enums struct swift

Swift标准库声明CommandLine为枚举.

/// Command-line arguments for the current process.
public enum CommandLine {

    /// Access to the raw argc value from C.
    public static var argc: Int32 { get }

    /// Access to the raw argv value from C. Accessing the argument vector
    /// through this pointer is unsafe.
    public static var unsafeArgv: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?> { get }

    /// Access to the swift arguments, also use lazy initialization of static
    /// properties to safely initialize the swift arguments.
    public static var arguments: [String]
}
Run Code Online (Sandbox Code Playgroud)

通过将其声明CommandLine为结构,可以很好地实现API的目的. 它被声明为枚举而不是结构的任何特定原因?

Rau*_*nak 5

好的,基于一些研究,我发现了这个:

使用无大小写枚举的优点是它不会被意外地实例化并且作为纯命名空间工作.(参考:https://github.com/raywenderlich/swift-style-guide#constants)

我相信这可能是其中一个原因.

  • 这是唯一的原因,这是正确的答案. (2认同)