Re: How to make a generic function to take a class or struct by reference?

2022-03-27 Thread vit via Digitalmars-d-learn

On Sunday, 27 March 2022 at 16:27:44 UTC, JN wrote:
I would like to have only one definition of getX if possible, 
because they both are doing the same thing. I can't remove the 
ref one, because without a ref it will pass the struct as a 
temporary and compiler won't like that.


```d
import std.stdio;

struct Foo
{
int x;

void doStuff()
{
*getX(this) = 5;
}
}

class Bar
{
int x;

void doStuff()
{
*getX(this) = 5;
}
}

int* getX(T)(ref T t)
{
return &t.x;
}

int* getX(T)(T t)
{
return &t.x;
}

void main()
{
Foo foo;
Bar bar = new Bar();

foo.doStuff();
bar.doStuff();

assert(foo.x == 5);
assert(bar.x == 5);
}
```


Try this:
```d

int* getX(T)(return auto ref T t){
static if(is(T == class))
return &t.x;
else static if(is(T == struct) && __traits(isRef, t))
return &t.x;
else
static assert(0, "no impl " ~ T.stringof);
}
```
or:

```d

int* getX(T)(return auto ref T t)
if(is(T == class) || (is(T == struct) && __traits(isRef, t))){
return &t.x;
}
```


Re: Windows Msys terminal not flushing on newlines

2022-03-27 Thread Anonymouse via Digitalmars-d-learn

On Sunday, 27 March 2022 at 18:09:30 UTC, Adam D Ruppe wrote:
Normally the IOLBF thing does help there - that means line 
buffering - but my recommentation is to explicitly call 
`stdout.flush()` any time it is important in your code. Then 
you aren't depending on the relatively hidden config value.


All right, thanks.


Re: Windows Msys terminal not flushing on newlines

2022-03-27 Thread kdevel via Digitalmars-d-learn

Don't know if this is OT here.

On Sunday, 27 March 2022 at 18:09:30 UTC, Adam D Ruppe wrote:
If the C library thinks it is talking to a pipe, it will switch 
to block buffering instead of line buffering. It must just 
think msys is a pipe (since it probably is under the hood).


while compiling a project with make -j6 I see this:

[...]
decimal/decimal.d(13080): decimal/decimal.d(13080): Deprecation: 
Deprecation: UUssagea goef  otfh et h`e `bodbyody` k`e ykweoyrwdo 
rids  idse pdreepcraetceadt.e dU.s eU s`e `dodo` i`n sitnesatde.a

d.
decimal/decimal.d(13217): decimal/decimal.d(13217): Deprecation: 
Deprecation: UUssaaggee  ooff  tthhee  ``bbooddyy``  
kkeeyywwoorrdd  iiss  ddeepprreeccaatteedd..  UUssee  ``ddoo``  
iinnsstteeaadd..


decimal/decimal.d(13239): Deprecation: decimal/decimal.d(13239): 
UDeprecation: sageU soafg et hoef  `the b`odybo`d ykey`w okredy 
wiosr dd eipsr edceaptreedc.a tUesde.  `Use d`o`d oins`t eiands.t

ead.
decimal/decimal.d(13327): decimal/decimal.d(13327): Deprecation: 
Deprecation: UUssaaggee  ooff  tthhee  ``bbooddyy``  
kkeeyywwoorrdd  iiss  ddeepprreeccaatteedd..  UUssee  ``ddoo``  
iinnsstteeaadd..


The write calls are mostly unbuffered as strace reveals:

29680 write(2, "\33[1m", 4) = 4
29680 write(2, "decimal/decimal.d(471): ", 24) = 24
29680 write(2, "\33[1;36m", 7)  = 7
29680 write(2, "Deprecation: ", 13) = 13
29680 write(2, "\33[m", 3)  = 3
29680 write(2, "U", 1)  = 1
29680 write(2, "s", 1)  = 1
29680 write(2, "a", 1)  = 1
29680 write(2, "g", 1)  = 1
29680 write(2, "e", 1)  = 1
29680 write(2, " ", 1)  = 1
29680 write(2, "o", 1)  = 1
29680 write(2, "f", 1)  = 1
29680 write(2, " ", 1)  = 1
29680 write(2, "t", 1)  = 1
29680 write(2, "h", 1)  = 1
29680 write(2, "e", 1)  = 1
29680 write(2, " ", 1)  = 1

