codeinabox@programming.dev to Rust@programming.devEnglish · 17 hours agoArguing about argumentssteveklabnik.comexternal-linkmessage-square16linkfedilinkarrow-up122
arrow-up122external-linkArguing about argumentssteveklabnik.comcodeinabox@programming.dev to Rust@programming.devEnglish · 17 hours agomessage-square16linkfedilink
minus-squareanton@lemmy.blahaj.zonelinkfedilinkarrow-up7·14 hours agoWith newtypes you can also emulate named args with a single struct. This also allows them to be passed on together. struct RectArgs{ x:i32, y:32, w:32, h:32, } impl Default for RectArgs{...} Then the call site looks like this: rect(RectArgs{ x:0, y:0, w:30, h:20, } // with default arguments rect(RectArgs{ w:30, h:20, .. RectArgs::default () }
minus-squareesa@discuss.tchncs.delinkfedilinkarrow-up1·edit-213 hours agoYeah, though in those cases you might get an extra question about why rect is a function and not a method, e.g. Rect { w: 30, h: 20, .. RectArgs::default() }.do_the_thing()
minus-squareanton@lemmy.blahaj.zonelinkfedilinkarrow-up1·13 hours agoI assumed the rect is a method on something like a graphics context or physics engine, but yeah.
minus-squareesa@discuss.tchncs.delinkfedilinkarrow-up1·13 hours agoYeah, decent assumption, I’ll tone down my comment a bit.
With newtypes you can also emulate named args with a single struct. This also allows them to be passed on together.
struct RectArgs{ x:i32, y:32, w:32, h:32, } impl Default for RectArgs{...}Then the call site looks like this:
rect(RectArgs{ x:0, y:0, w:30, h:20, } // with default arguments rect(RectArgs{ w:30, h:20, .. RectArgs::default () }Yeah, though in those cases you might get an extra question about why
rectis a function and not a method, e.g.Rect { w: 30, h: 20, .. RectArgs::default() }.do_the_thing()I assumed the rect is a method on something like a graphics context or physics engine, but yeah.
Yeah, decent assumption, I’ll tone down my comment a bit.