beefon
(Vladislav Alekseev)
1
3 Likes
beefon
(Vladislav Alekseev)
4
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?
Before CommandLine existed, the arguments property was on the Process type. (Walked backward in GitHub to find this.)
Alejandro
(Alejandro Alonso)
6
It’s effectively being used as a namespace here. You can’t instantiate enums.
1 Like
svanimpe
(Steven Van Impe)
7
Sometimes frameworks parse command line arguments first and only leave the remaining ones for your app to see. For example: Gtk – 3.0
I assume that's the reason.
beefon
(Vladislav Alekseev)
8
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?
svanimpe
(Steven Van Impe)
9
You don't pass GTK a copy, it gets a pointer to the originals and modifies them.
beefon
(Vladislav Alekseev)
10
There is such way though. You can avoid changing the state of the singleton.
beccadax
(Becca Royal-Gordon)
11
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.
1 Like
jonprescott
(Jonathan Prescott)
12
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."