News for hackers. Stuff that matters.
Re:Matt Godbolt sold me on Rust by showing me C++ by GardenLetter27 on Tuesday May 06, 2025 @02:26PM (#43908148)
It's a shame Rust doesn't have keyword arguments or named tuples to make handling some of these things easier without Args/Options structs boilerplate.
Re:Matt Godbolt sold me on Rust by showing me C++ by frankus on Tuesday May 06, 2025 @02:59PM (#43908500)
I work all day in Swift (which makes you go out of your way to omit argument labels) and I'm surprised they aren't more common.
Re:Matt Godbolt sold me on Rust by showing me C++ by kevincox on Wednesday May 07, 2025 @06:57AM (#43914192)
Yeah, this is one of the few things that I love about Swift. I think it gets it exactly right that keyword arguments should be the default and you can opt out in cases where the keyword is really unnecessary.
Re:Matt Godbolt sold me on Rust by showing me C++ by jsat on Tuesday May 06, 2025 @03:17PM (#43908653)
Had the same thought... It's backwards that any language isn't using named parameters at this point.
Re:Matt Godbolt sold me on Rust by showing me C++ by Ygg2 on Tuesday May 06, 2025 @04:41PM (#43909426)
Named parameters do come with a large footgun. Renaming your parameters is a breaking change.

Especially if you're coming from different langs.

Re:Matt Godbolt sold me on Rust by showing me C++ by const_cast on Wednesday May 07, 2025 @12:10AM (#43912194)
This only really applies to languages that don't check this at compile-time. I don't consider compile-time errors a foot gun. I mean, it should be impossible for that kind of bad code to ever get merged in most reasonable CI/CD processes.
Re:Matt Godbolt sold me on Rust by showing me C++ by Ygg2 on Wednesday May 07, 2025 @03:47AM (#43913166)
No? This happens in any language that has keyword args.

If I delete/rename a field of a class in any statically checked language, it's going to report a compile error, and it's still a breaking change.

Re:Matt Godbolt sold me on Rust by showing me C++ by const_cast on Wednesday May 07, 2025 @05:31PM (#43920766)
Sure, but it doesn't actually matter because this can't ever manifest as a bug. Your PR will just be rejected, which to me, is correct behavior.

Typically when you're changing a name you're changing behavior, too. This isn't just something we should let slip under the radar, otherwise bugs can actually manifest.

Re:Matt Godbolt sold me on Rust by showing me C++ by Spivak on Wednesday May 07, 2025 @06:02AM (#43913869)
I guess but you're changing your user-visible API so it should be a breaking change. In languages that don't have this type/arity is all that matters and the name is just nice sugar for the implementor who doesn't have to bind them to useful names.

Even if you don't use keyword args your parameter names are still part of your API surface in Python because callers can directly name positional args. Only recently have you been able enforce unnamed positional only args as well as the opposite.

Re:Matt Godbolt sold me on Rust by showing me C++ by _flux on Friday May 09, 2025 @12:37PM (#43938686)
E.g. OCaml has the ability to separate argument name and variable name for named arguments: let foo ~argument:variable = variable + variable

In fact, I think OCaml has one of the best labeled argument system around there. The only downside is that it doesn't always interact well with currying, but perhaps languages without currying could just copy all the rest.

Just to elaborate a bit, in OCaml you can have functions like:

    let foo ~a ?b c =
      let b = match b with
        | None -> 42
        | Some x -> x
      in a + b + c
And you can then call this like foo ~a:1 ~b:42 55 or foo ~a:2 ?b:None 55. But then forwarding those optional parameters works like:

    let bar ~a ?b c =
      foo ~a ?b c
and the optional parameter b will be forwarded as an optional parameter.

Given Rust's historical relations with OCaml I'm slightly disappointed that it doesn't have the same labeled and optional argument system.

Re:Matt Godbolt sold me on Rust by showing me C++ by shpongled on Tuesday May 06, 2025 @02:44PM (#43908333)
Yep, I would love anonymous record types, ala StandardML/OCaml
Re:Matt Godbolt sold me on Rust by showing me C++ by dcdgo on Tuesday May 06, 2025 @11:56PM (#43912118)
Rust Tuples can be destructured into named variables, or Enums can be used as Monads which give a label to a tuple of variables. Rust Enums are real fun to use so I encourage you to dive in. https://doc.rust-lang.org/rust-by-example/custom_types/enum....
Re:Matt Godbolt sold me on Rust by showing me C++ by Gazoche on Wednesday May 07, 2025 @04:54AM (#43913516)
Agreed, coming from Python it's one of the main things I miss in Rust. You can achieve something similar with the builder pattern or with structs + the Default trait, but it takes much more effort.