# CommandLine.arguments - why var?

**URL:** <https://forums.swift.org/t/commandline-arguments-why-var/26644>\
**Category:** Standard Library\
**Created:** [July 5, 2019, 2:44pm UTC](https://forums.swift.org/t/commandline-arguments-why-var/26644 "2019-07-05T14:44:47Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![beefon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/beefon/32/13391_2.png) [@beefon](https://forums.swift.org/u/beefon)\
**Post date:** [July 5, 2019, 2:44pm UTC](https://forums.swift.org/t/commandline-arguments-why-var/26644/1 "2019-07-05T14:44:47Z")

</div>

`static var arguments: [String]`

[https://developer.apple.com/documentation/swift/commandline/2294816-arguments](https://developer.apple.com/documentation/swift/commandline/2294816-arguments)

Why is it `var` and not `let`?

---

<div class="post-metadata">

**Author:** ![jonathanpenn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jonathanpenn/32/2319_2.png) [@jonathanpenn](https://forums.swift.org/u/jonathanpenn)\
**Post date:** [July 5, 2019, 3:23pm UTC](https://forums.swift.org/t/commandline-arguments-why-var/26644/3 "2019-07-05T15:23:23Z")

</div>

Interesting. I used to be a computed property:

[https://github.com/apple/swift/blame/93c8784463bf769d00d702e6db9aa025fb6e0e32/stdlib/public/core/Process.swift](https://github.com/apple/swift/blame/93c8784463bf769d00d702e6db9aa025fb6e0e32/stdlib/public/core/Process.swift)

But it's not anymore.

---

<div class="post-metadata">

**Author:** ![beefon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/beefon/32/13391_2.png) [@beefon](https://forums.swift.org/u/beefon)\
**Post date:** [July 5, 2019, 3:25pm UTC](https://forums.swift.org/t/commandline-arguments-why-var/26644/4 "2019-07-05T15:25:56Z")

</div>

Your link is about NSTask-like swift class, but here I'm asking about `CommandLine` enum.

Actually, the second question: why is it `enum` and not a `struct`/`class`? Is there a specific reason for choosing `enum` over other variants?

---

<div class="post-metadata">

**Author:** ![jonathanpenn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jonathanpenn/32/2319_2.png) [@jonathanpenn](https://forums.swift.org/u/jonathanpenn)\
**Post date:** [July 5, 2019, 3:34pm UTC](https://forums.swift.org/t/commandline-arguments-why-var/26644/5 "2019-07-05T15:34:59Z")

</div>

Before `CommandLine` existed, the `arguments` property was on the `Process` type. (Walked backward in GitHub to find this.)

---

<div class="post-metadata">

**Author:** ![Alejandro](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro/32/46197_2.png) [@Alejandro](https://forums.swift.org/u/Alejandro)\
**Post date:** [July 5, 2019, 3:35pm UTC](https://forums.swift.org/t/commandline-arguments-why-var/26644/6 "2019-07-05T15:35:40Z")

</div>

It’s effectively being used as a namespace here. You can’t instantiate enums.

---

<div class="post-metadata">

**Author:** ![svanimpe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/svanimpe/32/38963_2.png) [@svanimpe](https://forums.swift.org/u/svanimpe)\
**Post date:** [July 5, 2019, 3:39pm UTC](https://forums.swift.org/t/commandline-arguments-why-var/26644/7 "2019-07-05T15:39:25Z")

</div>

> [@beefon](#):
>
> Why is it `var` and not `let` ?

Sometimes frameworks parse command line arguments first and only leave the remaining ones for your app to see. For example: [Gtk – 3.0](https://developer.gnome.org/gtk3/stable/gtk3-General.html#gtk-init)

I assume that's the reason.

---

<div class="post-metadata">

**Author:** ![beefon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/beefon/32/13391_2.png) [@beefon](https://forums.swift.org/u/beefon)\
**Post date:** [July 5, 2019, 4:33pm UTC](https://forums.swift.org/t/commandline-arguments-why-var/26644/8 "2019-07-05T16:33:16Z")

</div>

In CommandLine you are affecting the whole state of the process from any place of the code at any time, while in GTK you can pass a copy of the args and GTK is actually expected to filter out its contents.

Unless there is a specific reason to make this read only by its nature variable writable, I wonder if this has had been overlooked, had it not?

---

<div class="post-metadata">

**Author:** ![svanimpe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/svanimpe/32/38963_2.png) [@svanimpe](https://forums.swift.org/u/svanimpe)\
**Post date:** [July 5, 2019, 5:05pm UTC](https://forums.swift.org/t/commandline-arguments-why-var/26644/9 "2019-07-05T17:05:12Z")

</div>

> [@beefon](#):
>
> while in GTK you can pass a copy of the args and GTK is actually expected to filter out its contents.

You don't pass GTK a copy, it gets a pointer to the originals and modifies them.

---

<div class="post-metadata">

**Author:** ![beefon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/beefon/32/13391_2.png) [@beefon](https://forums.swift.org/u/beefon)\
**Post date:** [July 5, 2019, 6:34pm UTC](https://forums.swift.org/t/commandline-arguments-why-var/26644/10 "2019-07-05T18:34:54Z")

</div>

There is such way though. You can avoid changing the state of the singleton.

---

<div class="post-metadata">

**Author:** ![beccadax](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/beccadax/32/34217_2.png) [@beccadax](https://forums.swift.org/u/beccadax)\
**Post date:** [July 5, 2019, 7:37pm UTC](https://forums.swift.org/t/commandline-arguments-why-var/26644/11 "2019-07-05T19:37:51Z")

</div>

It's an enum because `CommandLine` is just being used as a namespace, so we don't want you actually instantiating any instances of it. The compiler can prove that a frozen, caseless enum is not instantiable; it can't prove that for a struct.

As for its settability, I don't know what was in the implementor's head at the time, but I'm guessing the reasoning is basically "why not?" It can be convenient to structure command-line tools as removing or rewriting parts of the argument list before later stages of the program process it, and there isn't a compelling reason to prevent that kind of design.

In any case, it would be a breaking change to make it read-only now, so I doubt it's going to change.

---

<div class="post-metadata">

**Author:** ![jonprescott](https://avatars.discourse-cdn.com/v4/letter/j/e36b37/32.png) [@jonprescott](https://forums.swift.org/u/jonprescott)\
**Post date:** [July 5, 2019, 9:31pm UTC](https://forums.swift.org/t/commandline-arguments-why-var/26644/12 "2019-07-05T21:31:34Z")

</div>

I seem to be seeing a lot of issues that boil down to "namespaces." Has there been any consideration of adding the equivalent of C++ namespaces? Using a case-less enum as a namespace seems to be a recurrence of using a C++ class as a namespace "back in the day."

---

<div class="post-metadata">

**Author:** ![Nobody1707](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nobody1707/32/2169_2.png) [@Nobody1707](https://forums.swift.org/u/Nobody1707)\
**Post date:** [July 5, 2019, 10:49pm UTC](https://forums.swift.org/t/commandline-arguments-why-var/26644/13 "2019-07-05T22:49:15Z")

</div>

Yes, but nothing recently: [The One Stop Shop for Previous Submodule Pitches](https://forums.swift.org/t/the-one-stop-shop-for-previous-submodule-pitches/12485/)