Normally the IOLBF thing does help there - that means line 
buffering - but my recommentation is to explicitly call 
`stdout.flush()` any time it is important in your code. Then 
you aren't depending on the relatively hidden config value.


Shall I file an issue for this?


Re: How to make a generic function to take a class or struct by reference?

2022-03-27 Thread Steven Schveighoffer via Digitalmars-d-learn

On 3/27/22 12:27 PM, JN wrote:

int* getX(T)(T t)
{
     return &t.x;
}


Remove this one. It's a bad idea. It probably won't compile, but if it 
does, you will be using corrupted memory.


-Steve


Re: Windows Msys terminal not flushing on newlines

2022-03-27 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 27 March 2022 at 17:46:54 UTC, Anonymouse wrote:
I installed Git for Windows which comes with the Msys terminal, 
and I noticed writeln lines aren't being flushed on linebreaks


If the C library thinks it is talking to a pipe, it will switch 
to block buffering instead of line buffering. It must just think 
msys is a pipe (since it probably is under the hood).


Normally the IOLBF thing does help there - that means line 
buffering - but my recommentation is to explicitly call 
`stdout.flush()` any time it is important in your code. Then you 
aren't depending on the relatively hidden config value.


Windows Msys terminal not flushing on newlines

2022-03-27 Thread Anonymouse via Digitalmars-d-learn
I installed Git for Windows which comes with the Msys terminal, 
and I noticed writeln lines aren't being flushed on linebreaks. I 
had this a long time ago and thought I fixed it with the 
following, but I guess I never confirmed that it actually worked. 
I'm not sure where I copied it from, a TWiD perhaps?


```d
import std.stdio : stdout;
import core.stdc.stdio : _IOLBF;
stdout.setvbuf(4096, _IOLBF);  // arbitrary number
```

Is this not the right way to go about things? At least for Msys 
it seemingly does nothing.


Re: How to make a generic function to take a class or struct by reference?

2022-03-27 Thread drug via Digitalmars-d-learn

Auto ref?

```D

int* getX(T)(auto ref T t)
{
...
```



How to make a generic function to take a class or struct by reference?

2022-03-27 Thread JN via Digitalmars-d-learn
I would like to have only one definition of getX if possible, 
because they both are doing the same thing. I can't remove the 
ref one, because without a ref it will pass the struct as a 
temporary and compiler won't like that.


```d
import std.stdio;

struct Foo
{
int x;

void doStuff()
{
*getX(this) = 5;
}
}

class Bar
{
int x;

void doStuff()
{
*getX(this) = 5;
}
}

int* getX(T)(ref T t)
{
return &t.x;
}

int* getX(T)(T t)
{
return &t.x;
}

void main()
{
Foo foo;
Bar bar = new Bar();

foo.doStuff();
bar.doStuff();

assert(foo.x == 5);
assert(bar.x == 5);
}
```


Re: Help needed to learn typeof(return)

2022-03-27 Thread Vinod K Chandran via Digitalmars-d-learn
On Sunday, 27 March 2022 at 01:11:02 UTC, Steven Schveighoffer 
wrote:


Not sure what the question here is,

Thanks for the reply. Actually, my problem was this, I forgot the 
presence of `LargerOf!(A, B)` template function in that chapter. 
When I see it in a function, I thought where is the 
implementation of this function ? But later I found it.



And yes, you can get into paradoxical problems like:

```d
auto foo()
{
   typeof(return) x;
   return x;
}
```

which will not compile.

Yeah, I got the point. In fact, after reading the `Template` 
chapter in that book, I am amazed. There are lot of possibilities 
in D templates.





Re: Example of Windows SSL with Secure Channel?

2022-03-27 Thread janrinok via Digitalmars-d-learn

On Monday, 21 March 2022 at 17:30:31 UTC, Marcone wrote:
I hope one day the creators of the D programming language will 
implement std.socket.ssl ​​in the Phobos library.
I have to agree.  For those just getting to grips with D there 
are some things in the library that just make using them much 
more difficult than they ought to be, or altogether impossible.