October 26, 2017
On 2017-10-24 22:36, H. S. Teoh wrote:
> On Tue, Oct 24, 2017 at 01:22:41PM -0600, Jonathan M Davis via Digitalmars-d wrote:
> [...]
>> Personally, I don't want them in D. If you have enough arguments that
>> it matters, then the function probably has too many parameters or too
>> many similar parameters.
> 
> If a function has too many parameters, or only a small subset of
> parameters need to be something other than some default value, or the
> set of parameters may change frequently, then my default approach is to
> abstract the parameters into a struct:
> 
> 	struct OutputArgs {
> 		string filename;
> 		int width = 300;
> 		int height = 300;
> 		string fontDir = "/usr/share/local/fonts/arial.ttf";
> 		int fontSize = 12;
> 		Color background = Color.black;
> 		Color foreground = Color.white;
> 		bool antiAlias = true;
> 	}
> 
> 	void generateOutput(OutputArgs args) { ... }
> 
> 	void main() {
> 		// Setup function arguments.
> 		// N.B.: only .filename needs to be explicitly set.
> 		OutputArgs args;
> 		args.filename = "/tmp/output.png";
> 
> 		// Call function with mostly default arguments.
> 		generateOutput(args);
> 	}

For this, it would be nice if static initialization [1] work without temporary variables. Then it would be pretty close to named parameters.

[1] https://dlang.org/spec/struct.html#static_struct_init

-- 
/Jacob Carlborg
October 26, 2017
On Tuesday, 24 October 2017 at 17:30:27 UTC, Andrey wrote:
> Hello, why there are no named arguments for functions..

Named arguments for functions:
 - is useful..perhaps.
 - is desirable..debatable.
 - can provide a significant service to real programmers...??

If a case is to be made, that last item is where it needs to focus its attention.

Useful and desirable is just syntactic sugar...and too many languages focus on adding syntactic sugar...I don't like it.

October 27, 2017
On 2017-10-25 16:58, jmh530 wrote:

> You're passing the function arguments as template parameters. Usually you want them to be able to be passed at run-time.

No problem:

$ cat main.d

import std.stdio;

void foo(args...)()
{
    writeln(args);
}

void main(string[] args)
{
    foo!(args);
}

$ dmd main.d
$ ./main foo bar
["./main", "foo", "bar"]

-- 
/Jacob Carlborg
October 27, 2017
On Friday, 27 October 2017 at 06:45:03 UTC, Jacob Carlborg wrote:
> On 2017-10-25 16:58, jmh530 wrote:
>
>> You're passing the function arguments as template parameters. Usually you want them to be able to be passed at run-time.
>
> No problem:
>
> $ cat main.d
>
> import std.stdio;
>
> void foo(args...)()
> {
>     writeln(args);
> }
>
> void main(string[] args)
> {
>     foo!(args);
> }
>
> $ dmd main.d
> $ ./main foo bar
> ["./main", "foo", "bar"]

Color me surprised. Even the below compiles and runs without error

import std.stdio;

void foo(args...)()
{
    writeln(args);
}

void main()
{
    int x1 = 1;
    int x2 = x1 + 1;
    foo!(x1, x2);
}


1 2 3
Next ›   Last »