conv.

All stories
TechActive today · day 3

Rust blog post on missing named/optional arguments sparks language-design debate

A developer essay comparing Dart, C#, TypeScript and Rust argument syntax draws a detailed Lobsters thread on how Rust might add the feature.

What to know

  • Rust currently has no native named or optional function arguments; common workarounds use verbose Default-implementing structs that only work when every parameter has a default.
  • A blog post comparing Dart, C#, TypeScript, Python and Rust's handling of the feature drew a large Lobsters discussion after a quieter Hacker News posting the day before.
  • Commenters largely agree Rust's situation is weak and cite Swift, Objective-C, Gleam and OCaml as languages with well-designed separation of internal parameter names and external call labels.
  • The main point of contention is whether parameter names should automatically become part of a function's public API (risking breaking changes on rename) or require an explicit, opt-in design.

The dispute Whether parameter names should automatically become part of a function's public API (Python-style) or be explicitly opt-in per argument (Swift/Gleam/ObjC-style), given the breaking-change risk of the former. · positions read across 40 posts and comments

most voices

Rust should get named/optional arguments; current struct-and-Default workarounds are painful

  • “I would be so happy if this rfc got implemented. I regularly wish for named and optional parameters in my own code, and I get extremely tired of wading though dozens of similar-but-not-quite functions on docs.rs to find the one that I need.”

    drmorr · Lobsters ↗
many voices

Other languages already show good models — Swift, ObjC, Gleam and OCaml separate internal names from call-site labels

  • “Swift's approach to this is pretty neat, it separates internal parameter names and external argument labels, so you can rename the parameter without affecting the argument, and the other way around.”

    masklinn · Lobsters ↗
some voices

Named parameters as public API should be opt-in, not automatic, to avoid fragile breaking changes

  • “I would certainly not want any functions currently written suddenly be callable with named args because then all of a sudden changing an argument's name is a breaking change for crate authors. This would have to be explicit on the part of…”

    ekuber · Lobsters ↗
some voices

Existing crates like bon already cover much of this need without a language change

  • “https://crates.io/crates/bon Is a good option for some of these use cases. But it's not a thing I would build a public crate interface on/with.”

    schneems · Lobsters ↗

botahamec Blog authorekuber Lobsters commenterschneems Lobsters commenter

How it unfolded 5 developments, newest first · click a bar or a number to jump postscomments

