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

2023-09-12 Thread Salih Dincer via Digitalmars-d-learn

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


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

2023-09-12 Thread cc via Digitalmars-d-learn

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);
}
```



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

2023-09-11 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Sep 11, 2023 at 10:39:00PM +, 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


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

2023-09-11 Thread Steven Schveighoffer via Digitalmars-d-learn

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


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

2023-09-11 Thread Salih Dincer via Digitalmars-d-learn

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


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

2023-09-11 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Sep 11, 2023 at 10:05:11PM +, 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."


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

2023-09-11 Thread Salih Dincer via Digitalmars-d-learn

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


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

2023-09-11 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Sep 11, 2023 at 07:59:37PM +, 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


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

2023-09-11 Thread ryuukk_ via Digitalmars-d-learn

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


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

2023-09-11 Thread Salih Dincer via Digitalmars-d-learn

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



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

2023-09-11 Thread BoQsc via Digitalmars-d-learn

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);

}
```