Skip to content

💥 Numeric literals with underscore after decimal point #5124

@abs0luty

Description

@abs0luty

Gleam accepts underscores as digit separators in numeric literals. When an underscore appears immediately after the decimal point in a float literal (for example 0._1), the Erlang backend compiles and runs correctly, but the JavaScript backend emits an invalid JS literal and fails at parse time.

import gleam/io

pub fn main() {
  let x = 0.1

  case x {
    0._1 -> "bar"
    _ -> "foo"
  }
  |> io.println
}

Running this code on erlang target produces:

  Compiling test_project
   Compiled in 0.27s
    Running test_project.main
bar

Running on the JavaScript target fails during parsing. The generated JS (Node.js v24.5.0) includes:

  if (x === 0._1) {
            ^^

Node output:

SyntaxError: Invalid or unexpected token
    at compileSourceTextModule (node:internal/modules/esm/utils:357:16)
    at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:108:18)
    at #translate (node:internal/modules/esm/loader:550:12)
    at ModuleLoader.loadAndTranslate (node:internal/modules/esm/loader:597:27)
    at async #link (node:internal/modules/esm/module_job:180:19)

Node.js v24.5.0

Expected behavior

Both backends should accept the program and print bar. The JS backend should emit a valid JavaScript numeric literal, such as 0.1, instead of 0._1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedContributions encouraged

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions