Repository navigation
dang: empty collections carry their inferred type - #278
Merged
Merged
Conversation
NewOpenWeakVar makes a weak variable for the unknown part of one expression's type (an empty literal's element type) rather than a binding's. Like any weak variable it is bound once, by the first unification that pins it, so the type the program settles on can be read back when the expression is evaluated. Unlike a binding's, it may hold any rigid variable: a rigid variable an expression's type meets is in scope where the expression is. Taken into a binding's weak variable, it narrows to that binding's scope. It prints as a plain variable, as before it was weak. CommitWeak now also adds to the substitution it commits the weak variables it made for a binding's flexible parts. Before, `w := a!` made `a` weak in w's binding only, and a later `a := Float` from the same unification's caller never reached it. Signed-off-by: Alex Suraci <[email protected]>
An empty `[]` or `[:]` used to evaluate to a list or map whose element type
was a placeholder variable, whatever its static type, so an empty
[Float!]! from `xs ?? []` summed to Int 0 and `/` then divided as Int.
- Its element type is now an open weak variable, which the unification
that settles it binds for good (`xs ?? []`, an if branch, a reduce
accumulator, an argument); Eval reads the binding back. A `let` holding
one takes it into its own weak variable, scoped as before.
- Generic calls carry runtime type arguments on their frame: each of the
function's own signature variables is bound to what the arguments'
runtime types (and the block's result type) say it stands for. Blocks and
nested functions see the type arguments of the generic functions they
are written in, and a function's own variables shadow those. Empty
literals, map/flatMap results and materialized lists (return values
included) in a generic body resolve their element types through them,
so `[a: Numeric](xs: [a!]!) { xs.map { x => x * 2 } }` called with a
[Float!]! builds a [Float!]!.
A variable the arguments don't decide (only the expected result does, or
the arguments are null) is still unknown while the body runs.
TestEmptyCollectionsCarryTheirInferredType checks runtime against static
element types for each case.
Signed-off-by: Alex Suraci <[email protected]>
sum on an empty list now returns 0 or 0.0 by its element type, so the collections page and the skill drop the caveats, keeping only variables a generic call's arguments don't decide. test_list_sum covers `xs ?? []`, if branches, reduce accumulators, generic arguments, blocks, bodies and returns, and shadowed type variables. Signed-off-by: Alex Suraci <[email protected]>
Deploying dang with
|
| Latest commit: |
08d717c
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://e6fe9932.dang-3kk.pages.dev |
| Branch Preview URL: | https://runtime-element-types.dang-3kk.pages.dev |
vito
force-pushed
the
runtime-element-types
branch
from
November 9, 2026 17:39
90693d4 to
87e9750
Compare
vito
marked this pull request as ready for review
November 9, 2026 17:39
vito
force-pushed
the
runtime-element-types
branch
from
November 9, 2026 17:43
87e9750 to
9893811
Compare
vito
force-pushed
the
runtime-element-types
branch
from
November 10, 2026 00:14
9893811 to
8ac7d6d
Compare
The collections page said an empty literal needs an annotation, which the syntax page and the sum section below it contradict. Signed-off-by: Alex Suraci <[email protected]>
vito
force-pushed
the
runtime-element-types
branch
from
November 10, 2026 00:27
8ac7d6d to
08d717c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An empty
[]or[:], and a list built inside a generic body, now carries its inferred element type at runtime, so the runtime value matches its static type. As a result,sumon an emptyFloat!list returns0.0, so a later/divides as Float.This is option C of Q-27: fix the root cause rather than document the gap (#274's "Open question").
How
hm.NewOpenWeakVar). Like alet's weak variable, the first committed unification that settles it binds it for good:xs ?? [], an if branch, areduce([])accumulator, or a generic call's argument.List.EvalandMapLiteral.Evalread that binding back.letholding one moves it into the let's own weak variable, with the let's scope.hm.CommitWeaknow adds to the substitution it commits the weak variables it creates for a binding's flexible parts. Before,w := a'!(a[]passed for[a!]!) weakeneda'only insidew's binding, so the call's latera' := Floatnever reached it. This also fixeslet before = pending.sum; pending += [1.5], wherebefore's type stayed disconnected frompending's.map/flatMap/Map.mapresults, and materialized lists resolve their element types through these arguments.Maintainer decisions
0. Two cases: a variable only the expected result fixes (none[a: Numeric]: [a!]! { [] }read asnone.sum), or one whose arguments are all null. Passing the list through a typed boundary (an annotatedlet, or a[Float!]!parameter) still fixes it. Fully closing this gap would mean recording each call site's inferred instantiation. The maintainer accepted this gap (decision Q-40, option A), so it stays documented: the collections docs and the skill say so.enclosingVars) and captures only those.evalFrame.typeArgs), not as a context value. That keeps the frame tests' "no walk to the context root" guarantee. The flag for whether a function is generic is computed once, when its type is inferred.[]passed to a generic function now pins that call's variable for good, inference is a little more monomorphic than before. No existing test or golden changed.Tests
TestEmptyCollectionsCarryTheirInferredType(pkg/dang/runtime_types_test.go) compares each empty value's runtime element type against the expected static type. Cases:xs ?? [], an if branch, a reduce accumulator,[] + filtered,mapin a generic body, a literal in a generic body, aletlocal in a generic body, a[]generic argument, a method created during a generic call, and[:]with??.pkg/hm:TestOpenWeakVariablesandTestWeakBindingsReportWeakenedParts.data/collections.mdand the skill's stdlib reference give an empty sum its element type's zero; the collections page also says an empty literal takes its element type from context, matching the syntax page.tests/test_list_sum.dang: Float-division checks for each case, plus generic arguments, blocks, bodies and returns, shadowed nested type variables, and the deferredlet before = pending.sumcase.pkg/dang,pkg/hm,pkg/lsp,cmd/dang,docs/go,tests,docsandfmt/stalepass. I self-reviewed twice and fixed all IMPORTANT findings: nested-variable shadowing, methods capturing a call's arguments,[]passed to generic parameters, and generic return coercion.