Setting struct as default parameter of a function using struct literal?

ryuukk_ ryuukk.dev at gmail.com
Mon Sep 11 19:59:37 UTC 2023


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:

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

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


More information about the Digitalmars-d-learn mailing list