Re: What are these functions called and how to implement they?

2021-01-28 Thread dog2002 via Digitalmars-d-learn

On Thursday, 28 January 2021 at 18:56:15 UTC, frame wrote:

On Thursday, 28 January 2021 at 18:27:09 UTC, frame wrote:


[...]


A very simple example:

bool myEventA = true;
bool myEventB = false;

// event source that generates the event (must be called to run)
void source() {
observe(myEventA);
}

// routine that decides what handler to call
void observe(bool event) {
switch (event) {
case true:
onMyEventA(event);
break;

case false:
onMyEventB(event);
break;

default:
assert(0);
}
}

// handler
void onMyEventA(bool event) {
// do something
}

void onMyEventB(bool event) {
// do something
}


Thank you, I'll try it


Re: How do I compose pipes?

2021-01-28 Thread Anthony via Digitalmars-d-learn

On Friday, 29 January 2021 at 03:49:38 UTC, Ali Çehreli wrote:

On 1/28/21 3:45 PM, Anthony wrote:

> void end(AccumulatorPipe acc) {
>  auto pids = acc.pids ~ P.spawnShell("cat", acc.stdout);
>
>  foreach (pid; pids) {
>  P.wait(pid);
>  }
> }
> ```
>
>
> So now I can do something like:
> ```
> run("find source -name '*.d'")
>  .pipe("entr ./make.d tests")
>  .end(),

Cool but there should be one improvement because I don't think 
end() is guaranteed to be executed in that code, which may 
leave zombie processes around. From 'man waitpid':


  "A child that terminates, but has not been waited for becomes 
a "zombie".


Which is relayed to std.process documentation as "to avoid 
child processes becoming "zombies"".


Ali


I take it you're referring to missing scope guards like in your 
code

`scope (exit) wait(lsPid);`

Yeah, that is a tricky one. I can't think of a way to have a nice 
interface that also closes the pids on exit since scope guards 
are handled on function exit in this case.


Perhaps thats just the nature of the problem though.

I'll take a look at what scriptlike does 
https://github.com/Abscissa/scriptlike#script-style-shell-commands


Re: How do I compose pipes?

2021-01-28 Thread Ali Çehreli via Digitalmars-d-learn

On 1/28/21 3:45 PM, Anthony wrote:

> void end(AccumulatorPipe acc) {
>  auto pids = acc.pids ~ P.spawnShell("cat", acc.stdout);
>
>  foreach (pid; pids) {
>  P.wait(pid);
>  }
> }
> ```
>
>
> So now I can do something like:
> ```
> run("find source -name '*.d'")
>  .pipe("entr ./make.d tests")
>  .end(),

Cool but there should be one improvement because I don't think end() is 
guaranteed to be executed in that code, which may leave zombie processes 
around. From 'man waitpid':


  "A child that terminates, but has not been waited for becomes a "zombie".

Which is relayed to std.process documentation as "to avoid child 
processes becoming "zombies"".


Ali



Re: unittests and imported modules

2021-01-28 Thread H. S. Teoh via Digitalmars-d-learn
To answer your other question:

On Thu, Jan 28, 2021 at 07:44:59PM +, kdevel via Digitalmars-d-learn wrote:
[...]
> After inserting print statements into the other modules I found that
> the additional four unittests originate from the imported files. Is
> there a trick to get only the unittest from  run other than
> dropping the "-i" and specifiying the other object files on the
> command line?

AFAIK it's currently not possible with the built-in test runner. But
there are dub packages for replacement test runners that may offer this
functionality.


> And why are the unittests from the imported phobos functions not run?

Phobos was hacked to only run unittests when StdUnittest is defined.
It's a workaround for the lameness that unittests from *all* imported
modules are run with a -unittest build, including from external modules
and libraries you may not be concerned with.


T

-- 
The easy way is the wrong way, and the hard way is the stupid way. Pick one.


Re: Why filling AA in shared library freezes execution?

2021-01-28 Thread Siemargl via Digitalmars-d-learn

On Friday, 29 January 2021 at 00:45:12 UTC, Siemargl wrote:

Then i modify program, just removing DLL, copying TestFun() in 
main module and it runs.

Same compiler -m64 target.


Ups. Sorry, I just forget copy test_dll.dll inside VM :-)

So, program runs in Win7, but hangs after printing i:64511


I downgrade DMD to 2.090.1 + MSVC2013 libs and problem disappears.

But 2.092 + MSVC2013 libs also hangs. Not every time, but




Re: Why filling AA in shared library freezes execution?

2021-01-28 Thread Siemargl via Digitalmars-d-learn

On Thursday, 28 January 2021 at 12:42:09 UTC, Siemargl wrote:

Update. Something is broken in DLL support in druntime for Win7.

I take previous working in Win10 binary and try run it in 
virtual Windows 7 SP1 x64.

