News for hackers. Stuff that matters.
Re:Jiff: A brand new Datetime library for Rust, from the aut... by DemocracyFTW2 on Monday July 22, 2024 @02:02AM (#41031298)
could you do that with `struct` / `record` fields? In JavaScript which doesn't have named function parameters either I often write functions with a single `cfg` parameter that are called like `f({ hours: 2, seconds: 53, })` which I find nice b/c it re-uses existing data structures.
Re:Jiff: A brand new Datetime library for Rust, from the aut... by devanl on Monday July 22, 2024 @02:37AM (#41031487)
In Rust, you can't implicitly omit fields when instantiating a struct, so it would have to be a bit more verbose, explicitly using Rust's analog to the spread syntax.

It would have to look something like:

  f({ hours: 2, seconds: 53, ..Default::default() })

The defaults could come from some value / function with a name shorter than Default::default(), but it would be less clear.
Re:Jiff: A brand new Datetime library for Rust, from the aut... by estebank on Monday July 22, 2024 @10:35AM (#41034916)
Adding support for struct default field values would allow for

- leaving some mandatory fields

- reduce the need for the builder pattern

- enable the above to be written as f(S { hours: 2, seconds: 53, .. })

If that feature ever lands, coupled with structural/anonymous structs or struct literal inference, you're getting everything you'd want from named arguments without any of the foot guns.

Re:Jiff: A brand new Datetime library for Rust, from the aut... by kibwen on Monday July 22, 2024 @01:33PM (#41037050)
Has anyone ever proposed it? It's such a straightforward feature with such obvious semantics ("default field values are const contexts") and impossible-to-bikeshed syntax (`a: i32 = 42`) that I've been meaning to write up an RFC myself for around, oh, ten years now...
Re:Jiff: A brand new Datetime library for Rust, from the aut... by steveklabnik on Tuesday July 23, 2024 @12:32AM (#41042509)
nrc was poking at the problem in 2016 https://internals.rust-lang.org/t/struct-field-defaults/3412 which led to this RFC https://github.com/rust-lang/rfcs/pull/1806

It got postponed because it wasn't going to make it into Rust 2018: https://github.com/rust-lang/rfcs/pull/1806#issuecomment-327...

Re:Jiff: A brand new Datetime library for Rust, from the aut... by estebank on Tuesday July 23, 2024 @11:52AM (#41047364)
Last time I had this conversation[1] the main sticking point were around semantics of private fields, but I am convinced that the maximally restrictive version can be done (only pub fields can be optional) and maybe relaxed later after we get some real world experience. The other thing was about whether the default values needed to be const, but that makes sense as a restriction to me.

1: https://internals.rust-lang.org/t/pre-pre-rfc-syntactic-suga...

Re:Jiff: A brand new Datetime library for Rust, from the aut... by steveklabnik on Tuesday July 23, 2024 @11:58AM (#41047454)
Yeah makes sense! I haven’t felt the need for this feature personally, but it feels like it fits with the language pretty well to me. The big issue with default values (as far as I’m concerned) is for them to always have a default value, and this doesn’t change the opt-in nature of the concept.
Re:Jiff: A brand new Datetime library for Rust, from the aut... by estebank on Tuesday July 23, 2024 @12:21PM (#41047784)
The issue I have with Default is that the verbosity jump from derive(Default) to impl Default for Type is high, evej if you just want to specify a single field. The other reason I want this is because it makes defining builders almost trivial for any case where custom logic isn't needed for the fields. I would expect the impl-based builder pattern to become more niche if we had this feature. Your reason is yet another that would be nice to address as well. I like how this one change addresses multiple things at once.
Re:Jiff: A brand new Datetime library for Rust, from the aut... by steveklabnik on Tuesday July 23, 2024 @01:12PM (#41048460)
Yeah, full agreement here. I almost said something along the lines of "it's almost like extending #[derive(Default)] with serde-like attributes to control which fields get what defaults, but with nicer syntax," but got a little lazy with it since we're in agreement anyway. It is annoying that deriving Default is all or nothing.

I didn't even think about the builder thing, but you're right there too.

Re:Jiff: A brand new Datetime library for Rust, from the aut... by Galanwe on Monday July 22, 2024 @02:30AM (#41031447)
Well the beautiful thing about software engineering is that pretty much everything is possible, it essentially boils down to "but should you really"? :-)
Re:Jiff: A brand new Datetime library for Rust, from the aut... by 4hg4ufxhy on Monday July 22, 2024 @02:48AM (#41031542)
Kind of inefficient. Also it's less ergonomic since every struct is it's own type, so you need to have the signature on both sides.
Re:Jiff: A brand new Datetime library for Rust, from the aut... by benmmurphy on Monday July 22, 2024 @03:48AM (#41031820)
It should compile to the same because a struct passed by value is loaded into the registers the same as method arguments.