Re: How to work with long paths on Windows?

2022-11-13 Thread Preetpal via Digitalmars-d-learn
In case anyone wants to see a working example of how to use long 
paths on Windows, I uploaded a 
[gist](https://gist.github.com/preetpalS/2fd6c6bf05a94734f89b70b679716bf3) (see my comment in the gist for how to make it work). It is an upgraded version of the original command line tool shown in the example code. I actually use this program to find files and directories sometimes (it matches against relative child paths in the search directory). The command line tool should work on FreeBSD, Linux and macOS.


Re: Sorted Array (Container) Type

2022-11-13 Thread Tejas via Digitalmars-d-learn
On Sunday, 13 November 2022 at 18:51:09 UTC, Siarhei Siamashka 
wrote:
On Saturday, 12 November 2022 at 14:07:46 UTC, Per Nordlöw 
wrote:

Have anybody created a wrapper container

```d
struct Sorted(ArrayLike, alias lessThanPred)
```


that wraps an array-like type `ArrayLike` so that it's always 
sorted according to the binary predicate `lessThanPred`?


I'm not sure if these are a good fit for what you need, but 
have you checked 
https://dlang.org/phobos/std_container_rbtree.html and 
https://dlang.org/phobos/std_container_binaryheap.html ?


He said on Discord he want contiguous data structure, rbtree 
allocates too much


Hipreme's #6 Tip of the day - Knowing when and how to use dub's Have_version

2022-11-13 Thread Hipreme via Digitalmars-d-learn
Although I really don't like many things about how dub does, it 
brings many facilities. Imagine the following project structure:


Project A is using library B

Now, imagine that our `module b` has the following code:

```d
module b;

void print(string s){imported!"std.stdio".writeln(s);}

else version(Have_A)
{
void printA(string s){imported!"std.stdio".writeln("Printing 
from A!");}

}
```

Now, using the project A:

```d
module a;

void main()
{
  import b;
  printA("Printing from my project");
}
```

Now, do try to build it.

It compiles! But then you get a linker error! `Unresolved 
external function printA`.
Now, do you understand why this just happened? If you execute 
`dub --verbose` You will be able to understand what just happened:


Build command for B: `dmd b.d -lib`
Build command for A: `-Imodule/b -version=Have_A b.lib a.d`


So: Your function printA does not get included in the process! As 
when the `b.lib` was built, there wasn't any version for doing 
the implementation, but when A imported B, it basically imports B 
as:


```d
void print(string s);
void printA(string s);
```

So, it won't be actually implementing your function, it just 
knows about the symbol existence, so, why should you ever use 
Have_version?


The following code for B would have worked:
```d
module b;
//Same thin above void print(string s)...

version(Have_A)
{
   public import a.print_a_implementation;
}
```

That way `module a.print_a_implementation` and then, you can 
guarantee that your code will be included (if 
print_a_implementation.d exists in your project)!


So, the key way to think about this is by when thinking about 
using `version(Have_LibraryNameHere)`, you will need to think 
about a 2 compiler passes, one for implementing the functions, 
another for including them. If you remember that, you won't do 
the same mistake as me :)


Re: Proper way to exit with specific exit code?

2022-11-13 Thread Ruby The Roobster via Digitalmars-d-learn

On Sunday, 13 November 2022 at 21:16:32 UTC, mw wrote:
On Saturday, 19 September 2020 at 06:11:15 UTC, Jacob Carlborg 
wrote:

On 2020-09-17 16:58, drathier wrote:

What's the proper way to exit with a specific exit code?

I found a bunch of old threads discussing this, making sure 
destructors run and the runtime terminates properly, all of 
which seemingly concluding that it's sad that there isn't a 
way to do this easily, but hopefully things have changed in 
the last 5-10 years and I'm just missing the obvious solution.


The proper way is:

int main()
{
return 42;
}

I highly recommend against trying to terminate the application 
using `exit` or any other way. Just let the control flow 
return back to the `main` function.


I'm facing this problem to exit early from a multi threaded 
program for mem profiling purpose:


https://forum.dlang.org/thread/zbdevevgghtdgfryu...@forum.dlang.org


So what the simplest and reliable way to terminate all threads 
and exit to os?


I even tried core.stdc.stdlib.exit(-1), it does not work.


Just give the threads a killswitch, pass the killswitch method to 
all of the threads, wait for all the threads to exit, and then 
return.  If this doesn't work, than I don't know what will.


Re: Proper way to exit with specific exit code?

2022-11-13 Thread mw via Digitalmars-d-learn

On Sunday, 13 November 2022 at 22:42:45 UTC, mw wrote:

On Sunday, 13 November 2022 at 22:17:32 UTC, mw wrote:

On Sunday, 13 November 2022 at 22:06:09 UTC, Imperatorn wrote:

On Sunday, 13 November 2022 at 21:37:47 UTC, mw wrote:

On Sunday, 13 November 2022 at 21:16:32 UTC, mw wrote:


I even tried core.stdc.stdlib.exit(-1), it does not work.


Tried
```
import core.runtime;
   Runtime.terminate();
   core.stdc.stdlib.exit(-1);
```

Still does not work.


I have no idea why it would fail. What about assert(0)?



I guess these two calls somehow only terminate the calling 
thread (? this is strange for core.stdc.stdlib.exit), the 
whole program just hangs after the call, and can only be 
terminated by `kill -9`.



I have to manually go thru each of the treads and plug in some 
kind of early exit logic to stop the whole program.



Will try assert(0) later.


tried:

```
core.runtime.Runtime.terminate();
core.stdc.stdlib.exit(-1);
assert(0);
enforce(false);
```

Still not working, not even "Ctrl+C", have to `kill -9` to 
terminate it.


Tried put assert(0) at the top:


```
assert(0);
enforce(false);
core.runtime.Runtime.terminate();
core.stdc.stdlib.exit(-1);
```

Seems cannot even terminate the calling thread, since I got that 
assertion error thousands of times in the log.




Re: Proper way to exit with specific exit code?

2022-11-13 Thread mw via Digitalmars-d-learn

On Sunday, 13 November 2022 at 22:17:32 UTC, mw wrote:

On Sunday, 13 November 2022 at 22:06:09 UTC, Imperatorn wrote:

On Sunday, 13 November 2022 at 21:37:47 UTC, mw wrote:

On Sunday, 13 November 2022 at 21:16:32 UTC, mw wrote:


I even tried core.stdc.stdlib.exit(-1), it does not work.


Tried
```
import core.runtime;
   Runtime.terminate();
   core.stdc.stdlib.exit(-1);
```

Still does not work.


I have no idea why it would fail. What about assert(0)?



I guess these two calls somehow only terminate the calling 
thread (? this is strange for core.stdc.stdlib.exit), the whole 
program just hangs after the call, and can only be terminated 
by `kill -9`.



I have to manually go thru each of the treads and plug in some 
kind of early exit logic to stop the whole program.



Will try assert(0) later.


tried:

```
core.runtime.Runtime.terminate();
core.stdc.stdlib.exit(-1);
assert(0);
enforce(false);
```

Still not working, not even "Ctrl+C", have to `kill -9` to 
terminate it.


Re: Proper way to exit with specific exit code?

2022-11-13 Thread mw via Digitalmars-d-learn

On Sunday, 13 November 2022 at 22:06:09 UTC, Imperatorn wrote:

On Sunday, 13 November 2022 at 21:37:47 UTC, mw wrote:

On Sunday, 13 November 2022 at 21:16:32 UTC, mw wrote:


I even tried core.stdc.stdlib.exit(-1), it does not work.


Tried
```
import core.runtime;
   Runtime.terminate();
   core.stdc.stdlib.exit(-1);
```

Still does not work.


I have no idea why it would fail. What about assert(0)?



I guess these two calls somehow only terminate the calling thread 
(? this is strange for core.stdc.stdlib.exit), the whole program 
just hangs after the call, and can only be terminated by `kill 
-9`.



I have to manually go thru each of the treads and plug in some 
kind of early exit logic to stop the whole program.



Will try assert(0) later.


Re: Proper way to exit with specific exit code?

2022-11-13 Thread Imperatorn via Digitalmars-d-learn

On Sunday, 13 November 2022 at 21:37:47 UTC, mw wrote:

On Sunday, 13 November 2022 at 21:16:32 UTC, mw wrote:


I even tried core.stdc.stdlib.exit(-1), it does not work.


Tried
```
import core.runtime;
   Runtime.terminate();
   core.stdc.stdlib.exit(-1);
```

Still does not work.


I have no idea why it would fail. What about assert(0)?


Re: Proper way to exit with specific exit code?

2022-11-13 Thread mw via Digitalmars-d-learn

On Sunday, 13 November 2022 at 21:16:32 UTC, mw wrote:


I even tried core.stdc.stdlib.exit(-1), it does not work.


Tried
```
import core.runtime;
   Runtime.terminate();
   core.stdc.stdlib.exit(-1);
```

Still does not work.


Re: does dmd --build=profile-gc work with core.stdc.stdlib.exit()?

2022-11-13 Thread mw via Digitalmars-d-learn

On Sunday, 13 November 2022 at 19:02:29 UTC, mw wrote:
BTW, can --build=profile-gc can intercept "Ctrl+C" and 
generate *partial* report file?


And what's the suggested proper way to do


Is there a profile-gc plugin function I can call in the middle of 
my program to generate *partial* report file?


Re: Proper way to exit with specific exit code?

2022-11-13 Thread mw via Digitalmars-d-learn
On Saturday, 19 September 2020 at 06:11:15 UTC, Jacob Carlborg 
wrote:

On 2020-09-17 16:58, drathier wrote:

What's the proper way to exit with a specific exit code?

I found a bunch of old threads discussing this, making sure 
destructors run and the runtime terminates properly, all of 
which seemingly concluding that it's sad that there isn't a 
way to do this easily, but hopefully things have changed in 
the last 5-10 years and I'm just missing the obvious solution.


The proper way is:

int main()
{
return 42;
}

I highly recommend against trying to terminate the application 
using `exit` or any other way. Just let the control flow return 
back to the `main` function.


I'm facing this problem to exit early from a multi threaded 
program for mem profiling purpose:


https://forum.dlang.org/thread/zbdevevgghtdgfryu...@forum.dlang.org


So what the simplest and reliable way to terminate all threads 
and exit to os?


I even tried core.stdc.stdlib.exit(-1), it does not work.






Re: Making sense out of scope and function calls

2022-11-13 Thread 0xEAB via Digitalmars-d-learn

On Sunday, 13 November 2022 at 19:36:48 UTC, Dennis wrote:
Can you please provide a full example? I'm missing the 
definitions of _headers, hstring, values


```d
/++
“HTTP message string” – short-hand for `const(char)[]``.

$(SIDEBAR
Not sure about the name.
Would have prefered *cstring* (*const string* as opposed 
to D’s default immutable one),
but the common association with that term would be 
“zero-terminated string (as made famous by C)”.

)
 +/
alias hstring = const(char)[];

struct Foo {
private Headers _headers;
```

`Headers` was basically like the one here[0], just annoted with 
`scope` `scope return` etc. all over the place. I’m sorry, I 
don’t have any copy of that around anymore (as I’ve never 
commited it and already discarded my code + the idea of using 
`scope` – in its current state – in real world code).



and I suspect there's at least one `@safe` annotation somewhere.


yeah, everything is `@safe` here.


[0] 
https://github.com/oceandrift/http/blob/982030123bdbfb64681264d42ade15d0ccc9ebe9/message/oceandrift/http/message.d#L264




Re: Making sense out of scope and function calls

2022-11-13 Thread Dennis via Digitalmars-d-learn

On Sunday, 13 November 2022 at 19:06:40 UTC, 0xEAB wrote:

Why does only the latter sample compile?
The former leads to the following warning:


Can you please provide a full example? I'm missing the 
definitions of _headers, hstring, values, and I suspect there's 
at least one `@safe` annotation somewhere.


Re: Making sense out of scope and function calls

2022-11-13 Thread Imperatorn via Digitalmars-d-learn

On Sunday, 13 November 2022 at 19:06:40 UTC, 0xEAB wrote:

```d
struct Foo { /* … */
hstring[] getHeader(LowerCaseToken name) scope return
{
return _headers[name].values;
}

[...]


There's an old saying "you can't make sense out of scope"


Making sense out of scope and function calls

2022-11-13 Thread 0xEAB via Digitalmars-d-learn

```d
struct Foo { /* … */
hstring[] getHeader(LowerCaseToken name) scope return
{
return _headers[name].values;
}

hstring[] getHeader(hstring name)() scope return
{
enum token = LowerCaseToken.makeConverted(name);
return this.getHeader(token); // line 605
}
}
```

```d
struct Foo { /* … */
hstring[] getHeader(LowerCaseToken name) scope return
{
return _headers[name].values;
}

hstring[] getHeader(hstring name)() scope return
{
enum token = LowerCaseToken.makeConverted(name);
return _headers[token].values; // line 605
}
}
```

Why does only the latter sample compile?
The former leads to the following warning:

```
beyond.d(605,30): Deprecation: cannot take address of `scope` 
variable `this` since `scope` applies to first indirection only
beyond.d(605,30): Deprecation: cannot take address of `scope` 
variable `this` since `scope` applies to first indirection only

```

(and why is the deprecation message printed twice?)



Re: does dmd --build=profile-gc work with core.stdc.stdlib.exit()?

2022-11-13 Thread mw via Digitalmars-d-learn

On Sunday, 13 November 2022 at 18:51:17 UTC, mw wrote:

On Sunday, 13 November 2022 at 18:48:42 UTC, mw wrote:
BTW, can --build=profile-gc can intercept "Ctrl+C" and 
generate *partial* report file?


And what's the suggested proper way to do early exit, and 
still let --build=profile-gc generate reports?


I tried presss "Ctrl+C", and that cannot stop the program, it 
just hangs there.


I have to `kill -9 ` it to get it stopped.


My build command is:
```
/dmd2/linux/bin64/dub build --build=profile-gc --config=... 
--compiler=dmd

```



Re: does dmd --build=profile-gc work with core.stdc.stdlib.exit()?

2022-11-13 Thread mw via Digitalmars-d-learn

On Sunday, 13 November 2022 at 18:48:42 UTC, mw wrote:
BTW, can --build=profile-gc can intercept "Ctrl+C" and generate 
*partial* report file?


And what's the suggested proper way to do early exit, and still 
let --build=profile-gc generate reports?


I tried presss "Ctrl+C", and that cannot stop the program, it 
just hangs there.


I have to `kill -9 ` it to get it stopped.


Re: Sorted Array (Container) Type

2022-11-13 Thread Siarhei Siamashka via Digitalmars-d-learn

On Saturday, 12 November 2022 at 14:07:46 UTC, Per Nordlöw wrote:

Have anybody created a wrapper container

```d
struct Sorted(ArrayLike, alias lessThanPred)
```


that wraps an array-like type `ArrayLike` so that it's always 
sorted according to the binary predicate `lessThanPred`?


I'm not sure if these are a good fit for what you need, but have 
you checked https://dlang.org/phobos/std_container_rbtree.html 
and https://dlang.org/phobos/std_container_binaryheap.html ?


does dmd --build=profile-gc work with core.stdc.stdlib.exit()?

2022-11-13 Thread mw via Digitalmars-d-learn

Hi,

I'm mem-profiling a multi-threaded program, and want it to exit 
early, so I added a call

```
core.stdc.stdlib.exit(-1);
```

in a loop in one of the thread.

However when the program reached this point, it seems hang: it's 
not exiting, and CPU usage dropped to 0%.


I'm wondering does dmd --build=profile-gc work with 
core.stdc.stdlib.exit()?


And where is the output report file, and the filename? I didn't 
see any report file generated in the current working dir.


BTW, can --build=profile-gc can intercept "Ctrl+C" and generate 
*partial* report file?


And what's the suggested proper way to do early exit, and still 
let --build=profile-gc generate reports?


Thanks!


Re: Comparison of two 'dynamic arrays'.

2022-11-13 Thread matheus. via Digitalmars-d-learn

On Sunday, 13 November 2022 at 17:10:23 UTC, DLearner wrote:

...
The slight generalisation shown at bottom also worked.

However, is there a way of avoiding the for-loop?
...


I don't have too much knowledge in D, but I think so. (My main 
language is C).


Well, one way to make things "better" would be creating a 
constructor inside that struct with opCall, something like this:


import std.stdio;

void main() {
   struct test_struct {
  char[] Txt;
  static test_struct[] opCall(test_struct[] e){
auto _ = e.dup;
foreach(i,v; e[0].Txt){
_[0].Txt = e[0].Txt.dup;
}
return _;
}
   }
   test_struct[] A;
   A.length = 1;
   A[0].Txt.length = 1;
   A[0].Txt[0] = 'X';
   writeln(A);
   auto B = test_struct(A);
   writeln(A, B);
   A[0].Txt[0] = 'Y';
   writeln(A, B);
}

There still a loop over there, so let's wait for some advanced 
users to destroy this. :)


Matheus.


Re: Comparison of two 'dynamic arrays'.

2022-11-13 Thread DLearner via Digitalmars-d-learn

On Sunday, 13 November 2022 at 16:11:17 UTC, matheus. wrote:
[...]


You should add the code below after "auto B = A.dup;":

   B[0].Txt = A[0].Txt.dup;

The "Txt" property inside B is still referencing A without the 
above.


Matheus.


Thank you - your suggestion worked.

The slight generalisation shown at bottom also worked.

However, is there a way of avoiding the for-loop?
Things like
```
B[].Txt = A[].Txt.dup;
```
did not work for me.

```
void main() {

   import std.stdio;

   int index;
   struct test_struct {
  char[] Txt;
   }

   test_struct[] A;

   A.length = 2;
   A[0].Txt.length = 1;
   A[1].Txt.length = 1;

   A[0].Txt[0] = 'W';
   A[1].Txt[0] = 'X';

   writeln(A);

   auto B = A.dup;
   for (index = 0; index < A.length; index = index + 1) {
  B[index].Txt = A[index].Txt.dup;
   }

   writeln(A, B);

   A[0].Txt[0] = 'Y';
   A[1].Txt[0] = 'Z';

   writeln(A, B);
}
```


Re: Comparison of two 'dynamic arrays'.

2022-11-13 Thread Siarhei Siamashka via Digitalmars-d-learn

On Sunday, 13 November 2022 at 15:45:40 UTC, DLearner wrote:

```D
   struct test_struct {
  char[] Txt;
   }
   test_struct[] A;
   auto B = A.dup;
```


But A is not an `int[]` in your new example. You need a "deep 
copy" and I can see that similar questions had been asked in this 
forum before: 
https://forum.dlang.org/thread/yzvcyyqadpttqvtie...@forum.dlang.org


Maybe you can add a copy constructor to your struct, which would 
dup Txt?


Re: Comparison of two 'dynamic arrays'.

2022-11-13 Thread matheus. via Digitalmars-d-learn

On Sunday, 13 November 2022 at 15:45:40 UTC, DLearner wrote:
On Sunday, 13 November 2022 at 14:39:26 UTC, Siarhei Siamashka 
wrote:

On Sunday, 13 November 2022 at 14:28:45 UTC, DLearner wrote:

Creating a step 1.5:
```
int[] B = A;
```


```D
auto B = A.dup;
```

This will create a copy of A rather than referencing to the 
same buffer in memory.


Tested:

```
void main() {

   import std.stdio;


   struct test_struct {
  char[] Txt;
   }

   test_struct[] A;


   A.length = 1;
   A[0].Txt.length = 1;
   A[0].Txt[0] = 'X';

   writeln(A);


   auto B = A.dup;

   writeln(A, B);

   A[0].Txt[0] = 'Y';

   writeln(A, B);
}

```

Got:
```
[test_struct("X")]
[test_struct("X")][test_struct("X")]
[test_struct("Y")][test_struct("Y")]
```

Expected last line to be Y,X not Y,Y.


You should add the code below after "auto B = A.dup;":

   B[0].Txt = A[0].Txt.dup;

The "Txt" property inside B is still referencing A without the 
above.


Matheus.


Re: Comparison of two 'dynamic arrays'.

2022-11-13 Thread DLearner via Digitalmars-d-learn
On Sunday, 13 November 2022 at 14:39:26 UTC, Siarhei Siamashka 
wrote:

On Sunday, 13 November 2022 at 14:28:45 UTC, DLearner wrote:

Creating a step 1.5:
```
int[] B = A;
```


```D
auto B = A.dup;
```

This will create a copy of A rather than referencing to the 
same buffer in memory.


Tested:

```
void main() {

   import std.stdio;


   struct test_struct {
  char[] Txt;
   }

   test_struct[] A;


   A.length = 1;
   A[0].Txt.length = 1;
   A[0].Txt[0] = 'X';

   writeln(A);


   auto B = A.dup;

   writeln(A, B);

   A[0].Txt[0] = 'Y';

   writeln(A, B);
}

```

Got:
```
[test_struct("X")]
[test_struct("X")][test_struct("X")]
[test_struct("Y")][test_struct("Y")]
```

Expected last line to be Y,X not Y,Y.


Re: Comparison of two 'dynamic arrays'.

2022-11-13 Thread Siarhei Siamashka via Digitalmars-d-learn

On Sunday, 13 November 2022 at 14:28:45 UTC, DLearner wrote:

Creating a step 1.5:
```
int[] B = A;
```


```D
auto B = A.dup;
```

This will create a copy of A rather than referencing to the same 
buffer in memory.


Comparison of two 'dynamic arrays'.

2022-11-13 Thread DLearner via Digitalmars-d-learn

Hi

Program has two steps:
1. Creates an array (int[] A), whose size and element values are 
only known at run-time.


Then:
2. The elements (but not the array size) are further processed in 
a way that may or may not alter their value.


Requirement: to identify the indices (if any) of the elements 
whose value changed in step 2.


What is the recommended way to do this?


Creating a step 1.5:
```
int[] B = A;
```
And then step 2.5:
```
for (int j=0, j < A.length, j = j+1) {

   if (B[j] != A[J]) {
  < write the j value away somewhere>
   } else {
   }
}
```

Won't work, as any step 2 changes to A will be reflected in B.


In passing,is 'dynamic array' a good title for a 
structure/concept that seems more of a 'view'?
Surely 'dynamic array' is best used for something that is (no 
more than) a static array whose dimensions can vary at run time?



Best regards


Re: ImportC linking issue

2022-11-13 Thread Andrey Zherikov via Digitalmars-d-learn

On Saturday, 12 November 2022 at 10:02:12 UTC, confuzzled wrote:
Why would I have to search for libcrypto, libminizip, libexpat, 
and more that I haven't even figured out what library they are? 
I thought those dependencies would already be linked into 
libxlsxio_read.a which is a statically linked library.


This is called transitive dependencies.

For Unix systems linking a static library is not a linking per 
se, it means putting object files (.o) into an archive (.a) using 
[ar](https://linux.die.net/man/1/ar). So "libfoo.a" is just a set 
of object files that don't know anything about their dependencies 
and if you use something from libfoo.a, you might need to add 
another library to you link line. Technically there are two 
possible cases: (1) function that you use doesn't depend on 
anything else, then you don't need to add anything to your link 
line; and (2) function uses something from,  say libbar.a - then 
you have to add libbar.a to your link line.


Dynamic libraries (.so) are treated the same way as executables: 
they are created by linker ([ld](https://linux.die.net/man/1/ld) 
for example) and so they have to have all symbols resolved.  This 
means that if libfoo.so depends on libbar.a then the latter is 
already baked in into the former so your don't need to specify it 
when you link with libfoo.so. TBH I don't remember whether you 
should add libbar.so (dynamic library) which is a dependency of 
libfoo.so to your link line.


Re: Using glibc headers with ImportC

2022-11-13 Thread Mike Parker via Digitalmars-d-learn

On Sunday, 13 November 2022 at 09:15:39 UTC, qua wrote:

I agree it was unexpected that it didn't, at least for 
newcomers.


Almost everyone is a newcomer when it comes to ImportC.


Re: Using glibc headers with ImportC

2022-11-13 Thread qua via Digitalmars-d-learn

On Saturday, 12 November 2022 at 14:14:06 UTC, Adam D Ruppe wrote:

On Saturday, 12 November 2022 at 13:46:27 UTC, qua wrote:

This is supposed to work, right?


No, it isn't. And it probably never will.

importC looks for a .c file in the current directory. It is 
that .c file's responsibility to #include whatever .h files you 
want.


It seems that it will be now :)

https://issues.dlang.org/show_bug.cgi?id=23479
https://github.com/dlang/dmd/pull/14636

I agree it was unexpected that it didn't, at least for newcomers.