• esa@discuss.tchncs.de
    link
    fedilink
    arrow-up
    6
    ·
    4 hours ago

    I’m used to named arguments from other languages, and that makes me fairly partial to them. Having to tool around with builders feels a lot more complex and clunky.

    That said, I do wonder at how many of the usecases aren’t workarounds for long argument lists that are either stringly typed, or something similar. As in, with a signature of (i32,i32,i32,i32) -> whatever, named arguments seem like a way of getting the compiler to catch errors in confusing {x,y}_{position,length}; but if the signature were newtypes like Rect(Origin(i32,i32), Size(i32, i32)) -> whatever, then the felt need for named arguments drops considerably.

    • anton@lemmy.blahaj.zone
      link
      fedilink
      arrow-up
      5
      ·
      2 hours ago

      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 ()
      }
      
      • esa@discuss.tchncs.de
        link
        fedilink
        arrow-up
        1
        ·
        edit-2
        1 hour ago

        Yeah, 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()