Skip to content

fix(fmt): format a reflect.Value as the value it holds - #544

Merged
paralin merged 1 commit into
s4wave:devfrom
cogentcore:fix/fmt-reflect-value
Oct 25, 2026
Merged

paralin merged 1 commit into
s4wave:devfrom
cogentcore:fix/fmt-reflect-value

Conversation

@rcoreilly

Copy link
Copy Markdown
Contributor

Go's fmt treats a valid reflect.Value operand specially: it formats the value the Value holds, so fmt.Sprint(reflect.ValueOf(5)) is 5 and fmt.Sprintf("%+v", reflect.ValueOf(p)) prints the struct's fields. goscript's fmt printed the Value's type instead (int, main.pt, []int), and %d printed NaN. Test code that prints the reflect.Value results of an evaluation (for example a Go interpreter's test suite, which compares fmt.Sprintf("%v", result)) failed throughout because of it.

The two places every operand passes through, defaultFormatMaybe at depth 0 (Print, Println and the default verb) and formatOperandMaybe (the Printf verbs), now replace a valid reflect.Value by Interface(), as Go's printArg does. An invalid Value is left alone and still formats as <invalid reflect.Value>. A reflect.Value nested inside another value is unchanged, as in Go, which unwraps only top-level operands.

package_import_fmt checks %v, %d, %s and %+v, and Println with a slice and an invalid Value.

Go's fmt formats a valid reflect.Value operand as the value it holds:
fmt.Sprint(reflect.ValueOf(5)) is "5". goscript's printed the Value's
type, "int", and %d gave NaN. The operand is now replaced by its value,
for both the print and the printf verbs; an invalid Value still formats
as <invalid reflect.Value>.

Signed-off-by: Randall C. O'Reilly <oreilly@ucdavis.edu>
@paralin
paralin changed the base branch from master to dev October 25, 2026 18:38
@paralin
paralin merged commit f424a06 into s4wave:dev Oct 25, 2026
7 checks passed
@paralin

paralin commented Oct 25, 2026

Copy link
Copy Markdown
Member

Landed on master as 621317e, rebased onto the batch #532 to #550, with regenerated fixtures in fca8071.

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.

2 participants