Skip to content

str: no parse() typed by use — a field cannot become a number (fields() landed) #959

Closed

Description

@ehartford

Restated 2026-10-25. str.fields() landed (awk's $N and NF work). What is left: str has no parse(), so a field cannot become a number in a one-liner or ordinary code. The only integer parser is the free function std.string.parse(s) -> i32. Eric's review of #2127 set the shape: one generic parse() whose result type comes from its use (a typed binding, ?? 0 against an i64), with parse[T]() when nothing decides it; never parse_i64.


Original report

What

From the 2026-09-03 sed/awk parity matrix (seed v0.15.1.7-g54651442e). awk's default field splitting is on runs of whitespace with leading/trailing blanks ignored; With's str has only split(sep) on an exact separator, so:

$ printf 'beta  20 y\n' | with -n 'print(line.split(" ").get(1))'
                                                          → "" (the empty field between the two spaces)
$ … | with -n 'print(line.fields().get(1))'
error: unknown method 'fields' for type '&str'

wc -w via split(" ").len() overcounts for the same reason (18 for 17 words on the matrix input). No split_whitespace/fields/words exists anywhere in lib/std (verified by grep) — docs/improve_oneliners.md already lists "Count words on each line" and "Print the first word of each line" under "Missing Unicode and text conveniences" for this reason.

Integer parsing: str has no parse method (parse, parse_int, to_int, to_i64, parse_i64 all "unknown method"); the only parser is std.string.parse(s: &str) -> i32 (lib/std/string.w:236) — a free function, i32 only, and not an implicit one-liner import. awk's $2 + 0, {s += $2} therefore have no direct spelling; docs/improve_oneliners.md "Explicit parsing, typing, and empty-case handling" is the same gap from the cookbook side.

Proposal

  • str.fields() -> Vec[str] (whitespace runs, blanks ignored; the awk/Go/Rust split_whitespace contract), and the -n/-p cheat-sheet maps awk '{print $N}' to line.fields().get(N-1).
  • str.parse_i64() -> Option[i64] (and parse_f64), an Option because a non-number is normal input, so ?/??/unwrap_or read naturally: total = total + line.fields().get(1).parse_i64() ?? 0.
  • Keep std.string.parse as it is or make it the i32 twin; do not widen it silently.

Acceptance: the matrix rows awk '{print $2}', awk -F: NF, awk '{s+=$2} END{print s}' (with #957) and wc -w join the cli-selfhost-one-liner-tests lane. Companions: #957 (state/END), #958 (file operands, -i).

Activity

  1. changed the title [-]str: no whitespace-run field splitting (awk's $N) and no integer parse method — one-liners cannot spell awk '{print $2}' or '$2+0'[/-] [+]str: no parse() typed by use — a field cannot become a number (fields() landed)[/+] on Oct 25, 2026
  2. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced.

    $ cat typed.w
    let line = "a 12 b"
    let f = line.fields()
    let n: i64 = f[1].parse() ?? 0
    print(n + 1)
    $ with run typed.w
    error: unknown method 'parse' for type '&str'
     --> typed.w:3:14
    

    The same error appears for an owned str ("12".parse()). line.fields().len() works and prints the field count.

    pre-release: ordinary code that reads numbers from text hits it, and the shape is already decided by the #2127 review.

  3. ehartford commented on Oct 26, 2026

    @ehartford
    ContributorAuthor

    Fix campaign, 2026-10-25: flagged human attn needed. Left on do-before-release.

    Built and proved (branch fix-959, not merged). pub fn parse[T: Parse]() -> Option[T] on str, strict, typed by its use; trait Parse implemented for the integer types and f64 (strtod's correctly rounded double). Three Sema gaps had to close for it, each with a test:

    • a generic method of a builtin owner (impl str) was never instantiated: "BUG: user generic call lacks a concrete contract";
    • a method type parameter no argument mentions never took the demand on the result: let n: Option[i64] = cell.get() was "cannot infer generic method type parameter";
    • a ?? carrier got no expectation, so let n: i64 = s.parse() ?? 0 could not infer.

    The shape follows the #2127 review, and I recorded the design as delegated decision D160.

    Why it is not merged. The stack's battery found two problems:

    1. text |> parse |> match: in test/behavior/behav_pipeline_match.w and spec_ss09_7_pipeline_match.w now resolves to the new str.parse instead of the program's own fn parse(text: str) -> Result[..], because §9.6 says "that method takes precedence over a same-named free function". With parse on str, any program that pipes a str into its own parse changes meaning.
    2. The semantic ceremony census rose (plain-bits-field-copy-annotation in src 0 → 417, some-at-option-demand in lib 1 → 17). I have not traced it yet; the expectation the Sema changes add is the likely cause.

    Narrowest open question. When a pipeline stage names both a method of the value and a visible free function, and the method is generic with a type parameter nothing at the stage can infer, should the stage fall back to the free function (the method counts as not applicable, extending §9.6's "if no applicable method exists")? Or does a program's own declaration simply win (§18.1)?

    Brief:

    1. Others. Rust has no pipeline, and UFCS-style method lookup prefers inherent methods. F# and Elixir pipelines are always function application, with no method lookup. In D's UFCS a member function wins over a free function.
    2. Spec. §9.6 quoted above; §18.1: "local bindings and explicit use imports win over prelude names".
    3. Mission fit. The program wrote parse and declared it; a std method silently taking the name is a meaning the program did not choose. Program-declared names winning is the with-y reading.
    4. Prediction (70%). Eric rules that a program's own visible function wins over a std method in a pipeline stage (§18.1 over §9.6 for declarations of the program's own module), and keeps the name parse.

    Below the 75% bar for a delegated decision, so it waits. The census rise gets traced when the stage rule is settled.

  4. ehartford commented on Oct 27, 2026

    @ehartford
    ContributorAuthor

    Fixed on main in e040b90 (#2752), delegated as D165 (renumbering the unmerged branch's D160): str.parse[T: Parse]() -> Option[T], strict whole-text parsing typed by its use, for the integer types and f64. The two remaining Sema gaps are closed (a generic method of a builtin owner was never instantiated; a method type parameter no argument mentions now takes the result demand).

    What parked the branch is resolved in the same change:

    • Pipeline collision: a generic method is applicable at a pipeline stage only when the stage can instantiate it (§9.6 as now worded). input |> parse stays the program's own parse (nothing at the stage determines str.parse's T); under let n: Option[i64] = text |> parse the demand determines it and the method applies; with no same-named free function the refusal still names the missing type. Verified as a four-way matrix on stage1. Prediction 80% for this reading of §9.6.
    • Census: the rise was the branch's own ceremony — sixteen Some(...) wrappers at Option demands in the new string.w code and two typed field lets in the new Sema code. Removed; counts are back to main's (plain-bits 416, the D144 allowance ceiling; some-at lib 1, src 7).

    Tests behav_959_str_parse_by_use and behav_959_generic_method_typed_by_result (both fail pre-fix), with the two pipeline_match fixtures as the collision regression.

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

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