Re: pipeProcess output to hash string

2023-09-11 Thread vino via Digitalmars-d-learn
On Monday, 11 September 2023 at 22:08:54 UTC, Christian Köstlin 
wrote:

On 09.09.23 17:44, Vino wrote:

Hi All,

   Request your help on how to convert the output of 
std.process.pipeProcess to hash string


```

auto test(in Redirect redirect=Redirect.stdout | 
Redirect.stderr) {

     import std.process;
     import std.digest.crc;
     import std.stdio: writeln;

  result = std.process.pipeProcess("whoami" ~ "/?", 
redirect);

  auto content = result.stdout.readln;
  auto hash = crc32Of(content);
  return hash;
}
void main() {
   writeln(test);
}
```

Output: All writes the below.
```
[0, 0, 0, 0]
```

Required: Read the completed output and convert it to a single 
hash string.


From,
Vino.B

Good that you could solve your problem already.

Just three remarks:

First I would recommend to use `std.process : execute` instead 
of
`pipeProcess` in this usecase, as this will wait properly for 
the process to exit and it also will collect its output.


Second its always good to test the exit status of the process 
... just in case.


Third I would not look at `stderr` too much .. well behaved 
commandline tools should not put data for machines there.


Kind regards,
Christian


This is just an example, and more over execute has only stdout, 
where as we need all stdin,stdout,stderr, in order to minimize 
the runtime of log running task we run the stdout and stderr in a 
separate thread, the difference is that for pipeproces we have to 
use wait to ensure that the child process is completed, where as 
for execute we do not need to use wait, as the process exist only 
after the completion.


From,
Vino.


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: pipeProcess output to hash string

2023-09-11 Thread Christian Köstlin via Digitalmars-d-learn

On 09.09.23 17:44, Vino wrote:

Hi All,

   Request your help on how to convert the output of 
std.process.pipeProcess to hash string


```

auto test(in Redirect redirect=Redirect.stdout | Redirect.stderr) {
     import std.process;
     import std.digest.crc;
     import std.stdio: writeln;

  result = std.process.pipeProcess("whoami" ~ "/?", redirect);
  auto content = result.stdout.readln;
  auto hash = crc32Of(content);
  return hash;
}
void main() {
   writeln(test);
}
```

Output: All writes the below.
```
[0, 0, 0, 0]
```

Required: Read the completed output and convert it to a single hash string.

From,
Vino.B

Good that you could solve your problem already.

Just three remarks:

First I would recommend to use `std.process : execute` instead of
`pipeProcess` in this usecase, as this will wait properly for the 
process to exit and it also will collect its output.


Second its always good to test the exit status of the process ... just 
in case.


Third I would not look at `stderr` too much .. well behaved commandline 
tools should not put data for machines there.


Kind regards,
Christian


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

}
```