Got this
C:\Proj\dtest>test_dll_exe.exe

object.Exception@test_dll_exe.d(7): Enforcement failed

0x0001400013A3
0x0001400012CC
0x000140001074
0x000140004123
0x000140003FAC
0x00014000408B
0x000140003FAC
0x000140003EF3
0x0001400019D0
0x000140001114
0x00014002F60E
0x771359CD in BaseThreadInitThunk
0x7736A561 in RtlUserThreadStart

Then i modify program, just removing DLL, copying TestFun() in 
main module and it runs.

Same compiler -m64 target.


Ups. Sorry, I just forget copy test_dll.dll inside VM :-)

So, program runs in Win7, but hangs after printing i:64511



Re: How do I compose pipes?

2021-01-28 Thread Anthony via Digitalmars-d-learn

On Thursday, 28 January 2021 at 17:18:46 UTC, Ali Çehreli wrote:

On 1/28/21 2:16 AM, Anthony wrote:

> auto p = pipeProcess("ls");
> auto q = pipeProcess("cat", stdin = p.stdout); //it would be
good to do

That would work if `cat` received the *contents* of the files 
(and with a "-" command line switch). Since `ls` produces file 
names, you would have to make the complete `cat` command line 
from `ls`'s output.


> Do I need to manually extract the output from
pipes.stdin.readln

Seems to be so for the `ls | cat` case. But the following `find 
| grep` example shows how two ends of pipes can be connected:


import std.stdio;
import std.process;
import std.range;

// BONUS: Enable one of the following lines to enjoy an issue.
// version = bonus_bug;
// version = bonus_bug_but_this_works;

void main() {
  // Writes to 'a':
  auto a = pipe();
  auto lsPid = spawnProcess([ "find", "."], stdin, a.writeEnd);
  scope (exit) wait(lsPid);

  // Reads from 'a', writes to 'b':
  auto b = pipe();
  auto catPid = spawnProcess([ "grep", "-e", `\.d$` ], 
a.readEnd, b.writeEnd);

  scope (exit) wait(catPid);

  version (bonus_bug) {
// Fails with the following error.
//
// "/usr/include/dmd/phobos/std/typecons.d(6540): Error:
// `"Attempted to access an uninitialized payload."`"
writefln!"Some of the D source files under the current 
directory:\n%-(  %s\n%)"(

  b.readEnd.byLine);

  } else version (bonus_bug_but_this_works) {
// Note .take at the end:
writefln!"Some of the D source files under the current 
directory:\n%-(  %s\n%)"(

  b.readEnd.byLine.take(1000));

  } else {
// The results are read from 'b':
writeln(b.readEnd.byLine);
  }
}

I've discovered a strange issue, which can be observed by 
uncommenting the 'version = bonus_bug;' line above. But comment 
that one back in and uncomment the next line, now it works. (?)


Ali



Thanks Ali.
I was messing around and below seems to work well enough for me.

```

struct AccumulatorPipe {
Pid[] pids;

File stdin;
File stdout;
}

AccumulatorPipe run(string cmd) {
AccumulatorPipe acc;

auto p = P.pipeShell(cmd, P.Redirect.stdout);

acc.pids ~= p.pid;
acc.stdout = p.stdout;

return acc;
}

AccumulatorPipe pipe(AccumulatorPipe acc0, string cmd) {
AccumulatorPipe acc;
Pipe p = P.pipe();

acc.stdin = p.writeEnd;
acc.stdout = p.readEnd;

auto pid = P.spawnShell(cmd, acc0.stdout, acc.stdin);
acc.pids = acc0.pids ~ pid;

return acc;
}

void end(AccumulatorPipe acc) {
auto pids = acc.pids ~ P.spawnShell("cat", acc.stdout);

foreach (pid; pids) {
P.wait(pid);
}
}
```


So now I can do something like:
```
run("find source -name '*.d'")
.pipe("entr ./make.d tests")
.end(),
```


That would work if `cat` received the *contents* of the files 
(and with a "-" command line switch)


I was actually trying to use cat to just spit out the filenames 
of the directory as a test.

But I see what you mean.



Re: emplace doesn't forward aeguments

2021-01-28 Thread kinke via Digitalmars-d-learn

On Thursday, 28 January 2021 at 21:15:49 UTC, vitamin wrote:
Is there reason why std.conv.emplace doesn't forward arguments 
to __ctor?


Yeah, a bug in the emplace() version for classes, some missing 
`forward!args` in there (it works when emplacing a struct with 
identical ctor). E.g. 
https://github.com/dlang/druntime/blob/e2e304e1709b0b30ab65471a98023131f0e7620c/src/core/lifetime.d#L124-L128 if you want to fix it (std.conv.emplace is now an alias for core.lifetime.emplace in Phobos master).


Re: F*cked by memory corruption after assiging value to associative array

