Skip to content

Feature Request: Allow const with struct types #3073

@dijdzv

Description

@dijdzv

Is your feature request related to a problem? Please describe.

Currently, const only accepts primitive types. When defining color constants for a graphics library, I cannot use const:

struct Color {
  r : Int
  g : Int
  b : Int
  a : Double
}

const RED : Color = { r: 255, g: 0, b: 0, a: 1.0 }
// ❌ Error: Color is not a valid constant type, only immutable primitive types are allowed.

Describe the solution you'd like

Allow const with struct types when all fields are constant expressions:

const RED : Color = { r: 255, g: 0, b: 0, a: 1.0 }  // Would like this to work

Describe alternatives you've considered

Using pub let works as a workaround:

pub let red : Color = { r: 255, g: 0, b: 0, a: 1.0 }

Additional context

  • Environment: moon 0.1.20251227 (nightly, feature: rupes_recta)
  • I'm imagining something similar to Rust's const structs:
// rust

struct Color { r: u8, g: u8, b: u8, a: u8 }

const BLACK: Color = Color { r: 0, g: 0, b: 0, a: 255 };

fn main() {
    println!("Black: ({}, {}, {}, {})", BLACK.r, BLACK.g, BLACK.b, BLACK.a);
}
  • I do think that this should be part of the core library and should be delivered to all the MoonBit developers.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions