Skip to content

Document how generic arguments and lambda parameters are inferred - #15

Closed
Rigidity wants to merge 1 commit into
mainfrom
rigidity/generics-inference-docs
Closed

Rigidity wants to merge 1 commit into
mainfrom
rigidity/generics-inference-docs

Conversation

@Rigidity

@Rigidity Rigidity commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Documents what generic inference does on Rue main, which the pages predate. Rue range: 0.12.0..bb0bef5a (unreleased), generics campaign xch-dev/rue#362.

Do not merge before a Rue release

.rue-version pins 0.12.0, the latest release, and CI checks every example with that released CLI. Four of the new examples only compile on main, so the rue-examples job fails on this PR until the release that has the inference work is out and .rue-version is bumped (I left it alone, since there's no release to point it at). With the rue on my PATH, which reports 0.12.0, these blocks in docs/type-system/generics.md fail: lambda parameters (the lambda in a pair), accumulators, arguments without values (Never), and union types. Everything passes against a CLI built from main.

What changes

docs/type-system/generics.md gets an Inference section, one short subsection each, each with a compiled example:

  • Literals: a type parameter inferred from a literal type widens to its base type (identity(5) and either(condition, 5, 6) are Int), unless the expected type keeps the literal (let five: 5 = identity(5)). A type parameter that stands for a whole pair or list keeps the literals inside it (identity((5, "x")) is (5, "x")).
  • Lambda Parameters: untyped parameters come from the function type the parameter expects, once the other arguments give its type parameters a type, in any position, including a pair's first item. A lambda with no expected function type needs its parameters typed (shown as an error block).
  • Accumulators: a fold's untyped acc is typed from the seed and what the lambda returns. Pending: this subsection is written for the current behaviour and will be updated when the rule is extended (see below).
  • Arguments Without Values: a type parameter that the arguments hold no value of is Never (length(nil), reverse([]) is a List<Never>).
  • Union Types: a union expected of a call bounds a type parameter through the members the result can be (let r: Result<Int, String> = ok(1)).
  • Explicit Arguments gains the list of places where ::<...> is needed: a type parameter only in the return type; branches of an if or match; generic struct and enum variant initializers (and that Option::None takes none); a generic function passed as a value.

The map example no longer writes fn(value: Int), and docs/functions.md says where a lambda's parameter types come from rather than "the expected type".

Pending

The Accumulators subsection's wording is on hold. Its last paragraph says a nil inside a pair seed ((0, nil)) needs the accumulator typed, which is true on bb0bef5a and is what the in-progress change to Rue is expected to lift. I'll update the subsection when that lands.

Checked against the compiler

Every claim and every type in a comment was compiled with a CLI built from Rue main bb0bef5a, with the inferred type read from an error on a separate binding so that no expected type influenced it. A separate reviewer then checked each sentence against the same binary and found four that were too broad or too narrow, which are corrected here (where literals widen, which type parameters become Never, when a generic function passed as a value needs arguments, and the (0, nil) seed).

  • pnpm check:rue-examples --rue <main's rue> for the two pages: 28 examples, all pass.
  • pnpm prettier, pnpm typecheck, pnpm build (no broken links or anchors): pass.

Not in this PR

Against main's CLI, four existing examples on other pages fail. They're unrelated to inference and belong to the docs update for the next release:

  • docs/builtins.md line 80: keccak256 now needs hard_fork_2 = true in Rue.toml or --hard-fork-2.
  • docs/builtins.md line 116: substr is undeclared.
  • docs/builtins.md line 141: modpow now needs hard_fork_2.
  • docs/control-flow.md line 97: a new manual_assert warning, for an if statement that only raises.

Note

Low Risk
Documentation only: two pages, no site code or configuration. The risk is that it describes unreleased behaviour, so it must wait for the Rue release (the example check fails until then), and that the Accumulators subsection is expected to change before that.

  • Generics page: new Inference section (literals, lambda parameters, accumulators, arguments without values, union types), the list of where generic arguments are needed, and the map example without its annotation.
  • Functions page: one sentence on where a lambda's parameter types come from.

@Rigidity

Copy link
Copy Markdown
Collaborator Author

Closing: the docs are updated for each Rue release with the repo's update-rue-docs skill, from the diff since .rue-version and the merged PRs' descriptions, so this hand-written update would only conflict with it. The branch rigidity/generics-inference-docs is kept: its verified examples and the four rewordings (widening, Never, functions passed as values, fieldless variants) are useful input for that run.

@Rigidity Rigidity closed this Nov 22, 2026
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.

1 participant