00JCIV00 / cova

Commands, Options, Values, Arguments. A simple yet robust cross-platform command line argument parsing library for Zig.
https://00jciv00.github.io/cova/
MIT License
110 stars 5 forks source link

Review the use of `const` and `@constCast` throughout the library #35

Open 00JCIV00 opened 1 year ago

00JCIV00 commented 1 year ago

The use of @constCast is generally discouraged, but it's used throughout the library in the Command, Option, and Value types. There two main reasons for this:

  1. A desire to use const as a sort of equivalent to val in Kotlin, wherein the data can be made immutable to external users but still be mutated internally.
  2. A lack of understanding of how const works. Namely, the fact that it applies to memory directly instead of the data or Type. Because of this, the compiler can make assumptions about const data and even choose to move it to the rodata section of a program.

While the library ostensibly works fine in its current state, this is an item that's worth reviewing for completeness down the line.

00JCIV00 commented 1 year ago

Upon initial inspection, the current usage of const is required to share the same Type between comptime and runtime. With the runtime instances being allocated, the usage of const and @constCast might be ok. That said, it should still be looked at further.