hn.today

Generic Const Args and You

blog.rust-lang.org9 points2 comments
Screenshot of Generic Const Args and You

Generic Const Arguments (GCA) is a family of Rust features that expands what expressions can appear as const generic arguments, replacing the older generic_const_exprs. It introduces a gca!(...) marker to allow expressions that reference generic parameters inside const generics, enabling useful patterns previously impossible - such as constructing ADTs, tuples, and arrays that depend on generic values (e.g., gca!(Foo { field: N }) or u8; gca!(N)). The gca_adts feature covers struct/enum/tuple/array construction (array repeats not yet supported) but intentionally restricts expression forms inside gca! (no arithmetic or function calls). That restriction plus the explicit gca! macro are pragmatic compromises: gca!(..) const args permit generic-parameter use where ordinary const args cannot, but require explicit marking and have a narrower expression grammar.

Two const-item-focused features manage broader expressiveness and compiler complexity. gca_const_items lets arbitrary const items (including associated and inherent associated constants) participate in the type system and requires -Znext-solver; it supports transparent gca!(...) right-hand sides so the compiler can equate constants, enabling things like bounding associated consts and dyn traits. gca_min_const_items is a smaller, easier-to-stabilize subset that only accepts const items written as gca!(...), avoiding post-monomorphization errors and confusing unequal-constant diagnostics. A longer-term goal is to remove the explicit gca! (macroless), but implementing implicit insertion is hard: syntactic ambiguity and false positives (e.g., distinguishing Foo(N) needing gca! from Bar(ONE) function calls) remain unsolved.

Read on blog.rust-lang.org2 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.