Jump to page: 1 2
Thread overview
Setting struct as default parameter of a function using struct literal?
Sep 11
BoQsc
Sep 11
ryuukk_
Sep 13
cc
September 11

https://docarchives.dlang.io/v2.073.0/spec/struct.html#struct-literal

I would like to set function's default struct for a function in a way that it would be visible for the reader to see what options are set. Something like Options option = {silenceErrors: false}

Here is an example of what I would hope for to work but it surely does not work:

import std.stdio;

struct Options {
    bool silenceErrors = false;
}

void someFunction(Options option = {silenceErrors: false}){
	writeln(option);

}

void main() {
	someFunction();
}

Is it possible to make this part work in a simple way, maybe I'm using a wrong syntax here?

void someFunction(Options option = {silenceErrors: false}){
	writeln(option);

}
September 11

On Monday, 11 September 2023 at 17:51:04 UTC, BoQsc wrote:

>

Here is an example of what I would hope for to work but it surely does not work:

If I were you I would use enum, look at my code:

enum Options
{
  silenceErrors = false
}

void someFunction (Options option = Options.silenceErrors)
{
  writeln(cast(bool)option); // false
}

import std.stdio;
void main()
{
  someFunction();
}

SDB@79

September 11

On Monday, 11 September 2023 at 17:51:04 UTC, BoQsc wrote:

>

https://docarchives.dlang.io/v2.073.0/spec/struct.html#struct-literal

I would like to set function's default struct for a function in a way that it would be visible for the reader to see what options are set. Something like Options option = {silenceErrors: false}

Here is an example of what I would hope for to work but it surely does not work:

import std.stdio;

struct Options {
    bool silenceErrors = false;
}

void someFunction(Options option = {silenceErrors: false}){
	writeln(option);

}

void main() {
	someFunction();
}

Is it possible to make this part work in a simple way, maybe I'm using a wrong syntax here?

void someFunction(Options option = {silenceErrors: false}){
	writeln(option);

}

I would love to be able to use C style designated initialization everywhere too..

Recent version of D added named arguments so you can do something like:

void someFunction(Options option = Options(silenceErrors: false))

I don't like the useless repeating "option option option", but that's D for you

September 11
On Mon, Sep 11, 2023 at 07:59:37PM +0000, ryuukk_ via Digitalmars-d-learn wrote: [...]
> Recent version of D added named arguments so you can do something like:
> 
> ```D
> void someFunction(Options option = Options(silenceErrors: false))
> ```
> 
> I don't like the useless repeating "option option option", but that's D for you

Someone should seriously come up with a way of eliminating the repeated type name in default parameters.  It's a constant fly in my otherwise tasty soup of D.  Every time I have to type that I think about how nice it would be if we could just write

	void someFunction(Options option = .init) {...}

and be done with it.  Or else:

	void someFunction(auto options = Options.init) {}

though this is not as good because the `auto` may make it hard to parse function declarations.


T

-- 
Life would be easier if I had the source code. -- YHL
September 11

On Monday, 11 September 2023 at 20:17:09 UTC, H. S. Teoh wrote:

>

Someone should seriously come up with a way of eliminating the repeated type name in default parameters.

Why not allow it to be flexible enough by using a template parameter?

enum Options : bool
{
  silenceErrorsOff, silenceErrorsOn
}

struct Opts
{
  bool silenceErrors;
}

void someFunction(T)(T option = T())
{
  import std.stdio;
  writeln(cast(bool)option); // true
}

void main()
{
  Opts o;
  someFunction(o.silenceErrors = true);

  with(Options)
  someFunction(silenceErrorsOn);
}

SDB@79

September 11
On Mon, Sep 11, 2023 at 10:05:11PM +0000, Salih Dincer via Digitalmars-d-learn wrote:
> On Monday, 11 September 2023 at 20:17:09 UTC, H. S. Teoh wrote:
> > 
> > Someone should seriously come up with a way of eliminating the repeated type name in default parameters.
> 
> Why not allow it to be flexible enough by using a template parameter?

Because sometimes I want a specific type.


T

-- 
What did the alien say to Schubert? "Take me to your lieder."
September 11

On Monday, 11 September 2023 at 22:13:25 UTC, H. S. Teoh wrote:

>

Because sometimes I want a specific type.

it's possible...

alias ST = Options;
void specificType(ST option = ST())
{
  if(option)
  {
    assert(false);
  } else
    assert(true);
}

void main()
{
  specificType(); // No error
  specificType(ST.silenceErrorsOn); // assert failure
}

SDB@79

September 11

On Monday, 11 September 2023 at 19:59:37 UTC, ryuukk_ wrote:

>

I would love to be able to use C style designated initialization everywhere too..

Recent version of D added named arguments so you can do something like:

void someFunction(Options option = Options(silenceErrors: false))

I don't like the useless repeating "option option option", but that's D for you

void someFunction(bool silenceErrors = false, bool otherOption = true)

someFunction(otherOption: false); // works

I know options structs have benefits besides allowing parameter specification, but with the latest allowance of named parameters, it's worth exploring whether you still want to use an options struct or not.

-Steve

September 11
On Mon, Sep 11, 2023 at 10:39:00PM +0000, Salih Dincer via Digitalmars-d-learn wrote:
> On Monday, 11 September 2023 at 22:13:25 UTC, H. S. Teoh wrote:
> > 
> > Because sometimes I want a specific type.
> > 
> 
> it's possible...
> 
> ```d
> alias ST = Options;
> void specificType(ST option = ST())
[...]

This is missing the point.  The point is that I don't want to have to type `Options` or `ST` twice.  Since the type of the parameter is already known, the compiler does not need me to repeat the type name. It already knows enough to figure it out on its own.  "Don't Repeat Yourself" (DRY).


T

-- 
"Holy war is an oxymoron." -- Lazarus Long
September 13

On Monday, 11 September 2023 at 17:51:04 UTC, BoQsc wrote:

>

I would like to set function's default struct for a function in a way that it would be visible for the reader to see what options are set. Something like Options option = {silenceErrors: false}

If the function's defaults will always match the default initializers of the struct itself,

struct Options {
    bool silenceErrors = false;
}
void someFunction(Options option = Options.init) {
	writeln(option);
}

else:

struct Options {
    bool silenceErrors = false;
}
void someFunction(Options option = Options(silenceErrors: true)) {
	writeln(option);
}
« First   ‹ Prev
1 2