News for hackers. Stuff that matters.
Re:Rust 1.64.0 by curious145454 on Thursday September 22, 2022 @11:03AM (#32939819)
> This takes a bit more code to implement, but provides a simpler API for users.

I fail to see how hiding self.send() inside of IntoFuture makes anything simpler.

StorageRequest::new().set_debug(true).send().await?;

vs

StorageRequest::new().set_debug(true).await?;

Just reading the later variant makes me wonder "What are we (a)waiting for here? For set_debug to succeed?". I'd definitely stick to the former variant.

Perhaps, it's just not the best example and there are better usages than the one chosen to illustrate it.

Re:Rust 1.64.0 by david2ndaccount on Thursday September 22, 2022 @12:52PM (#32941504)
It’s sad that Rust has decided to settle with the builder pattern instead of implementing named arguments.
Re:Rust 1.64.0 by davidatbu on Friday September 23, 2022 @06:40AM (#32950125)
I'm pretty sure that there's no objection to including named arguments in Rust as of yet. It's just that no one has wanted it badly enough to write an RFC for, and implement, named arguments.
Re:Rust 1.64.0 by cyber_kinetist on Thursday September 22, 2022 @12:57PM (#32941594)
Can’t you pass “argument” structs as parameters to kinda emulate the feel of named arguments (along with using the Default trait)? I use this in C++ a lot, and it seems that some Rust devs are doing this too.
Re:Rust 1.64.0 by kevincox on Thursday September 22, 2022 @01:30PM (#32942057)
You can, but it is a mouthful. Especially if you want optional arguments.

    foo::some_func(foo::SomeFuncArgs{
      a: 8,
      b: Some(false),
      ..Default::default()
    })
Personally I would be really happy if `..` could default to `..Default::default()` which would make this a lot cleaner. But even then needing to name a type for the argument struct is noisy. Language-integrated keyword arguments would make this a lot cleaner:

    foo::some_func(
        a=8,
        b=Some(false))
Re:Rust 1.64.0 by cyber_kinetist on Saturday September 24, 2022 @06:20AM (#32961233)
By the way, C++20 designated initializers works like a charm for this.

  foo::some_func({
    .a = 8, 
    .b = false
  });
(I view this and std::span as the only two usable features in C++20, and pretty much everything else can go to the dustbin or the drawing board.)
Re:Rust 1.64.0 by estebank on Monday September 26, 2022 @09:25AM (#32982814)
I'm waiting to getting some spare time to propose (not quite but very close) that in an RFC. A lot of conversation about it from a while back is available at https://internals.rust-lang.org/t/pre-pre-rfc-syntactic-suga...
Re:Rust 1.64.0 by cercatrova on Friday September 23, 2022 @12:12AM (#32947969)
Indeed, it's basically the same as in JS/TS where you just pass an object of key/values rather than have named arguments.
Re:Rust 1.64.0 by kevincox on Friday September 23, 2022 @06:39AM (#32950117)
Yes, but it is a lot cleaner there because you don't need to specify the name of the type and you don't need to do anything special for optional arguments.

I wouldn't mind if it worked this way "under the hood" but syntax sugar for passing that last argument as keyword arguments would be a fantastic quality of life improvement.

Re:Rust 1.64.0 by maxbond on Thursday September 22, 2022 @01:09PM (#32941761)
Why? The builder pattern is great.
Re:Rust 1.64.0 by kevincox on Thursday September 22, 2022 @01:32PM (#32942081)
It gets awkward really fast when you may need to set a value.

    let mut foo = build_foo()
        .with_a(8);

    if need_to_set_b {
        foo = foo.with_b(false);
    }

    let foo = foo.build();
Re:Rust 1.64.0 by maxbond on Thursday September 22, 2022 @01:44PM (#32942244)
Would look very similar with named arguments, no?
Re:Rust 1.64.0 by svieira on Thursday September 22, 2022 @02:44PM (#32942967)
Re:Rust 1.64.0 by kevincox on Thursday September 22, 2022 @01:51PM (#32942337)
Maybe. It depends a bit on how they are implemented. If there is a good way to pass "default" then you can just have a condition for that one argument. But if there is no way to pass "default" then it can get hairy.
Re:Rust 1.64.0 by maxbond on Thursday September 22, 2022 @04:15PM (#32944050)
For sure. I would like to see that in Rust as well, that would be nice. I am willing to put up with the lack of variadic & named arguments for the goodness the borrow checker brings me, but I can see how that would improve the ergonomics substantially.
Re:Rust 1.64.0 by joseluis on Thursday September 22, 2022 @01:22PM (#32941932)
Having both approaches available would be better IMHO. Builder pattern is very nice but I frequently miss having named arguments too. Even if macros can partially fullfill that need, it's not the same and introduces more quirks and complexity...
Re:Rust 1.64.0 by Ygg2 on Thursday September 22, 2022 @04:37PM (#32944307)
Problem with named parameters is that they make renaming function params a breaking change.
Re:Rust 1.64.0 by metaltyphoon on Friday September 23, 2022 @12:22AM (#32948030)
Unless the IDE doesn’t support this refactoring? It’s ridiculously easy in C# and VS or VSC.
Re:Rust 1.64.0 by setr on Friday September 23, 2022 @12:53AM (#32948205)
A (public) library doesn't have access to all codebases depending on it.
Re:Rust 1.64.0 by Ygg2 on Friday September 23, 2022 @05:26AM (#32949660)
Even there changing the method param will invalidate semver specification.
Re:Rust 1.64.0 by maxbond on Thursday September 22, 2022 @01:43PM (#32942225)
Fair enough.
Re:Rust 1.64.0 by ameixaseca on Thursday September 22, 2022 @02:39PM (#32942903)
Please note the programmer needs to implement "IntoFuture" and has full control on what and how it would be translated into a future. The example is implemented like this, but it does not have to be.

And yes, the example is not the best.

Re:Rust 1.64.0 by minraws on Thursday September 22, 2022 @05:04PM (#32944612)
Aah, yeah that felt a bit off at the time of reading, nice note. I barely had time to keep up with this update and the changes... :p
Re:Rust 1.64.0 by Jatidude on Thursday September 22, 2022 @11:21AM (#32940116)
I think the concept is that you are awaiting the "request type". And getting back a "response type". The example is somewhat confusing since set_debug() returns a StorageRequest.

With tooling this would make your resulting type be a StorageResponse. The whole point is that you can have that "request type" be awaitable.

Re:Rust 1.64.0 by curious145454 on Thursday September 22, 2022 @11:29AM (#32940286)
Apparently, there's another meaning to word "simpler", of which I'm unaware :D
Re:Rust 1.64.0 by pornel on Thursday September 22, 2022 @12:46PM (#32941439)
You may be thinking of "primitive", "simplistic" or "spartan". Things that are simple to use are not necessarily primitive.

It's simpler for library users, because they don't need to know which particular method finalizes a builder to obtain a future from it, and `.await` just works in more situations.

Re:Rust 1.64.0 by minraws on Thursday September 22, 2022 @05:12PM (#32944695)
That's not quite the best example as someone else noted, you could better of write an API where converting to Future can be a shore of in places where await may work in the Future.

Say, `for await x in connection.item {}` not real syntax but something similar might be in Rust's future.