Discussion Thread: DIP 1030--Named Arguments--Final Review

Виталий Фадеев vital.fadeev at gmail.com
Sat Jul 3 15:46:47 UTC 2021


https://forum.dlang.org/post/osiiuzwesgmaegekodif@forum.dlang.org

On Monday, 11 May 2020 at 11:37:07 UTC, Mike Parker wrote:
> This is the discussion thread for the Final Review of DIP 1030, 
> "Named Arguments":
>
> https://github.com/dlang/DIPs/blob/7d114c93edb02d8fc4b05f0716bdb6057905fec2/DIPs/DIP1030.md

I dream it will be like this.

1. Of course, named args delimiter is ':'. Same in struct 
initialization:

     Border b = { width: 1 };

     struct Border {
         int width;
     };


2. Named args in Class constructor:

     // usage
     auto inp = new Input( border: 1 );

     // class
     class Input
     {
        Border border;

        this() {} // human-coded ctor

        // auto generated from "new Row( border: 1 )"
        //   if not found human-coded ctor with same args
        this( Border border )
        {
            this.border = border;

            this(); // call human-coded ctor
        }
     }

3. Named args in Class constructor merged with ctor args:

     // usage
     auto inp = new Input( "Label text", border: 1 );

     // class
     class Input
     {
        Border border;
        string label_text;

        // human-coded ctor
        this( string label_text )
        {
            this.label_text = label_text;
        }

        // auto generated from "new Row( "Label text", border: 1 )"
        //   if not found human-coded ctor with same args
        this( string label_text, Border border )
        {
            this.border = border;

            this( label_text ); // call human coded ctor
        }
     }

4. Of course, all inline:

     // usage
     auto inp = new Input( border: 1, "Label text" );

     // class
     class Input
     {
        Border border;
        string label_text;

        // human coded ctor
        this( string label_text )
        {
            this.label_text = label_text;
        }

        // auto generated from "new Row( border: 1, "Label text" )"
        this( Border border, string label_text )
        {
            // from named args
            this.border = border;

            // inlined from human coded ctor
            this.label_text = label_text;
        }
     }

5. Class fields is named arguments for ctor.

     // for class:
     class Input
     {
        Border border;
        string label_text;
     }

     // possible calls
     auto a1 = Input( border: 1 );
     auto a2 = Input( label_text: "Score" );
     auto a3 = Input( border: 1, label_text: "Score" );
     auto a4 = Input( label_text: "Score", border: 1 );

6. Function named args:

     // usage
     borderedInput( border: 1 );

     // function
     auto borderedInput( Border border )
     {
         return new Input( border: border );
     }


And then we will write clean code for UI, like in Dart/Flutter:

     auto inp = new Input(
         "Label text",
         border : 1,
         child  : new Image(
             src: "info.png"
         )
     );



More information about the Digitalmars-d mailing list