2021-01-28 Thread tsbockman via Digitalmars-d-learn

On Thursday, 28 January 2021 at 20:17:09 UTC, frame wrote:

On Thursday, 28 January 2021 at 19:22:16 UTC, tsbockman wrote:
It is possible to get things sort of working with on Windows, 
anyway.


I'm ok with it as long as the memory is not re-used by the GC. 
It seems that it can be prevented with addRoot() successfully.


GC.addRoot is not enough by itself. Each GC needs to know about 
every single thread that may own or mutate any pointer to memory 
managed by that particular GC.


If a GC doesn't know, memory may be prematurely freed, and 
therefore wrongly re-used. This is because when it scans memory 
for pointers to find out which memory is still in use, an 
untracked thread may be hiding a pointer on its stack or in 
registers, or it might move a pointer value from a location late 
in the scanning order to a location early in the scanning order 
while the GC is scanning the memory in between, such that the 
pointer value is not in either location *at the time the GC 
checks it*.


You won't be able to test for this problem easily, because it is 
non-deterministic and depends upon the precise timing with which 
each thread is scheduled and memory is synchronized. But, it will 
probably still bite you later.


If you were just manually creating additional threads unknown to 
the GC, you could tell the GC about them with 
core.thread.osthread.thread_attachThis and thread_detachThis. 
But, I don't know if those work right when there are multiple 
disconnected copies of D runtime running at the same time like 
this.


The official solution is to get the GC proxy connected properly 
from each DLL to the EXE. This is still very broken on Windows in 
other ways (again, explained at my link), but it should at least 
prevent the race condition I described above, as well as being 
more efficient than running multiple GCs in parallel.


Alternatively, you can design your APIs so that no pointer to GC 
memory is ever owned or mutated by any thread unknown to that GC. 
(This is the only option when working across language boundaries.)


emplace doesn't forward aeguments

2021-01-28 Thread vitamin via Digitalmars-d-learn
Is there reason why std.conv.emplace doesn't forward arguments to 
__ctor?


this doesn't work:

import std.conv : emplace;
import std.functional : forward;

struct Bar{

@disable this(const ref typeof(this) rhs)pure nothrow @safe 
@nogc;

}

class Foo{
Bar bar;

this(Bar bar){
this.bar = forward!bar;
}
}

void main(){
void[__traits(classInstanceSize, Foo)] tmp = void;

emplace!Foo(cast(Foo)tmp.ptr, Bar.init);   //error
}


Re: Compile time check for GC?

2021-01-28 Thread Kagamin via Digitalmars-d-learn

You can make it opt in, it's insurance.


Re: unittests and imported modules

2021-01-28 Thread kdevel via Digitalmars-d-learn

On Thursday, 28 January 2021 at 20:03:34 UTC, H. S. Teoh wrote:
[...]

   5 unittests passed


Which version of dmd is this?


$ dmd --version
DMD64 D Compiler v2.093.1
Copyright (C) 1999-2020 by The D Language Foundation, All Rights 
Reserved written by Walter Bright


In the latest releases, this inaccurate message has been 
replaced with:


5 modules passed unittests

The count displayed is not the number of unittests, but the 
number of *modules* that passed unittests.  I've no idea why 
the original message was worded in such a misleading way, but 
it should have been fixed by now.


DMD Versions > v2.094.1 expose a library issue here on my machine 
such that
I can only run the 32-Bit version of the compiler (which 
nonetheless

produces working 64-Bit-Code).



Re: F*cked by memory corruption after assiging value to associative array

2021-01-28 Thread frame via Digitalmars-d-learn

On Thursday, 28 January 2021 at 19:22:16 UTC, tsbockman wrote:
It is possible to get things sort of working with on Windows, 
anyway.


I'm ok with it as long as the memory is not re-used by the GC. It 
seems that it can be prevented with addRoot() successfully. The 
other problem with shared slice data is somewhat logical as the 
DLL GC doesn't care on the origin of the data from another thread 
and the data's origin GC sees any reference to it gone after 
passing it to the DLL function. They are isolated and data which 
must be kept longer should be copied where it's necessary.


Re: unittests and imported modules

2021-01-28 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jan 28, 2021 at 07:44:59PM +, kdevel via Digitalmars-d-learn wrote:
> Today I moved some functions to a new module within the same package
> and ran
> 
>dmd -g -i -unittest -checkaction=context -main -run .
> 
> dmd reported
> 
>5 unittests passed

Which version of dmd is this?  In the latest releases, this inaccurate
message has been replaced with:

5 modules passed unittests

The count displayed is not the number of unittests, but the number of
*modules* that passed unittests.  I've no idea why the original message
was worded in such a misleading way, but it should have been fixed by
now.


T

-- 
Why did the mathematician reinvent the square wheel?  Because he wanted
to drive smoothly over an inverted catenary road.


unittests and imported modules

2021-01-28 Thread kdevel via Digitalmars-d-learn
Today I moved some functions to a new module within the same 
package

and ran

   dmd -g -i -unittest -checkaction=context -main -run .

dmd reported

   5 unittests passed

I would have expected that only the one unittest in  
would

have been compiled. After inserting print statements into the
other modules I found that the additional four unittests originate
from the imported files. Is there a trick to get only the unittest
from  run other than dropping the "-i" and specifiying
the other object files on the command line?

And why are the unittests from the imported phobos functions not 
run?


Re: Why does calling readln() more than once not work

2021-01-28 Thread Ruby The Roobster via Digitalmars-d-learn

On Thursday, 28 January 2021 at 19:31:35 UTC, Adam D. Ruppe wrote:
On Thursday, 28 January 2021 at 19:25:52 UTC, Ruby The Roobster 
wrote:

readf(" %d",);


This leaves the \n at the end. A next readf thanks to the 
leading space would ignore that \n and keep going, but a readln 
stops at the first \n it sees, even if it is a leftover item in 
the buffer from a readf before.


I replaced readln with the following: readf(" %s\n",);
That seemed to do the trick(and note here, when you press enter, 
it stops taking input).


Re: Why does calling readln() more than once not work

2021-01-28 Thread Adam D. Ruppe via Digitalmars-d-learn
On Thursday, 28 January 2021 at 19:25:52 UTC, Ruby The Roobster 
wrote:

readf(" %d",);


This leaves the \n at the end. A next readf thanks to the leading 
space would ignore that \n and keep going, but a readln stops at 
the first \n it sees, even if it is a leftover item in the buffer 
from a readf before.


Re: Why does calling readln() more than once not work

2021-01-28 Thread Ruby The Roobster via Digitalmars-d-learn
On Thursday, 28 January 2021 at 19:25:52 UTC, Ruby The Roobster 
wrote:
I call readln() on a variable in a loop. On the next iteration, 
it's as if the readln() is ignored, as it moves on to the next 
line apparently. Here is the code:


for(int i = 1;i<1000;i++)
{
int tempx;
int tempy;
int high = 0;
double highs = 0;
writeln("Type in  data for an egg:");
write("Chicken: ");
readln(tempz);
write("Width: ");
readf(" %d",);
write("Hight: ");
readf(" %d",);
data[i] = new egg(tempx,tempy,cast(string)tempz);
//...
Anything that will fix this?


Here is the declaration for tempz(outside the loop): char[] tempz;


Why does calling readln() more than once not work

2021-01-28 Thread Ruby The Roobster via Digitalmars-d-learn
I call readln() on a variable in a loop. On the next iteration, 
it's as if the readln() is ignored, as it moves on to the next 
line apparently. Here is the code:


for(int i = 1;i<1000;i++)
{
int tempx;
int tempy;
int high = 0;
double highs = 0;
writeln("Type in  data for an egg:");
write("Chicken: ");
readln(tempz);
write("Width: ");
readf(" %d",);
write("Hight: ");
readf(" %d",);
data[i] = new egg(tempx,tempy,cast(string)tempz);
//...
Anything that will fix this?


Re: F*cked by memory corruption after assiging value to associative array

2021-01-28 Thread tsbockman via Digitalmars-d-learn

On Thursday, 28 January 2021 at 07:50:43 UTC, frame wrote:
Under Linux everything is shared. Under Windows each DLL seems 
to run in its own thread, has its own rt_options and do not see 
any __gshared variable value. Its completely isolated and so I 
assume that also GC is.


This stuff works correctly under Linux, and is quite broken in 
Windows. This has been known for years, but hasn't been fixed 
yet. This link for my other reply gives more details:

https://forum.dlang.org/post/veeksndchoppftluj...@forum.dlang.org


Also https://wiki.dlang.org/Win32_DLLs_in_D says: Each EXE and 
DLL will have their own gc instance.


They each have their own GC instance because no one has fully 
fixed the problems discussed at my link, above, not because it's 
actually a good idea for them each to have their own GC instance.


It is possible to get things sort of working with on Windows, 
anyway. But, this requires either:


A) Following all the same rules that you would need to follow if 
you wanted to share D GCed memory with another thread written in 
C. (Just adding GC roots is not enough.)


B) Ensuring that the GC proxy connections are properly 
established before doing anything else. This doesn't actually 
work correctly or reliably, but it might work well enough for 
your use case. Maybe.


Re: Why am I getting a dividing by zero error message

2021-01-28 Thread tsbockman via Digitalmars-d-learn
On Thursday, 28 January 2021 at 18:37:37 UTC, Ruby The Roobster 
wrote:

Here is the output/input of the program:
Type in  data for an egg:
Width: 3
Hight: 2

object.Error@(0): Integer Divide by Zero
...

Here is the source code:

import std.stdio;
import std.string;
void main(){
egg[1000] data;
data[0] = (new egg(0,0,"a"));
for(int i = 1;i<1000;i++)
{
...
data[i] = new egg(tempx,tempy,tempz);
for(int x = 0;x < i;x++)
{
int tempa;
int tempb;
double temp;
tempa = data[x].x;
if(tempa < 0)
tempa-=tempa;
tempb = data[x].y;
if(tempb < 0)
tempb-=tempb;
temp = tempa / tempb;
if(temp > highs)
{
highs = temp;
high = x;
}
}
...

Why is this happening? Does anybody know?


1) Outside the loops, you set `data[0] = (new Egg(0, 0, "a"))`

2) The outer `for(int i` loop, starts at `i = 1`, and so it never 
overwrites the work of step (1).


3) The inner `for(int x` loop starts at `x = 0`, so its first 
pass divides by `data[1].y`. Since you set that field equal to 0 
in step (1), and both operands have type `uint`, the result is a 
divide by zero error.


4) Also, `if(tempb < 0) tempb-=tempb;` will convert any negative 
values into zeroes. Maybe you meant to write `if(tempb < 0) tempb 
= -tempb;` to get the absolute value, instead?


There are many ways you could fix the program. (I won't try to 
guess what the best way is, though, because it's not entirely 
clear to me what this program is actually intended to do. As is, 
it does some calculations, but doesn't try to output their 
results in any way.)


Re: Why am I getting a dividing by zero error message

2021-01-28 Thread Ruby The Roobster via Digitalmars-d-learn
On Thursday, 28 January 2021 at 18:56:45 UTC, Ruby The Roobster 
wrote:

On Thursday, 28 January 2021 at 18:53:51 UTC, Dennis wrote:
On Thursday, 28 January 2021 at 18:37:37 UTC, Ruby The 
Roobster wrote:

object.Error@(0): Integer Divide by Zero
Why is this happening? Does anybody know?



data[0] = (new egg(0,0,"a"));


Here you set data[0].y to 0


tempb = data[x].y;


In the first iteration, this equals data[0].y which equals 0


temp = tempa / tempb;


And then you divide by zero here, hence the error


Okay. That worked. I added a check to set temp to zero if tempa 
or tempb is zero.


Instead of dividing, which causes the bug.


Re: Why am I getting a dividing by zero error message

2021-01-28 Thread Ruby The Roobster via Digitalmars-d-learn

On Thursday, 28 January 2021 at 18:53:51 UTC, Dennis wrote:
On Thursday, 28 January 2021 at 18:37:37 UTC, Ruby The Roobster 
wrote:

object.Error@(0): Integer Divide by Zero
Why is this happening? Does anybody know?



data[0] = (new egg(0,0,"a"));


Here you set data[0].y to 0


tempb = data[x].y;


In the first iteration, this equals data[0].y which equals 0


temp = tempa / tempb;


And then you divide by zero here, hence the error


Okay. That worked. I added a check to set temp to zero if tempa 
or tempb is zero.


Re: What are these functions called and how to implement they?

2021-01-28 Thread frame via Digitalmars-d-learn

On Thursday, 28 January 2021 at 18:27:09 UTC, frame wrote:

Not 100% sure what you mean but I guess you ask how to 
implement a handler? If an event occurs, a routine decides to 
call your onKeyPressed function and pass the keyCode which was 
pressed. The routine must be registered on a event source that 
will emit the events. The routine is basically just a callback 
and that is indeed called every frame again if an event occurs.


A very simple example:

bool myEventA = true;
bool myEventB = false;

// event source that generates the event (must be called to run)
void source() {
observe(myEventA);
}

// routine that decides what handler to call
void observe(bool event) {
switch (event) {
case true:
onMyEventA(event);
break;

case false:
onMyEventB(event);
break;

default:
assert(0);
}
}

// handler
void onMyEventA(bool event) {
// do something
}

void onMyEventB(bool event) {
// do something
}


Re: Why am I getting a dividing by zero error message

2021-01-28 Thread evilrat via Digitalmars-d-learn
On Thursday, 28 January 2021 at 18:37:37 UTC, Ruby The Roobster 
wrote:

Here is the output/input of the program:
Type in  data for an egg:
Width: 3
Hight: 2

object.Error@(0): Integer Divide by Zero

0x004023FE
0x0040CF9F
0x0040CF19
0x0040CDB4
0x00409033
0x00402638
0x75F86359 in BaseThreadInitThunk
0x77018944 in RtlGetAppContainerNamedObjectPath
0x77018914 in RtlGetAppContainerNamedObjectPath
Chicken:

Here is the source code:

import std.stdio;
import std.string;
void main(){
egg[1000] data;
data[0] = (new egg(0,0,"a"));
for(int i = 1;i<1000;i++)
{
int tempx;
int tempy;
string tempz;
int high = 0;
double highs = 0;
writeln("Type in  data for an egg:");
write("Width: ");
readf(" %d",);
write("Hight: ");
readf(" %d",);
write("Chicken: ");
tempz = readln();
data[i] = new egg(tempx,tempy,tempz);
for(int x = 0;x < i;x++)
{
int tempa;
int tempb;
double temp;
tempa = data[x].x;
if(tempa < 0)
tempa-=tempa;
tempb = data[x].y;
if(tempb < 0)
tempb-=tempb;


/*
x starts with 0, you are acessing data[x] which is set to 
egg(0,0,"a") and you get div by zero as a result. I see logic 
error, though I might be wrong because I haven't actually run 
your code.

*/


temp = tempa / tempb;
if(temp > highs)
{
highs = temp;
high = x;
}
}
tempx = data[high].x - data[i].x;
if(tempx < 0)
tempx-=tempx;
tempy = data[high].y - data[i].y;
if(tempy < 0)
tempy-=tempy;
}
}






Re: Why am I getting a dividing by zero error message

2021-01-28 Thread jmh530 via Digitalmars-d-learn
On Thursday, 28 January 2021 at 18:37:37 UTC, Ruby The Roobster 
wrote:

Here is the output/input of the program:
Type in  data for an egg:
Width: 3
Hight: 2

[...]


It might help to separate break this out into smaller functions. 
May make it easier to follow what is happening.


Re: Why am I getting a dividing by zero error message

2021-01-28 Thread Dennis via Digitalmars-d-learn
On Thursday, 28 January 2021 at 18:37:37 UTC, Ruby The Roobster 
wrote:

object.Error@(0): Integer Divide by Zero
Why is this happening? Does anybody know?



data[0] = (new egg(0,0,"a"));


Here you set data[0].y to 0


tempb = data[x].y;


In the first iteration, this equals data[0].y which equals 0


temp = tempa / tempb;


And then you divide by zero here, hence the error






Why am I getting a dividing by zero error message

2021-01-28 Thread Ruby The Roobster via Digitalmars-d-learn

Here is the output/input of the program:
Type in  data for an egg:
Width: 3
Hight: 2

object.Error@(0): Integer Divide by Zero

0x004023FE
0x0040CF9F
0x0040CF19
0x0040CDB4
0x00409033
0x00402638
0x75F86359 in BaseThreadInitThunk
0x77018944 in RtlGetAppContainerNamedObjectPath
0x77018914 in RtlGetAppContainerNamedObjectPath
Chicken:

Here is the source code:

import std.stdio;
import std.string;
void main(){
egg[1000] data;
data[0] = (new egg(0,0,"a"));
for(int i = 1;i<1000;i++)
{
int tempx;
int tempy;
string tempz;
int high = 0;
double highs = 0;
writeln("Type in  data for an egg:");
write("Width: ");
readf(" %d",);
write("Hight: ");
readf(" %d",);
write("Chicken: ");
tempz = readln();
data[i] = new egg(tempx,tempy,tempz);
for(int x = 0;x < i;x++)
{
int tempa;
int tempb;
double temp;
tempa = data[x].x;
if(tempa < 0)
tempa-=tempa;
tempb = data[x].y;
if(tempb < 0)
tempb-=tempb;
temp = tempa / tempb;
if(temp > highs)
{
highs = temp;
high = x;
}
}
tempx = data[high].x - data[i].x;
if(tempx < 0)
tempx-=tempx;
tempy = data[high].y - data[i].y;
if(tempy < 0)
tempy-=tempy;
}
}
class egg {
public:
this(uint x,uint y,string name)
{
this.x = x;
this.y = y;
name = cast(string)name;
}
uint x;
uint y;
char[] name;
}

Why is this happening? Does anybody know?


Re: What are these functions called and how to implement they?

2021-01-28 Thread frame via Digitalmars-d-learn

On Thursday, 28 January 2021 at 17:10:57 UTC, dog2002 wrote:
I saw these functions in some projects. For example: in Dagon 
(https://gecko0307.github.io/dagon/) there are functions like 
onKeyDown. This function doesn't need to call - it checks 
pressed keys every time. Or Update in Unity (game engine). It 
doesn't need to call, but it executes itself every frame.


Okay, maybe it sounds confusing. I just want to understand how 
to implement functions that wait for events and execute code 
inside the function. Something like this:


void onKeyPressed(int key) {
//the code to be executed if a key has been pressed
}


Not 100% sure what you mean but I guess you ask how to implement 
a handler? If an event occurs, a routine decides to call your 
onKeyPressed function and pass the keyCode which was pressed. The 
routine must be registered on a event source that will emit the 
events. The routine is basically just a callback and that is 
indeed called every frame again if an event occurs.


Re: Why filling AA in shared library freezes execution?

2021-01-28 Thread frame via Digitalmars-d-learn

On Thursday, 28 January 2021 at 17:00:35 UTC, Siemargl wrote:

On Thursday, 28 January 2021 at 16:46:40 UTC, frame wrote:

On Thursday, 28 January 2021 at 12:42:09 UTC, Siemargl wrote:
Update. Something is broken in DLL support in druntime for 
Win7.


I take previous working in Win10 binary and try run it in 
virtual Windows 7 SP1 x64.

Got this


You can't expect that a Win10 build also runs on Win7.


Why not?

Only VS2013 runtime is prereq for DMD programs


* you shouln't assume

I just guess that the library loading code is different between 7 
and 10. I get the same error if I try to load a LDC compiled DLL 
in a DMD executable.


Did you try to compile the DLL on Win 7 too?


Re: How do I compose pipes?

2021-01-28 Thread Ali Çehreli via Digitalmars-d-learn

On 1/28/21 2:16 AM, Anthony wrote:

> auto p = pipeProcess("ls");
> auto q = pipeProcess("cat", stdin = p.stdout); //it would be good to do

That would work if `cat` received the *contents* of the files (and with 
a "-" command line switch). Since `ls` produces file names, you would 
have to make the complete `cat` command line from `ls`'s output.


> Do I need to manually extract the output from pipes.stdin.readln

Seems to be so for the `ls | cat` case. But the following `find | grep` 
example shows how two ends of pipes can be connected:


import std.stdio;
import std.process;
import std.range;

// BONUS: Enable one of the following lines to enjoy an issue.
// version = bonus_bug;
// version = bonus_bug_but_this_works;

void main() {
  // Writes to 'a':
  auto a = pipe();
  auto lsPid = spawnProcess([ "find", "."], stdin, a.writeEnd);
  scope (exit) wait(lsPid);

  // Reads from 'a', writes to 'b':
  auto b = pipe();
  auto catPid = spawnProcess([ "grep", "-e", `\.d$` ], a.readEnd, 
b.writeEnd);

  scope (exit) wait(catPid);

  version (bonus_bug) {
// Fails with the following error.
//
// "/usr/include/dmd/phobos/std/typecons.d(6540): Error:
// `"Attempted to access an uninitialized payload."`"
writefln!"Some of the D source files under the current 
directory:\n%-(  %s\n%)"(

  b.readEnd.byLine);

  } else version (bonus_bug_but_this_works) {
// Note .take at the end:
writefln!"Some of the D source files under the current 
directory:\n%-(  %s\n%)"(

  b.readEnd.byLine.take(1000));

  } else {
// The results are read from 'b':
writeln(b.readEnd.byLine);
  }
}

I've discovered a strange issue, which can be observed by uncommenting 
the 'version = bonus_bug;' line above. But comment that one back in and 
uncomment the next line, now it works. (?)


Ali



What are these functions called and how to implement they?

2021-01-28 Thread dog2002 via Digitalmars-d-learn
I saw these functions in some projects. For example: in Dagon 
(https://gecko0307.github.io/dagon/) there are functions like 
onKeyDown. This function doesn't need to call - it checks pressed 
keys every time. Or Update in Unity (game engine). It doesn't 
need to call, but it executes itself every frame.


Okay, maybe it sounds confusing. I just want to understand how to 
implement functions that wait for events and execute code inside 
the function. Something like this:


void onKeyPressed(int key) {
//the code to be executed if a key has been pressed
}




Re: Why filling AA in shared library freezes execution?

2021-01-28 Thread Siemargl via Digitalmars-d-learn

On Thursday, 28 January 2021 at 16:46:40 UTC, frame wrote:

On Thursday, 28 January 2021 at 12:42:09 UTC, Siemargl wrote:
Update. Something is broken in DLL support in druntime for 
Win7.


I take previous working in Win10 binary and try run it in 
virtual Windows 7 SP1 x64.

Got this


You can't expect that a Win10 build also runs on Win7.


Why not?

Only VS2013 runtime is prereq for DMD programs


Re: Why filling AA in shared library freezes execution?

2021-01-28 Thread frame via Digitalmars-d-learn

On Thursday, 28 January 2021 at 12:42:09 UTC, Siemargl wrote:

Update. Something is broken in DLL support in druntime for Win7.

I take previous working in Win10 binary and try run it in 
virtual Windows 7 SP1 x64.

Got this


You can't expect that a Win10 build also runs on Win7.



Re: Weird interaction with public and non-public imports

2021-01-28 Thread SealabJaster via Digitalmars-d-learn

On Thursday, 28 January 2021 at 13:13:46 UTC, Paul Backus wrote:

...


https://issues.dlang.org/show_bug.cgi?id=21589

These issues are always so subtle and specific yet so annoying, 
e.g.:


https://issues.dlang.org/show_bug.cgi?id=21496

and

https://issues.dlang.org/show_bug.cgi?id=21377


Re: Weird interaction with public and non-public imports

2021-01-28 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 28 January 2021 at 13:07:13 UTC, SealabJaster wrote:

Please see: https://run.dlang.io/is/2mwcPH

I'd expect that the `isInstanceOf` would be true instead of 
false here.


Commenting out the public import changes the output of 
`fullyQualifiedName`. I can kind of see why this happens, but 
it's kind of annoying when things like `isInstanceOf` silently 
fail due to this.


Looks like a bug to me. I recommend filing a bug report on 
issues.dlang.org.


Weird interaction with public and non-public imports

2021-01-28 Thread SealabJaster via Digitalmars-d-learn

Please see: https://run.dlang.io/is/2mwcPH

I'd expect that the `isInstanceOf` would be true instead of false 
here.


Commenting out the public import changes the output of 
`fullyQualifiedName`. I can kind of see why this happens, but 
it's kind of annoying when things like `isInstanceOf` silently 
fail due to this.


For context: I found this after getting a compiler error after 
stitching a bunch of D files together, which is why there's a mix 
of a public import and local import in the same file.


Part of me feels like it's a bug since if the alias `T` is using 
one "version" (for lack of a better word) of `Nullable`, then 
surely that same "version" would be given to `isInstanceOf`?


It can get a bit more interesting as well: 
https://run.dlang.io/is/n5jzJs


Re: How to dinamically create Tuples?

2021-01-28 Thread Leonardo via Digitalmars-d-learn

On Wednesday, 27 January 2021 at 17:28:00 UTC, H. S. Teoh wrote:
On Wed, Jan 27, 2021 at 05:17:18PM +, Paul Backus via 
Digitalmars-d-learn wrote:

On Wednesday, 27 January 2021 at 17:11:52 UTC, Leonardo wrote:
> Hi, I want to know if are some way to dinamically create 
> Tuples, with variable size and types defined at runtime. 
> Thanks.


No. D is a statically-typed language, so all types have to be 
defined at compile time.


But you can probably achieve equivalent semantics with an array 
of

Variant (see std.variant).


T


Thanks you all, variant is more like I was expecting.


Re: Why filling AA in shared library freezes execution?

2021-01-28 Thread Siemargl via Digitalmars-d-learn

Update. Something is broken in DLL support in druntime for Win7.

I take previous working in Win10 binary and try run it in virtual 
Windows 7 SP1 x64.

Got this
C:\Proj\dtest>test_dll_exe.exe

object.Exception@test_dll_exe.d(7): Enforcement failed

0x0001400013A3
0x0001400012CC
0x000140001074
0x000140004123
0x000140003FAC
0x00014000408B
0x000140003FAC
0x000140003EF3
0x0001400019D0
0x000140001114
0x00014002F60E
0x771359CD in BaseThreadInitThunk
0x7736A561 in RtlUserThreadStart

Then i modify program, just removing DLL, copying TestFun() in 
main module and it runs.

Same compiler -m64 target.


How do I compose pipes?

2021-01-28 Thread Anthony via Digitalmars-d-learn
This post https://dlang.org/library/std/process/pipe.html 
mentions:


Pipes can, for example, be used for interprocess communication 
by spawning a new process and passing one end of the pipe to the 
child, while the parent uses the other end. (See also 
pipeProcess and pipeShell for an easier way of doing this.)


```
auto p = pipe();
auto outFile = File("D downloads.txt", "w");
auto cpid = spawnProcess(["curl", 
"http://dlang.org/download.html;],

 stdin, p.writeEnd);
scope(exit) wait(cpid);
auto gpid = spawnProcess(["grep", "-o", `http://\S*\.zip`],
 p.readEnd, outFile);
scope(exit) wait(gpid);
```

How am I able to compose the pipes from pipeProcess if I can't 
pass in an existing pipe into the function. Eg.


```
auto p = pipeProcess("ls");
auto q = pipeProcess("cat", stdin = p.stdout); //it would be good 
to do this or something like it

```

Do I need to manually extract the output from pipes.stdin.readln 
and place it in pipes.stdout.writeln?

Or is there an easier way to do this?

Thanks


Re: Why filling AA in shared library freezes execution?

2021-01-28 Thread frame via Digitalmars-d-learn

On Thursday, 28 January 2021 at 07:51:06 UTC, SealabJaster wrote:
On Wednesday, 27 January 2021 at 16:38:07 UTC, Adam D. Ruppe 
wrote:

...


Yikes! Ok, I thought DLLs were just "sort of" unusable due to 
the RTTI issue, but now I'm convinced that they're almost 
completely useless in their current state unless you want to 
live in a world of hurt and pain.


At least at Windows 10, DLLs are "working" except you can't throw 
exceptions and must use some form of exception proxy and wrapper 
to not ending in "illegal instructions". I can also compare 
TypeInfos by using the string representation which works fine.