Skip to content

strconv: use generic constraints for format_thousands - #29396

Merged
medvednikov merged 2 commits into
vlang:masterfrom
ChrizzzDev:feat/format_thousands_w_constraint
Oct 4, 2026
Merged

medvednikov merged 2 commits into
vlang:masterfrom
ChrizzzDev:feat/format_thousands_w_constraint

Conversation

@ChrizzzDev

Copy link
Copy Markdown
Contributor

Replace the manual $if T !in [...] { $compile_error(...) } check in
format_thousands with the native generic constraint syntax added
recently: format_thousands[T Numeric]. The compiler now enforces
the same restriction at both the call site and inside the body,
instead of a hand-written comptime check that has to be kept in
sync by hand.

Also:

  • document Numeric, which had no doc comment
  • stop format_thousands's doc comment from re-listing the accepted
    types by hand (now just refers to Numeric)
  • fix the add_thousands_sep/format_thousands doc examples to use
    the ```v fenced block vlib's doc tooling expects, so
    v doc -check-examples actually compiles and checks them instead
    of silently skipping them

Note: This uses the new generic type parameter constraint syntax ([T Numeric]) added in #29254. Building this branch requires a v toolchain built from a commit at or after that merge — an older local v binary will either reject [T Numeric] outright or (on pre-#29254 parsers) silently drop the constraint, so format_thousands would compile without the intended type-safety guarantee. If CI or a reviewer sees unexpected generics errors here, check that the toolchain includes #29254 first.

Copilot AI balanced review requested due to automatic review settings October 3, 2026 19:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

…2 vlib

Make `strconv.Numeric` public, so generic code that forwards a `T` to
`format_thousands` can constrain it with `[T strconv.Numeric]`, and list
the exact accepted types in its documentation. Cover every integer type
and a constrained generic wrapper in the tests.

`[T Numeric]` is the first constraint in a module that every program
imports (through `builtin`), and the V 0.5.2 compatibility compiler
cannot parse it. 9d7a2a7 ran the `-check` fixtures in
vlib/v/checker/tests/with_check_option/ with that compiler against the
current vlib, so all 4 of them (and compiler_errors_test.v) failed.
Run them against the compatibility compiler's own vlib instead, like the
other fixtures routed to it. Only the JSON cgen fixtures still get the
current vlib; none exist since the cJSON `json` module was removed.

Checked that nothing else needs V 0.5.2 to parse the current vlib:
- all 4 with_check_option fixtures match their .out byte for byte, and
  the 3 legacy compiler module fixtures still match theirs;
- make/`v self` bootstrap from vc/v.c, which is generated from master
  and already parses constraints: a v1 built from the current vc
  snapshot with -DCUSTOM_DEFINE_v1_fallback compiles cmd/v with this
  vlib; vc generation and `v up` use the current compiler;
- `-old-compiler`, the automatic V1 retry, `-vls-mode` and
  `build-module` already use the fallback's own vroot (or map module
  paths into it), and work with this change;
- CI only greps for unexpected V1 fallbacks; it never runs the
  compatibility compiler on this repository's vlib.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants