We Have Named Arguments at Home

23 days ago by esa to c/rust

Steve Klabnik recently wrote about named arguments, optional arguments, default arguments, function overloading, and why most of that design space has historically made him nervous in Rust. I agree with Steve. In fact, I think I agree slightly more s…
FizzyOrange 10 points 22 days ago

I agree - Structs are kind of a nicer solution here. Especially because it's pretty common to pass a whole set of options through the call stack.

In Python land you end up with functions with 30 arguments and when they want to pass them anywhere they give up and use kwargs which are completely awful and should almost never be used.

path: 0 26004353, hotness: undefined, score: 10, children: 0
BB_C 4 points 22 days ago

Remember that this has no runtime cost and is fully type-safe.

For the sake of maximal accuracy, enum size overhead can become relevant sometimes. But a work-around exists (boxing the huge variant), and with or without boxing, the performance would still beat whatever other languages have to offer.

Otherwise, good post (I would have to say that since it mostly rehashes points I made in previous comments šŸ˜‰).

path: 0 26005351, hotness: undefined, score: 4, children: 0
sukhmel 3 points 22 days ago

This one:

fn resize(width: u32, height: u32) {}
fn offset(dx: u32, dy: u32) {}

let f: fn(u32, u32) = if resizing { resize } else { offset };

gets rewritten to something non-equivalent:

struct Size {
    width: u32,
    height: u32,
}

fn resize(size: Size) {}

// No more arguing about arguments
let f: fn(Size) = resize;

But I would say that the original idea of applying wildly different functions depending on a flag shouldn't be easily achievable, because it's a bad idea a lot of times.

path: 0 26006067, hotness: undefined, score: 3, children: 0
rust
rust

@programming.dev

login for more options
8329
1423
339

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits
  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

go to feed...