digitalmars.D.learn - Setting struct as default parameter of a function using struct
- BoQsc (25/25) Sep 11 2023 https://docarchives.dlang.io/v2.073.0/spec/struct.html#struct-literal
- Salih Dincer (18/20) Sep 11 2023 If I were you I would use enum, look at my code:
- ryuukk_ (10/36) Sep 11 2023 I would love to be able to use C style designated initialization
- H. S. Teoh (14/23) Sep 11 2023 Someone should seriously come up with a way of eliminating the repeated
- Salih Dincer (26/28) Sep 11 2023 Why not allow it to be flexible enough by using a template
- H. S. Teoh (5/11) Sep 11 2023 Because sometimes I want a specific type.
- Salih Dincer (19/20) Sep 11 2023 it's possible...
- H. S. Teoh (10/20) Sep 11 2023 [...]
- Salih Dincer (36/40) Sep 12 2023 I think there are 3 possibilities, leaving aside what Steven
- Steven Schveighoffer (11/21) Sep 11 2023 ```d
- cc (20/24) Sep 12 2023 If the function's defaults will always match the default
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); } ```
Sep 11 2023
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: ```d enum Options { silenceErrors = false } void someFunction (Options option = Options.silenceErrors) { writeln(cast(bool)option); // false } import std.stdio; void main() { someFunction(); } ``` SDB 79
Sep 11 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
Sep 11 2023
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 youSomeone 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
Sep 11 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 79
Sep 11 2023
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:Because sometimes I want a specific type. T -- What did the alien say to Schubert? "Take me to your lieder."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?
Sep 11 2023
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()) { if(option) { assert(false); } else assert(true); } void main() { specificType(); // No error specificType(ST.silenceErrorsOn); // assert failure } ``` SDB 79
Sep 11 2023
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:[...] 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 LongBecause sometimes I want a specific type.it's possible... ```d alias ST = Options; void specificType(ST option = ST())
Sep 11 2023
On Monday, 11 September 2023 at 23:47:33 UTC, H. S. Teoh wrote: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).I think there are 3 possibilities, leaving aside what Steven suggested. Well, since these options will generally be static, why not use a template parameter? Besides, everything can be expressed with a single letter. For example: ```d // Steven suggested... void multiParams(bool silenceErrors = false, bool otherOption = true) { writeln; } // (1) It cannot be customized. auto someFunction(Options option = Options.init) => option;//*/ /*/ (2a) It doesn't run older versions: auto someFunction(Options option = Options(silenceErrors: false)) => option;//*/ /*/ (2b) There's a possibility of confusion: auto someFunction(Options option = Options (false, false) => option;//*/ struct Options { bool silenceErrors = true; bool printDebugs = true; string toString() => format("silenceErrors: %s\nprintDebugs: %s", silenceErrors, printDebugs); } import std.format, std.stdio; void main() { auto foo(T, T option = T.init)() => option; writeln(foo!Options); writeln(someFunction); } ``` SDB 79
Sep 12 2023
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: ```D void someFunction(Options option = Options(silenceErrors: false)) ``` I don't like the useless repeating "option option option", but that's D for you```d 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
Sep 11 2023
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, ```d struct Options { bool silenceErrors = false; } void someFunction(Options option = Options.init) { writeln(option); } ``` else: ```d struct Options { bool silenceErrors = false; } void someFunction(Options option = Options(silenceErrors: true)) { writeln(option); } ```
Sep 12 2023