Peak 4 pieces in one hour at Sep 22, 6 AM; 44 pieces over 4 days (4 posts · 40 comments) Sep 20, 4 PM — 1 piece · 1 post — Hacker News 1Sep 20, 5 PM — quietSep 20, 6 PM — quietSep 20, 7 PM — quietSep 20, 8 PM — quietSep 20, 9 PM — quietSep 20, 10 PM — quietSep 20, 11 PM — quietSep 21, 12 AM — quietSep 21, 1 AM — quietSep 21, 2 AM — quietSep 21, 3 AM — quietSep 21, 4 AM — quietSep 21, 5 AM — quietSep 21, 6 AM — quietSep 21, 7 AM — quietSep 21, 8 AM — quietSep 21, 9 AM — quietSep 21, 10 AM — quietSep 21, 11 AM — quietSep 21, 12 PM — quietSep 21, 1 PM — quietSep 21, 2 PM — quietSep 21, 3 PM — quietSep 21, 4 PM — 1 piece · 1 post — Lobsters 1Sep 21, 5 PM — quietSep 21, 6 PM — quietSep 21, 7 PM — 1 piece · 1 comment — Lobsters 1Sep 21, 8 PM — 2 pieces · 2 comments — Lobsters 2Sep 21, 9 PM — 2 pieces · 2 comments — Lobsters 2Sep 21, 10 PM — 1 piece · 1 comment — Lobsters 1Sep 21, 11 PM — 2 pieces · 2 comments — Lobsters 2Sep 22, 12 AM — 1 piece · 1 comment — Lobsters 1Sep 22, 1 AM — 1 piece · 1 comment — Lobsters 1Sep 22, 2 AM — 3 pieces · 3 comments — Lobsters 3Sep 22, 3 AM — 1 piece · 1 comment — Lobsters 1Sep 22, 4 AM — quietSep 22, 5 AM — 2 pieces · 2 comments — Lobsters 2Sep 22, 6 AM — 4 pieces · 1 post · 3 comments — Lobsters 3, Hacker News 1Sep 22, 7 AM — 3 pieces · 3 comments — Lobsters 3Sep 22, 8 AM — quietSep 22, 9 AM — 2 pieces · 2 comments — Lobsters 2Sep 22, 10 AM — quietSep 22, 11 AM — quietSep 22, 12 PM — quietSep 22, 1 PM — 1 piece · 1 comment — Lobsters 1Sep 22, 2 PM — 3 pieces · 3 comments — Lobsters 3Sep 22, 3 PM — quietSep 22, 4 PM — 1 piece · 1 comment — Lobsters 1Sep 22, 5 PM — quietSep 22, 6 PM — 2 pieces · 2 comments — Lobsters 2Sep 22, 7 PM — 1 piece · 1 comment — Lobsters 1Sep 22, 8 PM — quietSep 22, 9 PM — 1 piece · 1 comment — Lobsters 1Sep 22, 10 PM — quietSep 22, 11 PM — quietYesterday, 12 AM — quietYesterday, 1 AM — quietYesterday, 2 AM — quietYesterday, 3 AM — 1 piece · 1 comment — Lobsters 1Yesterday, 4 AM — quietYesterday, 5 AM — quietYesterday, 6 AM — quietYesterday, 7 AM — 1 piece · 1 comment — Lobsters 1Yesterday, 8 AM — 1 piece · 1 comment — Lobsters 1Yesterday, 9 AM — quietYesterday, 10 AM — quietYesterday, 11 AM — 1 piece · 1 post — Hacker News 1Yesterday, 12 PM — 1 piece · 1 comment — Lobsters 1Yesterday, 1 PM — 1 piece · 1 comment — Lobsters 1Yesterday, 2 PM — 1 piece · 1 comment — Lobsters 1Yesterday, 3 PM — quietYesterday, 4 PM — quietYesterday, 5 PM — quietYesterday, 6 PM — quietYesterday, 7 PM — quietYesterday, 8 PM — 1 piece · 1 comment — Lobsters 1Yesterday, 9 PM — quietYesterday, 10 PM — quietYesterday, 11 PM — quietToday, 12 AM — quietToday, 1 AM — quietToday, 2 AM — quietToday, 3 AM — quiet 123–45
Sep 21Sep 22yesterdaynow · 4:44 AM ET
  1. 5

    Thread compares named-argument designs in Swift, ObjC, Gleam and OCaml

    Commenters describe how Swift, Objective-C, Gleam and OCaml separate internal parameter names from external call-site labels, offering possible models for a future Rust design.

    “Swift's approach to this is pretty neat, it separates internal parameter names and external argument labels, so you can rename the parameter without affecting the argument, and the other way around.”
    — masklinn
    • > it makes parameter names part of the public API of the function, I really like the way gleam (and swift too I believe) goes about this. The public parameter name has to be picked explicitly rather than all arguments being named automatically. ```gleam pub fn replace( in string: String, each value: String, with replacement: String, ) -> String {…

      giacomo_cavalieriplt,rust2d ago25▲view on Lobsters ↗
    2 more of the top 3 · 34 posts in this stretch
    • I'm unsure why the parameter names being part of the API is presented as a problem? It's literally the point of feature. You just need to make sure that the syntax permits the implementation to use a different name, because it's the implementation - this is something that objective-c and swift both support, which means APIs are often written as…

      olliejplt,rust2d ago10▲view on Lobsters ↗
    • > it makes parameter names part of the public API of the function, After using Python's equivalent (keyword arguments) and Ada's equivalent (named parameter associations) quite a bit, I went from "parameter names being part of the API is bad" to "perhaps this isn't a bad idea." You're forced to think more about clear parameter names, which…

      pyjplt,rust2d ago9▲view on Lobsters ↗
    all of them →
  2. 4

    Commenters debate API-stability risks of named parameters

    Some argue making parameter names part of the public API would turn renaming into a breaking change, and suggest any such Rust feature should be explicit and opt-in for API designers rather than automatic.

    “I would certainly not want any functions currently written suddenly be callable with named args because then all of a sudden changing an argument's name is a breaking change for crate authors.”
    — ekuber
    • OCaml’s [labeled & optional arguments](https://ocaml.org/docs/labels) using `~` + `?` sigils is pretty novel in the functional space. I ended up liking it quite a lot ergonomically since a) you can easily forward along arguments, but also the labels can be alias for the scope meaning you don’t need to come up with argument names! A trivial…

      toastalplt,rust2d ago9▲view on Lobsters ↗
    2 more of the top 3 · 4 posts in this stretch
    • THIS. (And Swift also does named/optional parameters well, IMO.) This has been one of my pain points in moving from Swift to Rust.

      snejplt,rust2d ago5▲view on Lobsters ↗
    • I like ObjC's solution: ``` example(named: 1, arg: 2) ``` desugars to something like: ``` example_named_arg(1, 2) ``` The arg order is fixed, but Rust's syntax error can suggest autofix. Lack of support for all the possible permutations is a plus: makes it harder to go wild with overly configurable methods. It doesn't need any new object types. It…

      kornelplt,rust2d ago4▲view on Lobsters ↗
    all of them →
  3. 3

    Commenters cite existing Rust workarounds like the bon crate

    Lobsters users note that crates such as bon already provide builder-style named arguments for Rust, though some are wary of exposing them in public crate interfaces.

    “https://crates.io/crates/bon Is a good option for some of these use cases. But it's not a thing I would build a public crate interface on/with.”
    — schneems
    • https://crates.io/crates/bon Is a good option for some of these use cases. But it’s not a thing I would build a public crate interface on/with.

      schneemsplt,rust2d ago5▲view on Lobsters ↗
    1 more of the top 2 · 2 posts in this stretch
    • I would be so happy if this rfc got implemented. I regularly wish for named and optional parameters in my own code, and I get extremely tired of wading though dozens of similar-but-not-quite functions on docs.rs to find the one that I need. On the flip side, as soon as you introduce optional parameters you open the floodgates for something like…

      drmorrplt,rust2d ago2▲view on Lobsters ↗
    all of them →
  4. 2

    Essay resurfaces on Lobsters

    The same essay is submitted to Lobsters a day later and generates a much larger, more technical comment thread comparing language designs.

  5. 1

    Essay submitted to Hacker News

    The post appears on Hacker News with a small early discussion (2 comments).

    “Whenever someone asks what my least favorite part of Rust is, my answer is always the same: it doesn't have named or optional arguments.”
    — botahamec, blog author · source
  6. background

    botahamec publishes essay on Rust's missing named/optional arguments — The post argues Rust is the hardest of several tolerated languages to get named arguments out of, and walks through how Dart, C#, TypeScript and Rust each handle (or fail to handle) named and optional parameters.

What people are saying 16 voices from 1 site · best of 40 · verbatim

Still unanswered
  • Would a hypothetical Rust RFC for named arguments apply automatically to existing functions or require explicit opt-in?
  • Which existing-language model (Swift/ObjC label separation vs. OCaml's ~/? sigils vs. Python-style keyword args) would fit Rust's stability guarantees best?