3 ms·
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
by cyber_kinetist 4y ago
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.
- kevincox 4y agoYou 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))
- cercatrova 4y agoIndeed, it's basically the same as in JS/TS where you just pass an object of key/values rather than have named arguments.
- kevincox 4y agoYes, 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.
- cyber_kinetist 4y agoBy 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.)
- estebank 4y agoI'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-sugar-for-default-default/13234/75 https://internals.rust-lang.org/t/pre-pre-rfc-syntactic-suga...