Setting struct as default parameter of a function using struct literal?
Salih Dincer
salihdb at hotmail.com
Mon Sep 11 22:05:11 UTC 2023
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?
```d
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 at 79
More information about the Digitalmars-d-learn
mailing list