Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Swift library uses "enum CommandLine" instead of "struct CommandLine"?

The Swift standard library declares CommandLine as an enum.

/// 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]
}

The purpose of the API could very well be achieved by declaring CommandLine as a struct. Any specific reason why it is declared as an enum instead of a struct?

like image 462
Raunak Avatar asked Mar 24 '26 13:03

Raunak


1 Answers

Ok, based on some research, I found this:

The advantage of using a case-less enumeration is that it can't accidentally be instantiated and works as a pure namespace. (Ref: https://github.com/raywenderlich/swift-style-guide#constants)

I believe this could possibly be one of the reasons.

like image 179
Raunak Avatar answered Mar 26 '26 04:03

Raunak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!