Skip to content

A struct field with a default should infer its type: type T { ticks = 0 } fails to parse #2097

Closed

Description

@ehartford

Repro (with v0.15.3.0)

type T { ticks = 0, rate = 2.5, alive = true }

fn main:
    let t = T {}
    print(f"{t.ticks} {t.rate} {t.alive}")
error: expected ':'
 --> fieldinfer.w:1:16
1 | type T { ticks = 0, rate = 2.5 }
  |                ^

Expected

A field with a default takes its type from the default, as let and const do. ticks = 0 is an i32 field, and alive = true is a bool field. A float default follows the same contextual rule as a float const (see #2096): rate is f32 when its uses demand f32, and f64 only when nothing says otherwise.

An explicit type stays available, and necessary, where the default alone doesn't decide it. Examples are seed: u32 = 2463534242 (unsigned wrap-around arithmetic) and a field with no default (rocks: Vec[Rock]).

Why

Writing ticks: i32 = 0 repeats what the literal already says. In a raylib program the noise is everywhere: ship: Vector2 = Vector2 { x: 480.0, y: 300.0 }, radius: f32 = 10.0, alive: bool = true. Found writing a tutorial game for withlang-dev/wipe. Related: #2096.

Activity

  1. ehartford commented on Oct 4, 2026

    @ehartford
    ContributorAuthor

    Correction to the issue text: seed: u32 = 2463534242 isn't an example of a field that needs an explicit type. By convention the literal carries its type: seed = 2463534242u32, which already parses as a literal today. The remaining case for an explicit field type is a field with no default, such as rocks: Vec[Rock].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions