Re: Problem with clear on shared associative array?

2024-05-26 Thread mw via Digitalmars-d-learn

On Monday, 27 May 2024 at 00:43:47 UTC, Serg Gini wrote:

On Sunday, 26 May 2024 at 20:15:54 UTC, Andy Valencia wrote:
For others wrestling with this issue, I found out how to cast 
to unshared at this article:




You can check also this solution 
https://github.com/DmitryOlshansky/sharded-map



Pls NOTE: it is
a   `sharded` (meaning trunk-ed) NON-concurrent map,
not `shared` concurrent map.

These two words looks similar, but the meaning is very different.




Re: Parallel safe associative array?

2024-05-24 Thread mw via Digitalmars-d-learn

https://code.dlang.org/packages/rust_interop_d

wrapped:

DashMap: is an implementation of a concurrent associative 
array/hashmap in Rust.


Re: Recommendations on porting Python to D

2024-05-23 Thread mw via Digitalmars-d-learn

On Friday, 3 May 2024 at 17:53:41 UTC, mw wrote:

On Friday, 3 May 2024 at 17:38:10 UTC, Chris Piker wrote:

On Thursday, 25 April 2024 at 16:57:53 UTC, mw wrote:

[...]


Thanks for the suggestions.  I put the question aside for a 
bit, but yesterday ran across a python transpiler here:


  https://github.com/py2many/py2many

It already has support for C++, Go and others.  Since I have 
mountains of python code created over many years, maybe it 
would be worth contributing to this project out of self 
interest.


Can you take a look at py2many and see what you think about 
it?  Getting D on the support list might be good.


(Haven't checked its own implementation and output code 
quality.)


But it says has output for Kotlin, Dart, these two languages 
are similar to D syntactically, so will be a good start.


I took another quick look of the project, it uses the Python 
builtin `ast` module as parser, and visitor design pattern to 
implement the transcompiler, so I think it's of decent quality.


HTH.


Re: Recommendations on porting Python to D

2024-05-03 Thread mw via Digitalmars-d-learn

On Friday, 3 May 2024 at 17:38:10 UTC, Chris Piker wrote:

On Thursday, 25 April 2024 at 16:57:53 UTC, mw wrote:

On Wednesday, 24 April 2024 at 22:07:41 UTC, Chris Piker wrote:


Python-AST to D source converter may already exist?


https://github.com/joortcom/eiffel_rename/tree/main/yi

A rudimentary converter from (extended) Python to D. Maybe you 
can use it as a starting point.


Thanks for the suggestions.  I put the question aside for a 
bit, but yesterday ran across a python transpiler here:


  https://github.com/py2many/py2many

It already has support for C++, Go and others.  Since I have 
mountains of python code created over many years, maybe it 
would be worth contributing to this project out of self 
interest.


Can you take a look at py2many and see what you think about it? 
 Getting D on the support list might be good.


(Haven't checked its own implementation and output code quality.)

But it says has output for Kotlin, Dart, these two languages are 
similar to D syntactically, so will be a good start.




Recommendations for good concurrent hashset (esp. for strings)?

2024-05-01 Thread mw via Digitalmars-d-learn

Hi,

I'm looking for recommendations for good concurrent hashset (esp. 
for strings)?


Any libraries?

Thanks.




Re: Recommendations on porting Python to D

2024-04-25 Thread mw via Digitalmars-d-learn

BTW, maybe you can also try Mojo:

https://github.com/modularml/mojo




Re: Recommendations on porting Python to D

2024-04-25 Thread mw via Digitalmars-d-learn

On Wednesday, 24 April 2024 at 22:07:41 UTC, Chris Piker wrote:


Python-AST to D source converter may already exist?


https://github.com/joortcom/eiffel_rename/tree/main/yi

A rudimentary converter from (extended) Python to D. Maybe you 
can use it as a starting point.


It uses: PEG parser generator for (standard) Python (to extend 
Python syntax):


https://github.com/we-like-parsers/pegen



Another thing you can try (but both the Python-like syntax and 
parser is home-made I think):


dmt is a converter (offline or auto-invoking compiler after 
conversion) from Python-like indention style to curly braces for 
D programming language.


https://github.com/baryluk/dmt

ref:

https://forum.dlang.org/thread/vtftlolshtrtwhlhg...@forum.dlang.org?page=1


Re: Print debug data

2024-04-24 Thread mw via Digitalmars-d-learn

On Monday, 17 July 2023 at 03:43:04 UTC, Alain De Vos wrote:

Is it possible to print runtime memory usage of:
-The stack
-The heap
-The garbage collector ?


And how to print the memory stats of each class / struct type?




Re: dub: Could not resolve configuration for package demo

2024-03-15 Thread mw via Digitalmars-d-learn

On Friday, 15 March 2024 at 18:04:25 UTC, Inkrementator wrote:
You'll have to either fix the old vibe-d version, or fork 
msgpack-rpc to work with current vibe-d, whatever is more 
appropriate and easier.


I'm trying to fix it with the latest vibe-d 0.10.0, now the new 
error:


```
../../src/msgpackrpc/transport/tcp.d(13,8): Error: unable to read 
module `driver`
../../src/msgpackrpc/transport/tcp.d(13,8):Expected 
'vibe/core/driver.d' or 'vibe/core/driver/package.d' in one of 
the following import paths:


```

that line is:

```
import vibe.core.driver;
```

Do you know where is `vibe.core.driver` in the new version?


Re: dub: Could not resolve configuration for package demo

2024-03-15 Thread mw via Digitalmars-d-learn

On Friday, 15 March 2024 at 18:04:25 UTC, Inkrementator wrote:

On Friday, 15 March 2024 at 17:48:26 UTC, mw wrote:

```
$ dub build
Could not resolve configuration for package demo
```


Trying to build your dependency msgpack-rpc, it spits out
```
 Warning The sub configuration directive "vibe-d" -> 
[libevent] references a configuration that does not exist.

Error Could not resolve configuration for package msgpack-rpc
```


Thanks for the reply.

But where you see this warning message? I didn't see it even with 
`$ dub build -v`.




Re: dub: Could not resolve configuration for package demo

2024-03-15 Thread mw via Digitalmars-d-learn

OK, looks something wrong with dub / or vibe-d 0.10.0

https://github.com/msgpack-rpc/msgpack-rpc-d/blob/master/examples/with_http_server/dub.sdl#L5

with
```
dependency "vibe-d" version="~>0.7.25"
```

`dub build` can at least starts.

But

```
dependency "vibe-d" version="~>0.10.0"
```


```
$ dub build
Error Could not resolve configuration for package with_http_server

```



dub: Could not resolve configuration for package demo

2024-03-15 Thread mw via Digitalmars-d-learn

Very simple thing from https://dub.pm/getting-started/first-steps/

After I add dependencies, it cannot build:

```
$ cat dub.json
{
"authors": [
"mw"
],
"copyright": "Copyright © 2024, mw",
"description": "msgpack-rpc-d demo.",
"license": "MIT",
"name": "demo",
"dependencies": {
"msgpack-rpc": "~>0.1.3"
},
"subConfigurations": {
"msgpack-rpc": "default"
}
}

$ dub build
Could not resolve configuration for package demo
```

So what I'm missing?

Thanks.


Re: length's type.

2024-02-07 Thread mw via Digitalmars-d-learn

On Thursday, 8 February 2024 at 05:56:57 UTC, Kevin Bailey wrote:
I don't think it's productive to compare the behavior to C. C 
is now 50 years old. One would hope that D has learned a few 
things in that time.


How many times does the following loop print? I ran into this 
twice doing the AoC exercises. It would be nice if it Just 
Worked.

```
import std.stdio;

int main()
{
  char[] something = ['a', 'b', 'c'];

  for (auto i = -1; i < something.length; ++i)
writeln("less than");

  return 0;
}
```



This is horrible, even if you use `int i`, it still won't work as 
you have thought (ok, I thought):



```
import std.stdio;

int main()
{
  char[] something = ['a', 'b', 'c'];

  for (int i = -1; i < something.length; ++i)
writeln("less than");

  writeln("done");
  return 0;
}

```

it will just output
```
done
```


Re: length's type.

2024-01-28 Thread mw via Digitalmars-d-learn

On Sunday, 28 January 2024 at 16:16:34 UTC, Olivier Pisano wrote:


If .length were to be an int, D could not handle array of more 
than 2G bytes. The whole language would be useless on 64 bit 
systems.



The array.length better to be *signed* `long` (signed size_t) 
instead of unsigned.


Can you guess what is the output of this array element average 
calculation example:


==
import std.algorithm;
import std.stdio;

void main() {
  long[] a = [-5000, 0];
  long   c = sum(a) / a.length;
  writeln(c);
}
==

See the result here:

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


under gdb: received signal SIG34, Real-time event 34.; received signal SIG35, Real-time event 35

2023-11-14 Thread mw via Digitalmars-d-learn

Hi,

I have this in ~/.gdbinit already:

```
handle SIGUSR1 SIGUSR2 nostop
handle SIGUSR1 noprint
handle SIGUSR2 noprint
```

Today I encountered:

received signal SIG34, Real-time event 34.

then I added to ~/.gdbinit

```
handle SIG34 nostop noprint pass noignore
```

Next, I got:

received signal SIG35, Real-time event 35.


I'm wondering why? and how many more such SIG I should put 
into ~/.gdbinit?


BTW, I'm using LDC2 1.34.0

Thanks.




Re: strange link error: _My_struct__xtoHashFNbNeKxSQBlQBoQBiZm _My_struct__xopEqualsMxFKxSQBlQBoQBiZb

2023-10-16 Thread mw via Digitalmars-d-learn
On Tuesday, 17 October 2023 at 01:54:12 UTC, Richard (Rikki) 
Andrew Cattermole wrote:

On 17/10/2023 2:15 PM, mw wrote:
On Tuesday, 17 October 2023 at 01:11:13 UTC, Richard (Rikki) 
Andrew Cattermole wrote:

They are for structs as well.


Ah?! I use quite a few struts, but I never have provided such 
two methods.


Indeed, they are generated by the compiler, not user provided.

They are needed when you have fields that are not basic types 
like other structs.


Is string basic types? as I showed one earlier Foo {one string 
and two ints}, my other struct only has double and long, it also 
has the same link errors about toHash and opEquals.


Re: strange link error: _My_struct__xtoHashFNbNeKxSQBlQBoQBiZm _My_struct__xopEqualsMxFKxSQBlQBoQBiZb

2023-10-16 Thread mw via Digitalmars-d-learn
On Tuesday, 17 October 2023 at 01:14:04 UTC, Richard (Rikki) 
Andrew Cattermole wrote:

On 17/10/2023 1:58 PM, mw wrote:
Oh the <_My_struct> part is my simplification, it is mangled 
as something like : 
_D6..<_My_struct>..__xtoHashFNbNeKxSQBlQBoQBiZm


When dealing with linker errors, please do not simplify, it can 
make problems unsolvable.


It's just my own module and type name, nothing special or 
interesting.


Re: strange link error: _My_struct__xtoHashFNbNeKxSQBlQBoQBiZm _My_struct__xopEqualsMxFKxSQBlQBoQBiZb

2023-10-16 Thread mw via Digitalmars-d-learn
On Tuesday, 17 October 2023 at 01:11:13 UTC, Richard (Rikki) 
Andrew Cattermole wrote:

They are for structs as well.


Ah?! I use quite a few struts, but I never have provided such two 
methods.


Re: strange link error: _My_struct__xtoHashFNbNeKxSQBlQBoQBiZm _My_struct__xopEqualsMxFKxSQBlQBoQBiZb

2023-10-16 Thread mw via Digitalmars-d-learn
On Tuesday, 17 October 2023 at 00:44:17 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
xtoHash and xopEquals are generated by the compiler 
automatically.




These two are for `class`, but shouldn't be generated for 
`struct`, right?


Re: strange link error: _My_struct__xtoHashFNbNeKxSQBlQBoQBiZm _My_struct__xopEqualsMxFKxSQBlQBoQBiZb

2023-10-16 Thread mw via Digitalmars-d-learn
On Tuesday, 17 October 2023 at 00:44:17 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
xtoHash and xopEquals are generated by the compiler 
automatically.


Curiously those two symbol names are not demangling.

Given this, I suspect the best thing to do is file a bug report 
with ldc with the code that generated the linker error.


Oh the <_My_struct> part is my simplification, it is mangled as 
something like : _D6..<_My_struct>..__xtoHashFNbNeKxSQBlQBoQBiZm





Re: strange link error: _My_struct__xtoHashFNbNeKxSQBlQBoQBiZm _My_struct__xopEqualsMxFKxSQBlQBoQBiZb

2023-10-16 Thread mw via Digitalmars-d-learn

On Monday, 16 October 2023 at 21:20:39 UTC, mw wrote:
It's very hard to isolate the problem. I have comment out that 
piece part of code for now (non-essential part of my program): 
comment out where the struct is used, not the struct definition.


Anyway, I will try some time later.


BTW, the struct is generated by

https://github.com/dcarp/protobuf-d/blob/master/examples/dub.json

using the `--d_opt=message-as-struct` option, it looks like this:

```
struct Foo
{
@Proto(1) string a = protoDefaultValue!string;
@Proto(2) int b = protoDefaultValue!int;
@Proto(3) int c = protoDefaultValue!int;
}

```


Re: strange link error: _My_struct__xtoHashFNbNeKxSQBlQBoQBiZm _My_struct__xopEqualsMxFKxSQBlQBoQBiZb

2023-10-16 Thread mw via Digitalmars-d-learn
It's very hard to isolate the problem. I have comment out that 
piece part of code for now (non-essential part of my program): 
comment out where the struct is used, not the struct definition.


Anyway, I will try some time later.


strange link error: _My_struct__xtoHashFNbNeKxSQBlQBoQBiZm _My_struct__xopEqualsMxFKxSQBlQBoQBiZb

2023-10-16 Thread mw via Digitalmars-d-learn

Hi,

I just encountered a strange link error: I have a `struct` type 
`My_struct`, the program compiles fine, but at link time, it 
errors out:


undefined reference to _My_struct__xtoHashFNbNeKxSQBlQBoQBiZm
undefined reference to _My_struct__xopEqualsMxFKxSQBlQBoQBiZb

looks like it treats My_struct type as `class` type?

I'm just wondering how to fix this?


both compilers report the same link error:

DMD64 D Compiler v2.105.0
LDC - the LLVM D compiler (1.35.0):


Thanks.



Re: is the array literal in a loop stack or heap allocated?

2023-10-10 Thread mw via Digitalmars-d-learn

On Wednesday, 11 October 2023 at 03:15:30 UTC, H. S. Teoh wrote:
On Wed, Oct 11, 2023 at 02:54:53AM +, mw via 
Digitalmars-d-learn wrote:

Hi,

I want to confirm: in the following loop, is the array literal 
`a` vs. `b` stack or heap allocated? and how many times?


void main() {

int[2] a;


This is stack-allocated. Once per call to the function.



int[] b;


This is an empty slice. It can refer to either stack or heap 
memory, depending on what's assigned to it.




int i;
While(++i <=100) {

  a = [i, i+1];  // array literal


`a` is overwritten in-place once per loop.


How about the temporary array literal on the right hand side? 
It's stack / heap allocated? Or it's not in the language 
specification, but up to the (optimizing) compiler to decide?





  b = [i, i+1];

[...]

A new array consisting of 2 elements is allocated, once per 
loop, and assigned to b each time. Any arrays from previous 
iterations will be collected by the GC eventually.



T





is the array literal in a loop stack or heap allocated?

2023-10-10 Thread mw via Digitalmars-d-learn

Hi,

I want to confirm: in the following loop, is the array literal 
`a` vs. `b` stack or heap allocated? and how many times?


void main() {

int[2] a;
int[] b;

int i;
While(++i <=100) {

  a = [i, i+1];  // array literal
  b = [i, i+1];

}

}


Thanks.


Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-09 Thread mw via Digitalmars-d-learn

On Monday, 9 October 2023 at 16:51:31 UTC, rempas wrote:

On Monday, 9 October 2023 at 16:42:38 UTC, mw wrote:

use:

import std.conv;

[...]


Damn, sorry, forgot to mention. I cannot use Phobos.


but you `import std.stdio;`?

Or copy the std/conv.d over to your build,

or copy / write a toString(int) function yourself, which is 
compile-time callable.




Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-09 Thread mw via Digitalmars-d-learn

use:

import std.conv;

... i.to!string ...



```
import std.stdio;
import std.conv;

static foreach(i; 0 .. 10) {
  mixin(create_fn!(i.to!string));
}

enum create_fn(string num) = `
  void function_`~ num ~`() { writeln("Hello from function `~ num 
~`!"); }

`;

void main() {
  function_9();
}

```


$ dmd -c gen_fun_i.d
$ nm gen_fun_i.o  | grep function_
 W _D9gen_fun_i10function_0FZv
 W _D9gen_fun_i10function_1FZv
 W _D9gen_fun_i10function_2FZv
 W _D9gen_fun_i10function_3FZv
 W _D9gen_fun_i10function_4FZv
 W _D9gen_fun_i10function_5FZv
 W _D9gen_fun_i10function_6FZv
 W _D9gen_fun_i10function_7FZv
 W _D9gen_fun_i10function_8FZv
 W _D9gen_fun_i10function_9FZv

$ dmd gen_fun_i.d
$ ./gen_fun_i
Hello from function 9!


On Monday, 9 October 2023 at 16:33:32 UTC, rempas wrote:

Let's see the following example:

```d
import std.stdio;

static foreach(i; 0 .. 10) {
  mixin(create_fn!(i.stringof));
}

enum create_fn(string num) = `
  void function_`~ num ~`() { writeln("Hello from function `~ 
num ~`!"); }

`;

void main() {
  function10();
}
```

I'm trying to create a series of function. There will be ten of 
them, and they will be called `function_0`, `function_1`, etc. 
However, in my example, "stringof" returns the character "i" 
itself and turns that into a string instead of getting its 
actual value (number).


Any ideas how I can achieve what I'm trying to achieve?





Re: allocated object address as high as 46th bit (i.e in the 131072 GB range)

2023-10-09 Thread mw via Digitalmars-d-learn
On Monday, 9 October 2023 at 05:57:47 UTC, Richard (Rikki) Andrew 
Cattermole wrote:
As far as I'm aware, no cpu that you can get ahold of support 
more than 48bit of address space at the hardware level.


There is simply no reason at this time to support more, due to 
the fact that nobody has implemented anywhere near that maximum.


Also worth noting, the address a block of memory is, has no 
relation to the hardware. A kernel will instruct the cpu to map 
it wherever it pleases per process.


Thanks for the info. I'm surprised that kernel set virtual space 
that high.


allocated object address as high as 46th bit (i.e in the 131072 GB range)

2023-10-08 Thread mw via Digitalmars-d-learn

https://dlang.org/library/core/bitop/bsr.html

I'm trying to find out allocated object's address' space:

```
import std.stdio;
import core.bitop;

void main() {
  const size_t ONE_G = 1 << 30;
  char[][128] ptrs;
  foreach (i, ref ptr; ptrs) {
ptr = new char[ONE_G];
if (ptr is null) {
  break;
}
writeln(i, ": ", bsr(cast(size_t)ptr.ptr));
  }
}

```

I tried on a few 64-bit machines (all of them have less than 
128GB memory), and the result are about all the same:


```
$ uname -m
x86_64

$ ./bit_op
0: 46
1: 46
2: 46
3: 46
4: 46
5: 46
6: 46
7: 46
8: 46
9: 46
10: 46
Killed
```

What puzzled me is that the highest set bit of the allocated 
address are all 46! i.e in the in the 2^47 ~= 131072 GB range!


I know this could be an OS thing, but just wonder if anyone can 
give me some explanation?


Thanks.



Re: how to assign multiple variables at once by unpacking array?

2023-10-08 Thread mw via Digitalmars-d-learn

On Sunday, 8 October 2023 at 07:45:56 UTC, Andrea Fontana wrote:

On Sunday, 8 October 2023 at 07:44:04 UTC, Andrea Fontana wrote:


```
int a,b,c;

"1,2,3"
.splitter(',')
.zip(only(, , ))
.each!(x => *x[1] = x[0].to!int);

writeln(a, b, c);
```


or:

```
int a,b,c;

only(, , )
.zip("1,2,3".splitter(','))
.each!(x => *x[0] = x[1].to!int);

writeln(a, b, c);
```


Nice.


Re: is there a way to use sumtype in `switch/case` (with multiple statements)? or how can I get the `tag` and `storage`?

2023-10-07 Thread mw via Digitalmars-d-learn

On Saturday, 7 October 2023 at 19:30:23 UTC, mw wrote:

On Saturday, 7 October 2023 at 19:25:51 UTC, mw wrote:

Or how can I get the `tag` and `storage` myself?

https://github.com/dlang/phobos/blob/a3f22129dd2a134338ca02b79ff0de242d7f016e/std/sumtype.d#L310



If I add this line to the above func `isF`:

```
  writeln(t.tag);
```

it won't compile:

sum_type.d(79): Error: no property `tag` for `t` of type 
`std.sumtype.SumType!(Fahrenheit, Celsius, Kelvin)`


Shouldn't the compiler error message be: t.tag is private? rather 
than "no property `tag` for `t`"?


The tag is stored there, why the programmer cannot inspect it, 
and get the payload?


Re: is there a way to use sumtype in `switch/case` (with multiple statements)? or how can I get the `tag` and `storage`?

2023-10-07 Thread mw via Digitalmars-d-learn

On Saturday, 7 October 2023 at 19:25:51 UTC, mw wrote:

Or how can I get the `tag` and `storage` myself?

https://github.com/dlang/phobos/blob/a3f22129dd2a134338ca02b79ff0de242d7f016e/std/sumtype.d#L310



If I add this line to the above func `isF`:

```
  writeln(t.tag);
```

it won't compile:

sum_type.d(79): Error: no property `tag` for `t` of type 
`std.sumtype.SumType!(Fahrenheit, Celsius, Kelvin)`





is there a way to use sumtype in `switch/case` (with multiple statements)? or how can I get the `tag` and `storage`?

2023-10-07 Thread mw via Digitalmars-d-learn

https://dlang.org/library/std/sumtype.html

seems right now the `match!(...)` template only generate a 
delegate, e.g. suppose the following (silly) code:


```
bool isF(Temperature t) {
  while (true) {
t.match!(
  (Fahrenheit f) {return true;},
  (_) {return false;}  // I want to return from the func isF, 
not the delegate!

);
  }
}

enforce( isF(t1));
```

currently, this code will do infinite loop.


I'm wondering if the following are possible:

```
bool isF(Temperature t) {
  while (true) {
switch (t) {
  case Fahrenheit f: /* do multiple statements of `f` */ 
return true;

  default: return false;
}
  }
}
```

Or how can I get the `tag` and `storage` myself?

https://github.com/dlang/phobos/blob/a3f22129dd2a134338ca02b79ff0de242d7f016e/std/sumtype.d#L310

Thanks.


Re: how to assign multiple variables at once by unpacking array?

2023-10-07 Thread mw via Digitalmars-d-learn
Interesting: in terms of easy of coding, clarity and future 
maintenance, which one is superior?


The one liner in Python, or your "solution" with dozen lines of 
code? BTW, is that a solution at all? Did it achieved what the 
original goal asked in the OP question?


So, who should learn from whom?


On Saturday, 7 October 2023 at 12:01:07 UTC, Salih Dincer wrote:

On Saturday, 7 October 2023 at 07:31:45 UTC, mw wrote:

https://stackoverflow.com/questions/47046850/is-there-any-way-to-assign-multiple-variable-at-once-with-dlang

How to do this Python code in D:

```

s = "1 2 3"
A,B,C = map(int, s.split(" "))
A,B,C

(1, 2, 3)

```

Is there a better way (since 2017)?


My words to those who come from Python:

If you are making money from Python, please stay there, but if 
you want to learn new things and a modern language, "Welcome to 
D"


and please use Tuples :)

```d
import std.typecons, std.stdio;

struct DICT(C, F, S)
{
  S[C] dict;
  C[F] freq;

  void opAssign(Tuple!(C, F, S) chr) {
dict[chr[0]] = chr[2];
freq[chr[1]] = chr[0];
  }

  string toString() const
  {
import std.array : appender;
import std.algorithm : sort;
import std.format : formattedWrite;

auto r = appender!string;
foreach(f; freq.keys.sort!"a>b") {
  auto key = freq[f];
  r.formattedWrite("(%c) %s, %.1f\n",
key, dict[key], f);
}
return r.data;
  }
}

void main()
{
  alias index = char;
  alias rank = float;
  alias name = string;

  alias Dict = DICT!(index, rank, name);
  alias chr = Tuple!(index, rank, name);

  auto chrs = [ chr(44, 61.3, "Comma"),
chr(34, 26.7, "Doublequote"),
chr(39, 24.3, "Apostrophe"),
chr(45, 15.3, "Hyphen"),
chr(63,  5.6, "Question"),
chr(58,  3.4, "Colon"),
chr(33,  3.3, "Exclamation"),
chr(59,  3.2, "Semicolon")
  ];

  Dict enDict;
  foreach(tup; chrs) //multiple insertion
enDict = tup;

  writeln("Frequency distributions of punctuation marks used in 
English: ");

  enDict = chr(46, 65.3, "Dot"); // single insertion
  enDict.writeln;
}
```

SDB@79





Re: How to get all modules in a package at CT?

2023-10-05 Thread mw via Digitalmars-d-learn

On Thursday, 5 October 2023 at 21:25:54 UTC, cc wrote:


So how about at runtime? I just want the compiler to help to 
list them, instead of doing manually.


At runtime, simply:
```d
foreach (m; ModuleInfo) {
writeln(m.name);
}
```

However, Walter has hinted that he wants to remove ModuleInfo 
at some point.


So ModuleInfo contains all the modules (transitive closure) built 
into the current binary that is running?


Is there document about this ModuleInfo?

I only find Struct object.ModuleInfo

https://dlang.org/library/object/module_info.html

Which seems different.



Re: Getting all struct members and values with introspection avoiding string mixins

2023-10-05 Thread mw via Digitalmars-d-learn

On Thursday, 5 October 2023 at 21:41:38 UTC, cc wrote:


If you have `T info`, T.tupleof[n] will always match up with 
info.tupleof[n].  You can think of `info.tupleof[n]` as being 
rewritten by the compiler in-place as info.whateverFieldThatIs. 
 You might try this version (note the double {{ }} with static 
foreach):

```d
void printStructInfo( T )( T info ) {
static foreach( i, A; info.tupleof ) {{
enum attribName = T.tupleof[i].stringof;
writefln( "%s : %s", attribName, info.tupleof[i] );
}}
}
```

Be advised that T.tupleof and __traits(allMembers, T) return 
two different sets of things.  allMembers will probably get you 
a lot of stuff you don't want and will need to write checks to 
avoid.  Using .tupleof is a perfectly acceptable practice.



Thanks, this version does not include the alias.




Re: How to get all modules in a package at CT?

2023-10-05 Thread mw via Digitalmars-d-learn

On Thursday, 5 October 2023 at 20:07:38 UTC, user1234 wrote:

No. Sorry.

Generally compile time code cannot interact with the system. To 
be evaluable at compile time code has to be strongly pure, that 
is not the case of the function you would need.


Otherwise you'd need a new traits for that... but that traits 
would violate the rule explained before.


If you want to iterate the package for modules imported in 
it, I'm not sure. __traits(allMembers, package) will list 
names of imported packages but not which modules.


static reflection on import decls is broken, that wont work well


So how about at runtime? I just want the compiler to help to list 
them, instead of doing manually.




Re: How to get all modules in a package at CT?

2023-10-05 Thread mw via Digitalmars-d-learn

On Saturday, 24 November 2018 at 15:21:57 UTC, Anonymouse wrote:

On Saturday, 24 November 2018 at 08:44:19 UTC, Domain wrote:
I have a package named command, and many modules inside it, 
such as command.build, command.pack, command.help...
I want to get all these modules at compile time so that I know 
what command is available.


If you just want static if expressions of whether *known* 
modules are available or not, then test if __traits(identifier, 
package.module) compiles.


---

// Two-step workaround for 
https://issues.dlang.org/show_bug.cgi?id=19409
enum hasBuild = __traits(compiles, __traits(identifier, 
command.build));
enum hasPack = __traits(compiles, __traits(identifier, 
command.pack));
enum hasHelp = __traits(compiles, __traits(identifier, 
command.help));


static if (hasBuild) { /* ... */ }
static if (hasPack) { /* ... */ }
static if (hasHelp) { /* ... */ }

---

__traits(compiles, { import package.module; }) mostly works, 
but I ran into problems when the module was available and 
merely did not compile.


If you want to iterate the package for modules imported in it, 
I'm not sure. __traits(allMembers, package) will list names of 
imported packages but not which modules.



How to list unknown sub-modules? (and not walking the source tree 
dir). Is there a compile time solution to this problem?


Thanks.




Re: Getting all struct members and values with introspection avoiding string mixins

2023-09-28 Thread mw via Digitalmars-d-learn

On Sunday, 1 May 2016 at 10:13:47 UTC, H. S. Teoh wrote:


Using typeof(T.tupleof) seems a bit circuitous. Here's how I'd 
do it:


void printStructInfo( T )( T info ) {
import std.stdio : writefln;
foreach (memb; __traits(allMembers, T)) {
writefln("%s: %s", memb,
__traits(getMember, info, memb));
}
}

(For structs that have members other than data fields, you'll 
need a static if to filter out non-value members, but this 
should get you started.)


How to check `isAlias`? e.g.

```
struct foo {
  int bar;
  alias bar baz;
}
```

I want to filter out baz (right now, it's included).



Re: array index out of bound may not throw exception?

2023-07-21 Thread mw via Digitalmars-d-learn

On Friday, 21 July 2023 at 23:32:41 UTC, Adam D Ruppe wrote:

On Friday, 21 July 2023 at 21:27:45 UTC, mw wrote:
However, I just debugged a case, where out of bound array 
index didn't throw exception, and just hang the thread


Uncaught exceptions in a thread terminate that thread and are 
reported when you call the `join` method of the thread.




Is there a way to let it report on the spot when it happens?




array index out of bound may not throw exception?

2023-07-21 Thread mw via Digitalmars-d-learn

Hi,

I have thought array index out of bound always throw exceptions.

However, I just debugged a case, where out of bound array index 
didn't throw exception, and just hang the thread, which is much 
harder to debug (than exception which tells the exact error and 
source line location).


So my question: array index out of bound may not throw exception 
in D?


I tried DMD and LDC, both have this problem.

Is there any flag I can pass to the compiler to let it always 
throw exception?


Thanks.


Re: looking for recommendation: which thread safe logger library?

2023-07-12 Thread mw via Digitalmars-d-learn

On Wednesday, 12 July 2023 at 09:47:26 UTC, Danilo wrote:

On Wednesday, 12 July 2023 at 05:27:27 UTC, mw wrote:

But what's wrong with my code? the strange compiler error?


Might be a bug/issue in the logger module.

`sharedLog` uses the `shared` attribute,
but the base class for everything ("abstract class Logger")
does not use the `shared` attribute anywhere.

- 
https://github.com/dlang/phobos/blob/master/std/logger/core.d#L516C22-L516C22


It works when you cast away the `shared` attribute from 
`sharedLog`,


cast away? the variable itself is called `sharedLog`, the cast 
completely defeat the purpose.


Sigh, D is so broken on such basic stuff.


Re: looking for recommendation: which thread safe logger library?

2023-07-11 Thread mw via Digitalmars-d-learn

On Wednesday, 12 July 2023 at 04:48:23 UTC, Danilo wrote:

On Wednesday, 12 July 2023 at 01:55:00 UTC, mw wrote:

```
import std.experimental.logger;

void main() {
std.experimental.logger.sharedLog.trace("msg");
}
```


See examples at https://dlang.org/phobos/std_logger.html
and https://dlang.org/phobos/std_logger_filelogger.html



But what's wrong with my code? the strange compiler error?


Re: looking for recommendation: which thread safe logger library?

2023-07-11 Thread mw via Digitalmars-d-learn

On Wednesday, 12 July 2023 at 01:26:25 UTC, mw wrote:

On Wednesday, 12 July 2023 at 01:24:41 UTC, mw wrote:

Hi,

Just wondering which logger library is thread safe?

https://code.dlang.org/

Need to find a mature one to be used in a multi-threaded env.



Oh, forget to mention: need output to logger file.


tried:

https://run.dlang.io/is/ufy4yF

```
import std.experimental.logger;

void main() {
std.experimental.logger.sharedLog.trace("msg");
}
```

but:

onlineapp.d(4): Error: none of the overloads of template 
`std.logger.core.Logger.memLogFunctions!LogLevel.trace.logImpl` 
are callable using argument types `!()(string) shared`
/dlang/dmd/linux/bin64/../../src/phobos/std/logger/core.d(734):   
 Candidates are: `logImpl(int line = __LINE__, string file = 
__FILE__, string funcName = __FUNCTION__, string prettyFuncName = 
__PRETTY_FUNCTION__, string moduleName = __MODULE__, A...)(lazy A 
args)`
/dlang/dmd/linux/bin64/../../src/phobos/std/logger/core.d(780):   
 `logImpl(int line = __LINE__, string file = 
__FILE__, string funcName = __FUNCTION__, string prettyFuncName = 
__PRETTY_FUNCTION__, string moduleName = __MODULE__, A...)(lazy 
bool condition, lazy A args)`



so, how to fix this compiler error?


Re: looking for recommendation: which thread safe logger library?

2023-07-11 Thread mw via Digitalmars-d-learn

On Wednesday, 12 July 2023 at 01:24:41 UTC, mw wrote:

Hi,

Just wondering which logger library is thread safe?

https://code.dlang.org/

Need to find a mature one to be used in a multi-threaded env.



Oh, forget to mention: need output to logger file.


looking for recommendation: which thread safe logger library?

2023-07-11 Thread mw via Digitalmars-d-learn

Hi,

Just wondering which logger library is thread safe?

https://code.dlang.org/

Need to find a mature one to be used in a multi-threaded env.

Thanks.


Re: gdc 12.1: undefined references when linking separately compiled files

2023-07-06 Thread mw via Digitalmars-d-learn

On Thursday, 6 July 2023 at 22:44:27 UTC, Alexibu wrote:



I just encountered this problem in recently released debian 
bookworm (gdc 12.2.0), I was able to fix these undefined 
lambdas inside std library with -fall-instantiations, and a 
bunch of other undefined lambdas in my own code by changing 
template arguments of the form

alias e = (a => a)
to a separate definition
auto (T)default_e(T a)
{
   return a;
}
and
alias e = default_e



Using GDC may require rewrite?

I have thought GDC use the same front-end as DMD & LDC?



Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread mw via Digitalmars-d-learn

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

Try HashMap see if it is still a problem.

If no, then it's another example of the built in AA problem.


is ref inout redundant in: ref inout(T) opIndex(size_t index)

2023-06-19 Thread mw via Digitalmars-d-learn

Hi, I just saw this line:

https://github.com/dlang/dmd/blob/master/druntime/src/core/stdcpp/vector.d#LL66C5-L66C39
```
66:ref inout(T) opIndex(size_t index) inout pure nothrow 
@safe @nogc   { return as_array[index]; }

```

I'm wondering if the `ref` and `inout` redundant here? They both 
mean the same thing? in C++ terms both return the reference of 
the i-th element? so only one of them should be enough?


If not, can someone help to explain the difference? the following 
4 return types:


1) `ref T` alone
2) `inout T` alone
3) `ref inout(T)`
4) `inout ref(T)`


BTW, what does the second `inout` before `pure` do? it's also 
redundant?


Thanks.



Re: Using C++ Classes From D: dmd cannot link, while ldc segfault

2023-06-19 Thread mw via Digitalmars-d-learn

If I use array:

```
extern(C++) {

void getInts(core.stdcpp.array.array!(int, 10) vec) {
  foreach (int i; 0 .. 10) {
vec.at(i) = i;
  }
}

}
```

```
#include 
using namespace std;

void getInts(array* vector);
```

Both DMD and LDC has link error:

base.cpp:42: undefined reference to `getInts(std::array10ul>*)'


Re: Using C++ Classes From D: dmd cannot link, while ldc segfault

2023-06-19 Thread mw via Digitalmars-d-learn
On Monday, 19 June 2023 at 05:56:54 UTC, Richard (Rikki) Andrew 
Cattermole wrote:

On 19/06/2023 5:54 PM, mw wrote:
Ha, I saw vector.d there, So I can use this vector.d as the D 
side of C++'s std::vector?


Probably, I just don't know how well tested it is.

But worth a go!


```
import core.stdcpp.vector;

extern(C++) {

void getInts(core.stdcpp.vector.vector!(int) vec) {
  foreach (int i; 0 .. 10) {
vec.push_back(i);
  }
}

}
```

dmd v2.104.0 failed:

```
/usr/include/dmd/druntime/import/core/stdcpp/vector.d(58): Error: 
undefined identifier `size`, did you mean alias `size_t`?
/usr/include/dmd/druntime/import/core/stdcpp/vector.d(33): Error: 
template instance `core.stdcpp.vector.vector!(int, 
allocator!int)` error instantiating

main.d(32):instantiated from here: `vector!int`
```

LDC - the LLVM D compiler (1.32.2):
```
main.d(32): Error: undefined identifier `vector` in module 
`core.stdcpp.vector`, did you mean enum member `MIctor`?

```

So what's wrong the LDC? how do I write 
`core.stdcpp.vector.vector` then?


Re: Using C++ Classes From D: dmd cannot link, while ldc segfault

2023-06-18 Thread mw via Digitalmars-d-learn
On Monday, 19 June 2023 at 05:46:13 UTC, Richard (Rikki) Andrew 
Cattermole wrote:


On 19/06/2023 5:39 PM, mw wrote:
Then it will be very tedious, esp. for such library class 
std::list.


Yes, you would also need to verify it with every compiler you 
need (MSVC, vs linux gcc).


There could be a reason why it isn't in 
https://github.com/dlang/dmd/tree/master/druntime/src/core/stdcpp


Ha, I saw vector.d there, So I can use this vector.d as the D 
side of C++'s std::vector?





Re: Using C++ Classes From D: dmd cannot link, while ldc segfault

2023-06-18 Thread mw via Digitalmars-d-learn

On Monday, 19 June 2023 at 05:39:51 UTC, mw wrote:
Then it will be very tedious, esp. for such library class 
std::list.


Is there a tool that can automate this?



A related question: basically I want to pass an array of objects 
from D side to the Cpp side, is there any example showing how to 
do this?


On this page

https://dlang.org/spec/cpp_interface.html#cpp-templates

I did not find any information about passing array (containers) 
between D and Cpp.


Thanks.


Re: Using C++ Classes From D: dmd cannot link, while ldc segfault

2023-06-18 Thread mw via Digitalmars-d-learn
On Monday, 19 June 2023 at 05:32:23 UTC, Richard (Rikki) Andrew 
Cattermole wrote:
This is just a guess, but I think the problem is the vtable is 
incomplete.


Because of this the offsets are wrong. So you wouldn't be 
calling push_back.


So, you mean on the D side, it need to list all the fields and 
methods of the C++ class?


```d
class std_list(T) {
  @disable this();
  void push_back(const ref T value);
}
```

Then it will be very tedious, esp. for such library class 
std::list.


Is there a tool that can automate this?

Thanks.



Using C++ Classes From D: dmd cannot link, while ldc segfault

2023-06-18 Thread mw via Digitalmars-d-learn

Hi,

I'm following this example:

https://dlang.org/spec/cpp_interface.html#using_cpp_classes_from_d

and try to wrap a std::list

base.cpp
```cpp
#include 
#include 

using namespace std;

class Base
{
public:
virtual void print3i(int a, int b, int c) = 0;
};

class Derived : public Base
{
public:
int field;
Derived(int field) : field(field) {}

void print3i(int a, int b, int c)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}

int mul(int factor);
};

template class std_list : public std::list {};
void getInts(std_list* list);

int Derived::mul(int factor)
{
return field * factor;
}

Derived *createInstance(int i)
{
// get list of ints from D side
std_list list;
list.push_back(911);
::getInts();
for (int i : list) {
cout << "i = " << i << endl;
}

return new Derived(i);
}

void deleteInstance(Derived *)
{
delete d;
d = 0;
}
```

main.c
```
extern(C++)
{
abstract class Base
{
void print3i(int a, int b, int c);
}

class Derived : Base
{
int field;
@disable this();
override void print3i(int a, int b, int c);
final int mul(int factor);
}

Derived createInstance(int i);
void deleteInstance(ref Derived d);
}

extern(C++) {

class std_list(T) {
  @disable this();
  void push_back(const ref T value);
}

void getInts(std_list!(int) list) {
  foreach (int i; 0 .. 10) {
list.push_back(i);
  }
}

}

void main()
{
import std.stdio;

auto d1 = createInstance(5);
writeln(d1.field);
writeln(d1.mul(4));

Base b1 = d1;
b1.print3i(1, 2, 3);

deleteInstance(d1);
assert(d1 is null);

auto d2 = createInstance(42);
writeln(d2.field);

deleteInstance(d2);
assert(d2 is null);
}

```

Makefile
```
c2d:
g++ -c -g -ggdb base.cpp
ldmd2 -g main.d base.o -L-lstdc++ && ./main

```

if I use LCD (ldmd2), it segfaults:
```
$ make
g++ -c -g -ggdb base.cpp
ldmd2 -g main.d base.o -L-lstdc++ && ./main
Segmentation fault (core dumped)
```

and gdb shows it fails at: main.d:29 list.push_back(i);

if I use DMD, it cannot link:
```
$ make
g++ -c -g -ggdb base.cpp
dmd -g main.d base.o -L-lstdc++ && ./main
/usr/bin/ld: main.o:(.data._D4main__T8std_listTiZQm6__vtblZ+0x0): 
undefined reference to `std_list::push_back(int const&)'

collect2: error: ld returned 1 exit status
Error: linker exited with status 1
```

Looks like something wrong with the push_back().

So, how to fix this DMD link error, and LDC segfaults?

Thanks.




Re: ldc link error on new machine: undefined reference to `_D6object9Throwable7messageMxFNbNfZAxa'

2023-06-14 Thread mw via Digitalmars-d-learn

On Thursday, 15 June 2023 at 01:20:50 UTC, H. S. Teoh wrote:
On Thu, Jun 15, 2023 at 12:49:30AM +, mw via 
Digitalmars-d-learn wrote:

Hi,

Recently encountered a similar problem, ultimately the cause 
was that my library paths turned out to be wrongly set, so it 
was picking up the wrong version of the precompiled libraries.  
Probably you could check whether the library paths in ldc2.conf 
are set correctly, and also double-check whether the libraries 
at those paths are actually the correct ones for your compiler 
version (you may have installed the wrong libraries in the 
right paths).  Mixing up libraries from different LDC releases 
tend to show up as link errors of this kind.


Thanks for the tip, indeed it was caused by `ld` picking up the 
wrong lib (in a different sys path):


```
$ locate libdruntime-ldc
(... my local LDC install)
/usr/lib/x86_64-linux-gnu/libdruntime-ldc-debug-shared.so
/usr/lib/x86_64-linux-gnu/libdruntime-ldc-debug-shared.so.2.0.98
/usr/lib/x86_64-linux-gnu/libdruntime-ldc-debug-shared.so.98
/usr/lib/x86_64-linux-gnu/libdruntime-ldc-debug.a
/usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so
/usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.2.0.98
/usr/lib/x86_64-linux-gnu/libdruntime-ldc-shared.so.98
/usr/lib/x86_64-linux-gnu/libdruntime-ldc.a
```

while I'm using my local installed LDC.  After removing the 
system installation, it worked.





ldc link error on new machine: undefined reference to `_D6object9Throwable7messageMxFNbNfZAxa'

2023-06-14 Thread mw via Digitalmars-d-learn

Hi,

I switched to a different machine to build my project, suddenly I 
got lots of link errors. (It builds fine on the old machine, and 
my software version are the same on both machines LDC - the LLVM 
D compiler (1.32.2))


e.g.:
```
...
/usr/bin/ld: 
/home//.dub/cache/cachetools/0.3.1/build/library-unittest-linux.posix-x86_64-ldc_v1.32.2-71414BF132F687B008642EB263AC23264B21497EA1FEF7D8AFA169EDF7967ADF/libcachetools.a(cachetools.containers.hashmap.o):(.data.rel.ro._D10cachetools10containers7hashmap11KeyNotFound6__vtblZ+0x48): undefined reference to `_D6object9Throwable7messageMxFNbNfZAxa'

```

even manually specify
```
"lflags-ldc": [
"-ldruntime-ldc"
],
```
does not solve the problem, while the symbol is there:

```
$ nm /project/ldc2/lib/libdruntime-ldc.a | grep 
_D6object9Throwable7messageMxFNbNfZAxa

 U _D6object9Throwable7messageMxFNbNfZAxa
 U _D6object9Throwable7messageMxFNbNfZAxa
 U _D6object9Throwable7messageMxFNbNfZAxa
 U _D6object9Throwable7messageMxFNbNfZAxa
 U _D6object9Throwable7messageMxFNbNfZAxa
 T _D6object9Throwable7messageMxFNbNfZAxa
```

Anything else I should check?

Thanks.



Re: looking for hint to debug a strange multi-thread bug (grpc-d-core)

2023-06-13 Thread mw via Digitalmars-d-learn
UPDATE: life is too short to debug dlang built-in AA to right, 
let's just use HashMap from emsi_containers


https://github.com/mw66/grpc-d/blob/master/source/grpc/server/package.d#L25
```
HashMap!(string, ServiceHandlerInterface) services;
```

After this change, the problem goes away.

I think there are some bugs in the builtin AA implementation, as 
demo-ed in this multi-threaded program (maybe also related to 
GC?).




Re: looking for work-around: _d_assocarrayliteralTX segfault assigning a shared associative array an AA literal

2023-06-13 Thread mw via Digitalmars-d-learn
On Tuesday, 13 June 2023 at 22:21:10 UTC, Steven Schveighoffer 
wrote:
As far as I can tell, this problem has been occurring for a 
long time.


BTW, you don't need to define it in global space, just:

```d
void main()
{
   shared aa = ["abc": "123"];
}
```


I have to ask the old-timers on this forums: looks like the dlang 
built-in AA has caused us too many problems, I'm just wondering 
which (dict / map) library on https://code.dlang.org/ provides 
the best (reliable and convenient) drop-in replacement of the 
built-in AA?


Thanks.



looking for hint to debug a strange multi-thread bug (grpc-d-core)

2023-06-13 Thread mw via Digitalmars-d-learn

Hi,

I recently found and started playing with the grpc-d-core[1] 
package, and my program structure looks like this:


https://github.com/mw66/grpc-demo/blob/master/source/main.d

If I run L64 alone (without L66 ~ 79 grpc-d part):
```
64: auto t = new Thread({fun();}).start();
```
it works fine.

If I run L66 ~ 79 grpc-d part alone (without L64), it also works 
fine.


But if I run both, the program received signal SIGSEGV, 
Segmentation fault at here:


https://github.com/mw66/grpc-d/blob/master/source/grpc/server/package.d#L94

with LDC, the print out of `services.keys` shows its corrupted 
with some strange values.


under DMD, it fails at:

https://github.com/mw66/grpc-d/blob/master/source/grpc/server/package.d#L99

while the `services.keys` seems fine.

What puzzles me is that: these two parts of the code are actually 
independent, does not know each other at all (as I just started 
to add the grpc-d skeleton code with dummy returns, and no client 
connected or made any rpc calls).


And the `services` defined here is a private class field:

https://github.com/mw66/grpc-d/blob/master/source/grpc/server/package.d#L21

So how can it be corrupted by `fun()`?


Any suggestions where should I look into?

Thanks.


[1] https://code.dlang.org/packages/grpc-d-core




Re: looking for work-around: _d_assocarrayliteralTX segfault assigning a shared associative array an AA literal

2023-06-13 Thread mw via Digitalmars-d-learn

On Tuesday, 13 June 2023 at 17:12:41 UTC, Anonymouse wrote:

On Tuesday, 13 June 2023 at 17:06:55 UTC, mw wrote:

Does anyone know how to fix it? or any work-around?


Thanks.


I don't know if it's *correct* or not, but I think I did this 
at the time to work around it.


```
shared string[string] aa;

void main()
{
auto aaTemp = [ "abc" : "123" ];
aa = cast(shared)aaTemp;
}
```


Thanks, I just did a simple test:

https://gist.github.com/run-dlang/88cefdde8bb7f61f173ad11b2a03d5ee

(BTW, https://run.dlang.org/ is not stable these few days).

```
import core.thread;
import std;
import std.parallelism;

shared string[string] aa;

void fun() {
  foreach (i; 0 .. 5) {
writeln("fun", i, aa);
foreach (num; parallel(iota(5))) {
// writeln(num);
}
Thread.sleep(5.seconds);
  }
}


void main() {
writeln("start", aa);
auto aaTemp = [ "abc" : "123" ];
aa = cast(shared)aaTemp;
writeln(aa);
foreach (num; parallel(iota(5))) {
  auto t = new Thread({fun();}).start();
}
}
```

run result:
```
start[]
["abc":"123"]
fun0["abc":"123"]
fun0["abc":"123"]
fun0["abc":"123"]
fun0["abc":"123"]
fun0["abc":"123"]
fun1["abc":"123"]
fun1["abc":"123"]
fun1["abc":"123"]
fun1["abc":"123"]
fun1["abc":"123"]
fun2["abc":"123"]
fun2["abc":"123"]
fun2["abc":"123"]
fun2["abc":"123"]
fun2["abc":"123"]
fun3["abc":"123"]
fun3["abc":"123"]
fun3["abc":"123"]
fun3["abc":"123"]
fun3["abc":"123"]
fun4["abc":"123"]
fun4["abc":"123"]
fun4["abc":"123"]
fun4["abc":"123"]
fun4["abc":"123"]
```

looks like different threads can read the same `aa` contents. So 
at least it's working.


Re: how to skip empty field in csvReader?

2023-06-06 Thread mw via Digitalmars-d-learn
On Tuesday, 6 June 2023 at 14:18:25 UTC, Steven Schveighoffer 
wrote:

On 6/6/23 1:09 AM, mw wrote:


Is there a way to tell csvReader to skip such empty fields?


What I have done is specify that it's a string, and then handle 
the conversion myself.



The std library need to be enhanced to skip such empty field 
(very simple change I think), it's a common scenario in real 
world data, which Python can handle easily.



Possibly it can use Nullable, but I'm not sure.

Or, is there another CSV reader library with this 
functionality I can use?


I don't know how much of this is supported in tsv-utils but you 
might give it a look.



https://github.com/eBay/tsv-utils

Do you know if there is any API doc?

Readme only has command line doc.





how to skip empty field in csvReader?

2023-06-05 Thread mw via Digitalmars-d-learn

Hi,

https://run.dlang.io/is/9afmT1

```
void main()
{
import std.csv;
import std.stdio: write, writeln, writef, writefln;
import std.algorithm.comparison : equal;
string text = "Hello;65;;\nWorld;123;7.5";
struct Layout
{
string name;
int value;
double other;
}

auto records = text.csvReader!Layout(';');
assert(records.equal([
Layout("Hello", 65, 2.5),
Layout("World", 123, 7.5),
]));


}
```

There is an empty field in the 1st line: "Hello;65;;", then

std.csv.CSVException@/dlang/dmd/linux/bin64/../../src/phobos/std/csv.d(1232): Floating 
point conversion error for input "".

Is there a way to tell csvReader to skip such empty fields?

Or, is there another CSV reader library with this functionality I 
can use?



Thanks.



As of 2023, we still cannot declaring a constant string[char] AA?

2023-05-16 Thread mw via Digitalmars-d-learn

Hi,

I just run into this problem again:

https://stackoverflow.com/questions/26861708/what-is-the-syntax-for-declaring-a-constant-stringchar-aa

So, the solution still is to use:
```
static this () {
  ...
}
```

What happened to this comments:
"""
It should be noted that this restriction will eventually go away, 
once there is a solid library implementation of associative 
arrays. There is currently work in progress to this effect, which 
seems to be nearing completion. –

Meta  Nov 14, 2014 at 17:41
"""

Still not there yet, after ~10 years?


Re: help: Unresolvable dependencies to package openssl

2023-03-09 Thread mw via Digitalmars-d-learn
On Thursday, 9 March 2023 at 01:22:08 UTC, Steven Schveighoffer 
wrote:


This is a known limitation -- dub builds the selections file 
based on *all* configurations in the file. If you have 
conflicting ones, it will not know what to pick.


However, if you manually construct the selections file, then it 
will work.


Thanks, it worked.



See https://github.com/dlang/dub/issues/1217

-Steve





help: Unresolvable dependencies to package openssl

2023-03-08 Thread mw via Digitalmars-d-learn

Hi,

In my dub.json, I have:

```
"dependencies": {
"apache-thrift": "==0.16.0",
...
}

"subConfigurations": {
"apache-thrift": "use_openssl_1_1",
"pyd": "python39"
},

```

But:

$ dub build
Unresolvable dependencies to package openssl:
  apache-thrift 0.16.0 depends on openssl ~>2.0.3
  apache-thrift 0.16.0 depends on openssl ~>1.1.6
  app ~master depends on openssl ~>2.0.3


Why the subConfigurations: "use_openssl_1_1":

https://github.com/apache/thrift/blob/master/dub.json#L26

{
  "name": "use_openssl_1_1",
  "versions": ["use_openssl_1_1_x"],
  "dependencies": {
"openssl": {
  "version": "~>2.0.3"
}
  }
}

did not pick up the 2.0.3? but report conflict instead?

Thanks.




Re: How to debug/set breakpoint a dynamic library.so ?

2023-02-09 Thread mw via Digitalmars-d-learn

On Thursday, 9 February 2023 at 19:26:59 UTC, Ali Çehreli wrote:

On 2/9/23 06:00, mw wrote:
The dynamic library.so is built from D (with pyd), and invoked 
from Python.


I'm just wondering How to debug/set breakpoint a dynamic 
library.so ?


Can someone give an example?

Thanks.


I may be totally off on this but I think it is as simple as the 
following:


  gdb --args your_python_program

Once inside, set a break point on the dynamic library symbol. 
Then run.


Ali



Thank you, it worked!


All the best wishes for the recent earthquake in Turkey.




How to debug/set breakpoint a dynamic library.so ?

2023-02-09 Thread mw via Digitalmars-d-learn
The dynamic library.so is built from D (with pyd), and invoked 
from Python.


I'm just wondering How to debug/set breakpoint a dynamic 
library.so ?


Can someone give an example?

Thanks.


Re: in dub single file build how to pass "-J" options?

2022-12-21 Thread mw via Digitalmars-d-learn

On Thursday, 22 December 2022 at 02:19:23 UTC, mw wrote:

I have example.d:

```
#!/usr/bin/env dub

/+dub.sdl:
dependency "tkd" version="~>1.1.14"
+/
...
```

$ dub build --single example.d

...
Error: need `-J` switch to import text file `folder_page.png`


I'm wondering how to pass  "-J" options?


Found it, it's:

```
/+dub.sdl:
dependency "tkd" version="~>1.1.14"
dflags "-Jmedia"
+/
```


BTW, for such single file build, do I have to use dub? is there 
another way to just use dmd?


Still don't know how to directly invoke dmd to build.



in dub single file build how to pass "-J" options?

2022-12-21 Thread mw via Digitalmars-d-learn

I have example.d:

```
#!/usr/bin/env dub

/+dub.sdl:
dependency "tkd" version="~>1.1.14"
+/
...
```

$ dub build --single example.d

...
Error: need `-J` switch to import text file `folder_page.png`


I'm wondering how to pass  "-J" options?


BTW, for such single file build, do I have to use dub? is there 
another way to just use dmd?


Thanks.



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 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: 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.


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: difference between x.atomicOp!"+="(1) and atomicFetchAdd(x, 1)?

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

On Thursday, 10 November 2022 at 18:30:16 UTC, Paul Backus wrote:

On Thursday, 10 November 2022 at 17:04:31 UTC, mw wrote:

Hi,

Anyone can help explain what is the difference between 
x.atomicOp!"+="(1) and atomicFetchAdd(x, 1)?


Looking at the source in druntime, `atomicOp!"+="` forwards to 
`atomicFetchAdd` internally, so they should have the same 
behavior.


Thanks! Indeed:

https://github.com/dlang/dmd/blob/master/druntime/src/core/atomic.d#L582

(source is always your best friend :-)

and looks like atomicFetchAdd is more fundamental:

```
return cast(T)(atomicFetchAdd(val, mod) + mod);
```


difference between x.atomicOp!"+="(1) and atomicFetchAdd(x, 1)?

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

Hi,

Anyone can help explain what is the difference between 
x.atomicOp!"+="(1) and atomicFetchAdd(x, 1)?


From the doc, their return values are different

atomicOp
Performs the binary operation 'op' on val using 'mod' as the 
modifier.

Returns:
The result of the operation.


atomicFetchAdd
Atomically adds mod to the value referenced by val and returns 
the value val held previously. This operation is both lock-free 
and atomic.

Returns:
The value held previously by val.


Apart from this, any other difference, esp in a multithreaded 
program? Are they the same? Is atomicOp also lock-free?


Thanks.





Re: druntime thread (from foreach parallel?) cleanup bug

2022-11-01 Thread mw via Digitalmars-d-learn
On Tuesday, 1 November 2022 at 18:18:45 UTC, Steven Schveighoffer 
wrote:




And I just noticed, one of the thread trace points to here:

https://github.com/huntlabs/hunt/blob/master/source/hunt/util/DateTime.d#L430

```
class DateTime {
   shared static this() {
     ...
     dateThread.isDaemon = true;  // not sure if this is 
related

   }
}
```

in the comments, it said: "BUG: ... crashed".  Looks like 
someone run into this (cleanup) issue already, but unable to 
fix it.


Anyway I logged an issue there:

https://github.com/huntlabs/hunt/issues/96




Oh yeah, isDaemon detaches the thread from the GC. Don't do 
that unless you know what you are doing.




Maybe the hunt library author doesn't know. (My code does not 
directly use this library, it got pulled in by some other 
decencies.)



Currently, the `isDaemon` doc does not mention this about this:

https://dlang.org/library/core/thread/threadbase/thread_base.is_daemon.html

Sets the daemon status for this thread. While the runtime will 
wait for all normal threads to complete before tearing down the 
process, daemon threads are effectively ignored and thus will not 
prevent the process from terminating. In effect, daemon threads 
will be terminated automatically by the OS when the process exits.


Maybe we should add to the doc?


BTW, what is exactly going wrong with their code?

I saw the tick() method call inside the anonymous `dateThread` is 
accessing these two stack variables of shared static this():


https://github.com/huntlabs/hunt/blob/master/source/hunt/util/DateTime.d#L409

Appender!(char[])[2] bufs;
const(char)[][2] targets;

Why does this tick() call work after the static this() finished 
in a normal run?


Why the problem only shows up when program finish?




Re: druntime thread (from foreach parallel?) cleanup bug

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

Can you show a code snippet that includes the parallel foreach?


(It's just a very straight forward foreach on an array; as I said 
it may not be relevant.)



And I just noticed, one of the thread trace points to here:

https://github.com/huntlabs/hunt/blob/master/source/hunt/util/DateTime.d#L430

```
class DateTime {
  shared static this() {
...
dateThread.isDaemon = true;  // not sure if this is related
  }
}
```

in the comments, it said: "BUG: ... crashed".  Looks like someone 
run into this (cleanup) issue already, but unable to fix it.


Anyway I logged an issue there:

https://github.com/huntlabs/hunt/issues/96




druntime thread (from foreach parallel?) cleanup bug

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

My program received signal SIGSEGV, Segmentation fault.

Its simplified structure looks like this:

```
void foo() {
  ...
  writeln("done");  // saw this got printed!
}

int main() {
  foo();
  return 0;
}

```

So, just before the program exit, it failed. I suspect druntime 
has a thread (maybe due to foreach parallel) cleanup bug 
somewhere, which is unrelated to my own code. This kind of bug is 
hard to re-produce, not sure if I should file an issue.


I'm using: LDC - the LLVM D compiler (1.30.0) on x86_64.


Under gdb, here is the threads info (for the record):

Thread 11 "xxx" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x153df700 (LWP 36258)]
__GI___res_iclose (free_addr=true, statp=0x153dfdb8) at 
res-close.c:103

103 res-close.c: No such file or directory.


(gdb) info threads
  Id   Target Id Frame
  1Thread 0x15515000 (LWP 36244) "lt" 0x10af1d2d 
in __GI___pthread_timedjoin_ex (threadid=23456246527744, 
thread_return=0x0, abstime=0x0, block=) at 
pthread_join_common.c:89
* 11   Thread 0x153df700 (LWP 36258) "lt" __GI___res_iclose 
(free_addr=true, statp=0x153dfdb8) at res-close.c:103
  17   Thread 0x155544817700 (LWP 36264) "lt" 0x10afac70 
in __GI___nanosleep (requested_time=0x155544810e90, 
remaining=0x155544810ea8) at 
../sysdeps/unix/sysv/linux/nanosleep.c:28



(gdb) thread 1
[Switching to thread 1 (Thread 0x15515000 (LWP 36244))]
#0  0x10af1d2d in __GI___pthread_timedjoin_ex 
(threadid=23456246527744, thread_return=0x0, abstime=0x0, 
block=) at pthread_join_common.c:89

89  pthread_join_common.c: No such file or directory.
(gdb) where
#0  0x10af1d2d in __GI___pthread_timedjoin_ex 
(threadid=23456246527744, thread_return=0x0, abstime=0x0, 
block=) at pthread_join_common.c:89
#1  0x55fb94f8 in 
core.thread.osthread.joinLowLevelThread(ulong) ()
#2  0x55fd7210 in 
_D4core8internal2gc4impl12conservativeQw3Gcx15stopScanThreadsMFNbZv ()
#3  0x55fd3ae7 in 
_D4core8internal2gc4impl12conservativeQw3Gcx4DtorMFZv ()
#4  0x55fd3962 in 
_D4core8internal2gc4impl12conservativeQw14ConservativeGC6__dtorMFZv ()

#5  0x55fc2ce7 in rt_finalize2 ()
#6  0x55fc0056 in rt_term ()
#7  0x55fc0471 in 
_D2rt6dmain212_d_run_main2UAAamPUQgZiZ6runAllMFZv ()

#8  0x55fc0356 in _d_run_main2 ()
#9  0x55fc01ae in _d_run_main ()
#10 0x55840c02 in main (argc=2, argv=0x7fffe188) at 
//home/zhou/project/ldc2-1.30.0-linux-x86_64/bin/../import/core/internal/entrypoint.d:42
#11 0x10163b97 in __libc_start_main (main=0x55840be0 
, argc=2, argv=0x7fffe188, init=, 
fini=, rtld_fini=, 
stack_end=0x7fffe178)

at ../csu/libc-start.c:310
#12 0x556dccca in _start ()


(gdb) thread 11
[Switching to thread 11 (Thread 0x153df700 (LWP 36258))]
#0  __GI___res_iclose (free_addr=true, statp=0x153dfdb8) at 
res-close.c:103

103 res-close.c: No such file or directory.
(gdb) where
#0  __GI___res_iclose (free_addr=true, statp=0x153dfdb8) at 
res-close.c:103

#1  res_thread_freeres () at res-close.c:138
#2  0x102de8c2 in __libc_thread_freeres () at 
thread-freeres.c:29
#3  0x10af0700 in start_thread (arg=0x153df700) at 
pthread_create.c:476
#4  0x10263a3f in clone () at 
../sysdeps/unix/sysv/linux/x86_64/clone.S:95



(gdb) thread 17
[Switching to thread 17 (Thread 0x155544817700 (LWP 36264))]
#0  0x10afac70 in __GI___nanosleep 
(requested_time=0x155544810e90, remaining=0x155544810ea8) at 
../sysdeps/unix/sysv/linux/nanosleep.c:28
28  ../sysdeps/unix/sysv/linux/nanosleep.c: No such file or 
directory.

(gdb) where
#0  0x10afac70 in __GI___nanosleep 
(requested_time=0x155544810e90, remaining=0x155544810ea8) at 
../sysdeps/unix/sysv/linux/nanosleep.c:28
#1  0x55fb8c3b in 
_D4core6thread8osthread6Thread5sleepFNbNiSQBo4time8DurationZv ()
#2  0x55d9a0c2 in 
_D4hunt4util8DateTimeQj25_sharedStaticCtor_L406_C5FZ9__lambda4MFZv () at home/zhou/.dub/packages/hunt-1.7.16/hunt/source/hunt/util/DateTime.d:430

#3  0x55fb89f4 in thread_entryPoint ()
#4  0x10af06db in start_thread (arg=0x155544817700) at 
pthread_create.c:463
#5  0x10263a3f in clone () at 
../sysdeps/unix/sysv/linux/x86_64/clone.S:95





Re: Can someone tell me what the compiler thought I was trying to do?

2022-10-18 Thread mw via Digitalmars-d-learn
@mustuse as a function attribute was in the original version of 
the DIP. It was vetoed by Walter. Thus, only the type attribute 
remains in the accepted version.


Let's continue the discussion here:

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

in general, it's about: command query separation principle


Re: Can someone tell me what the compiler thought I was trying to do?

2022-10-18 Thread mw via Digitalmars-d-learn

On Wednesday, 19 October 2022 at 01:38:27 UTC, Adam D Ruppe wrote:

On Wednesday, 19 October 2022 at 01:34:54 UTC, mw wrote:

Is there any (design) doc about this?


scroll up, click the link from this very thread.

https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1038.md#design-goals-and-possible-alternatives


"""
Rather than make either sacrifice, this DIP proposes a design 
that allows both rigor and simplicity to be maintained, and 
reserves the possibility for a future DIP to allow @mustUse as a 
function attribute.

"""

Another future DIP? I think this DIP is flawed for not using 
"@mustUse as a function attribute"




Re: Can someone tell me what the compiler thought I was trying to do?

2022-10-18 Thread mw via Digitalmars-d-learn

On Wednesday, 19 October 2022 at 01:30:23 UTC, H. S. Teoh wrote:

On Wed, Oct 19, 2022 at 01:15:37AM +, Adam D Ruppe via


it only applies to types, not to functions.


Wat... so what's the use of it then?  So it's not possible to 
mark the return value of an int function @mustUse without 
making, in theory, *all* ints @mustUse?


I must confess I'm baffled as to the purpose of this strange 
design.




Same, can't believe it.

Is there any (design) doc about this?



Re: library to solve the system of linear equations

2022-10-18 Thread mw via Digitalmars-d-learn
On Tuesday, 18 October 2022 at 09:56:09 UTC, Siarhei Siamashka 
wrote:

On Monday, 17 October 2022 at 20:05:24 UTC, mw wrote:

On Monday, 17 October 2022 at 19:54:12 UTC, Yura wrote:

it is possible to install the most recent ldc and gdc 
compilers on Ubuntu 18.04?


Yes, I used LDC on the same system.


What's the recommended way to have up to date D compilers in 
Ubuntu?


I just download the official prebuilt binary from the ldc github 
repo.





Re: library to solve the system of linear equations

2022-10-17 Thread mw via Digitalmars-d-learn
On Monday, 17 October 2022 at 20:39:10 UTC, rikki cattermole 
wrote:

On 18/10/2022 9:37 AM, mw wrote:
Maybe Mir should add static check for supported complier 
versions, rather than let user try and error.


Dub has dependency checks for compiler/dub in it.

It doesn't need to be in code.


Not everyone use dub to build.


Re: library to solve the system of linear equations

2022-10-17 Thread mw via Digitalmars-d-learn

On Monday, 17 October 2022 at 20:22:47 UTC, jmh530 wrote:


If you have a problem with support for mir, submit a bug 
report. I don't think gdc is supported, but ldc should be.


The latest version of Mir can only be compiled with latest Ldc 
1.30, 1.29 doesn't work.


Maybe Mir should add static check for supported complier 
versions, rather than let user try and error.


Re: library to solve the system of linear equations

2022-10-17 Thread mw via Digitalmars-d-learn

On Monday, 17 October 2022 at 19:54:12 UTC, Yura wrote:

it is possible to install the most recent ldc and gdc compilers 
on Ubuntu 18.04?


Yes, I used LDC on the same system.


Re: best memory profiler (to debug accumulated allocation of each line) for D?

2022-10-14 Thread mw via Digitalmars-d-learn

On Friday, 14 October 2022 at 19:06:12 UTC, rassoc wrote:

On 10/14/22 20:45, mw via Digitalmars-d-learn wrote:

Any suggestions?


There's `dmd -profile=gc` or `dub build --build=profile-gc`.


Thanks. I eventually use valgrind --tool=massif, and found the 
problem was in a underlying C library doing lots of small 
allocations.


best memory profiler (to debug accumulated allocation of each line) for D?

2022-10-14 Thread mw via Digitalmars-d-learn

Hi,

With CPU profiler, we can see the accumulated run time of each 
function & line, I'm wondering if there is such a profiler for 
memory allocation?


The reason I'm asking is because I suspect there are some small 
but repetitive memory allocation going on in the libraries (in C 
and D) I'm using which I do not have control (and no source 
available), but I want to find out where.


I want to run on some test data, and get the memory allocation 
stats for each function (better for each line of code), so I can 
find out where the problem is.


Any suggestions?

Thanks.




cannot gdb LDC build binary: Segmentation fault

2022-10-06 Thread mw via Digitalmars-d-learn

Hi,

I have a LDC (1.30.0) built binary on Ubuntu 18.04.5 LTS x86_64, 
the program core dumps somewhere, so I want to debug it. However 
under gdb, the program fails as soon as I start it:


```
(gdb) r
[Thread debugging using libthread_db enabled]
Using host libthread_db library 
"/lib/x86_64-linux-gnu/libthread_db.so.1".

[New Thread 0x155544f17700 (LWP 55978)]
[Thread 0x155544f17700 (LWP 55978) exited]
[New Thread 0x1554b700 (LWP 55979)]
[New Thread 0x15542700 (LWP 55980)]
[New Thread 0x15539700 (LWP 55981)]
[New Thread 0x15530700 (LWP 55982)]
[New Thread 0x153b2700 (LWP 55983)]
[New Thread 0x153a9700 (LWP 55984)]
[New Thread 0x153a0700 (LWP 55985)]
[New Thread 0x15397700 (LWP 55986)]
[New Thread 0x1538e700 (LWP 55987)]
[New Thread 0x15385700 (LWP 55988)]

Thread 11 "lt" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x1538e700 (LWP 55987)]
__GI___get_nprocs () at 
../sysdeps/unix/sysv/linux/getsysstats.c:146
146 ../sysdeps/unix/sysv/linux/getsysstats.c: No such file or 
directory.

(gdb) q
A debugging session is active.

Inferior 1 [process 55973] will be killed.

Quit anyway? (y or n) y
```

Without gdb, the program at least can run for a while, and make 
some progress before it core dumps.


Has anyone experienced such problem before?

any suggestions where I should look at?

Thanks.



Re: csvReader: how to read only selected columns while the class Layout has extra field?

2022-10-03 Thread mw via Digitalmars-d-learn

On Monday, 3 October 2022 at 18:02:51 UTC, Salih Dincer wrote:

On Sunday, 2 October 2022 at 19:48:52 UTC, mw wrote:

```
text.csvReader!Layout(["b","c","a"]);  // Read only 
these column

```

The intention is very clear: only read the selected columns 
from the csv, and for any other fields of class Layout, just 
ignore (with the default D .init value).


Why don't you do this?  For example you can try the following?

```d
import std.csv, std.math.algebraic : abs;

 
string str = "a,b,c\nHello,65,63.63\n➊➋➂❹,123,3673.562";

 struct Layout
 {
 int value;
 double other;
 string name;
 }



You didn't get my question, please add:

```
int extra_field;  // un-comment to see the error
```

to the struct, then you will see the error.




Re: csvReader: how to read only selected columns while the class Layout has extra field?

2022-10-02 Thread mw via Digitalmars-d-learn

On Sunday, 2 October 2022 at 21:03:40 UTC, rassoc wrote:

But say, I'm curious, what's the purpose of adding an 
optional/useless contents field? What's the use-case here?



We have a class/struct for a data record,
some of its data fields need to be saved/loaded from CSV files; 
while there are other helper fields which are useful for various 
computation tasks (e.g. caching some intermediate computation 
results), these fields do not need to be saved/loaded from the 
csv files.



A CSV library should consider all the use cases, and allow users 
to ignore certain fields.


Re: csvReader: how to read only selected columns while the class Layout has extra field?

2022-10-02 Thread mw via Digitalmars-d-learn

```
text.csvReader!Layout(["b","c","a"]);  // Read only these 
column

```

The intention is very clear: only read the selected columns from 
the csv, and for any other fields of class Layout, just ignore 
(with the default D .init value).




csvReader: how to read only selected columns while the class Layout has extra field?

2022-10-02 Thread mw via Digitalmars-d-learn

Hi,

I'm following the example on

https://dlang.org/phobos/std_csv.html

```
class Layout
{
int value;
double other;
string name;
int extra_field;  // un-comment to see the error
}

void main()
{
import std.csv;
import std.stdio: write, writeln, writef, writefln;
import std.algorithm.comparison : equal;
string text = "a,b,c\nHello,65,2.5\nWorld,123,7.5";

auto records =
text.csvReader!Layout(["b","c","a"]);  // Read only these 
column

foreach (r; records) writeln(r.name);
}
```

This works fine so far, but if I un-comment the extra_field line, 
I got runtime error:


```
core.exception.ArrayIndexError@/dlang/dmd/linux/bin64/../../src/phobos/std/csv.d(1209):
 index [3] is out of bounds for array of length 3

??:? _d_arraybounds_indexp [0x5565b4b974d1]
/dlang/dmd/linux/bin64/../../src/phobos/std/csv.d:1209 pure @safe 
void std.csv.CsvReader!(onlineapp.Layout, 1, immutable(char)[], 
dchar, immutable(char)[][]).CsvReader.prime() [0x5565b4b73ed2]
/dlang/dmd/linux/bin64/../../src/phobos/std/csv.d:1154 pure @safe 
void std.csv.CsvReader!(onlineapp.Layout, 1, immutable(char)[], 
dchar, immutable(char)[][]).CsvReader.popFront() [0x5565b4b73c80]
/dlang/dmd/linux/bin64/../../src/phobos/std/csv.d:1069 pure ref 
@safe std.csv.CsvReader!(onlineapp.Layout, 1, immutable(char)[], 
dchar, immutable(char)[][]).CsvReader 
std.csv.CsvReader!(onlineapp.Layout, 1, immutable(char)[], dchar, 
immutable(char)[][]).CsvReader.__ctor(immutable(char)[], 
immutable(char)[][], dchar, dchar, bool) [0x5565b4b73ae8]
/dlang/dmd/linux/bin64/../../src/phobos/std/csv.d:366 pure @safe 
std.csv.CsvReader!(onlineapp.Layout, 1, immutable(char)[], dchar, 
immutable(char)[][]).CsvReader 
std.csv.csvReader!(onlineapp.Layout, 1, immutable(char)[], 
immutable(char)[][], char).csvReader(immutable(char)[], 
immutable(char)[][], char, char, bool) [0x5565b4b735f3]

./onlineapp.d:18 _Dmain [0x5565b4b72ca4]
```

I'm just wondering how to work-around this?

Thanks.


Re: Interfacing with Rust

2022-09-30 Thread mw via Digitalmars-d-learn

extern(C++)?

Why do you think Rust export C++ linkage?

And why do you think Rust export some kind of OO object model 
linkage?



Do it in plain C style, you may make it work.

As said, check how it's done in:

https://code.dlang.org/packages/rust_interop_d


Re: How to do alligned allocation?

2022-09-30 Thread mw via Digitalmars-d-learn

On Friday, 30 September 2022 at 16:23:00 UTC, mw wrote:
On Friday, 30 September 2022 at 15:57:22 UTC, Quirin Schroll 
wrote:
When I do `new void[](n)`, is that buffer allocated with an 
alignment of 1 or what are the guarantees? How can I set an 
alignment? Also, is the alignment of any type guaranteed to be 
a power of 2?


https://dlang.org/library/core/stdc/stdlib/aligned_alloc.html

It's the C func, so check C lib doc.


and then use emplace on the C-alloc-ed memory.


  1   2   3   >