Re: FreeBSD 13 : wrong kernel version and size of kevent_t

2021-12-20 Thread eugene via Digitalmars-d-learn

On Monday, 20 December 2021 at 21:19:43 UTC, rempas wrote:

I don't have FreeBSD and I can't check the output and header


I have it VirtualBox

files myself but from what you are saying this seems to me as a 
bug. I would recommend you to file an 
[issue](https://issues.dlang.org/) just so the developers 
themself can notice it


Ok.




Re: Thread exits immediately with no reason.

2021-12-20 Thread bauss via Digitalmars-d-learn

On Tuesday, 21 December 2021 at 01:13:10 UTC, Ali Çehreli wrote:


Note: nothrow means "does not throw Exception". Errors can 
still be thrown.


Ali


Which sort of makes sense since they're in theory 
"unrecoverable", but the name is sort of misleading because of 
this minor detail. It should at the very least warn people about 
functions that may throw errors.


Re: How to properly use variadic templates (functions)?

2021-12-20 Thread rempas via Digitalmars-d-learn

On Monday, 20 December 2021 at 22:02:02 UTC, russhy wrote:
Here how i'd do, but i'm not sure how to keep track of the 
index of the arguments, i forgot..


```D
import core.stdc.stdio: putc, stdout;

void print(T...)(string prompt, T args)
{
foreach (a; args)
{
alias A = typeof(a);

static if (is(A : string))
{
for (int j = 0; j < a.length; j++)
{
putc(a[j], stdout);
}
}
else
{
// handle your other types
print("", A.stringof);
}
}

}

void main()
{
print("Prompt (ignored)", "Hello", " world!\n", 123);
}
```


This will not do for me because I want to do formatted output and 
I need the index. But thanks a lot for tying to help!


Re: How to properly use variadic templates (functions)?

2021-12-20 Thread rempas via Digitalmars-d-learn

On Monday, 20 December 2021 at 21:49:59 UTC, Adam D Ruppe wrote:


still use

foreach(arg; args)

and skip that index variable.


I know I can use foreach ("static foreach" more specifically) but 
I need to be able to get the index to choose a specific argument 
because I want to do formatting (that's why I said "printf")


Re: Thread exits immediately with no reason.

2021-12-20 Thread Ali Çehreli via Digitalmars-d-learn

On 12/20/21 3:56 PM, solidstate1991 wrote:

> The problem is, that even with disabling all error-handling that would
> allow to shut down the thread safely, the thread only runs that while
> loop once.

I bet it's throwing an Error. Call your thread entry function from a 
try-catch wrapping function to see whether that's the case:


// Rename existing function:
protected void audioThreadImpl() @nogc nothrow {
  // ...
}

// New function
protected void audioThread() @nogc nothrow {
  try {
audioThreadImpl();

  } catch (Error err) {
stderr.writefln!"ERROR: %s"(err);
  }
}

That should print a call stack. You can catch Throwable instead of Error 
but with nothrow, it's guaranteed that it's not an Exception.


Note: nothrow means "does not throw Exception". Errors can still be thrown.

Ali



Thread exits immediately with no reason.

2021-12-20 Thread solidstate1991 via Digitalmars-d-learn
Here's this function, which serves as a thread entry point: 
https://github.com/ZILtoid1991/iota/blob/main/source/iota/audio/wasapi.d#L220


The problem is, that even with disabling all error-handling that 
would allow to shut down the thread safely, the thread only runs 
that while loop once.


However, if I set my debugger to have a breakpoint right at the 
loop entry it can kinda loop, however the real-time capabilities 
cease to work properly.


I'm using this handy application to debug my library: 
https://github.com/ZILtoid1991/iota/blob/main/testsource/app.d#L54


After the referenced line (where I start my audio thread) I also 
set the main program to wait 10 seconds. At least the audio 
thread just exits for some reason instead of being suspended by 
Thread.wait().


Re: How to properly use variadic templates (functions)?

2021-12-20 Thread russhy via Digitalmars-d-learn
Here how i'd do, but i'm not sure how to keep track of the index 
of the arguments, i forgot..


```D
import core.stdc.stdio: putc, stdout;

void print(T...)(string prompt, T args)
{
foreach (a; args)
{
alias A = typeof(a);

static if (is(A : string))
{
for (int j = 0; j < a.length; j++)
{
putc(a[j], stdout);
}
}
else
{
// handle your other types
print("", A.stringof);
}
}

}

void main()
{
print("Prompt (ignored)", "Hello", " world!\n", 123);
}
```



Re: How to properly use variadic templates (functions)?

2021-12-20 Thread Adam D Ruppe via Digitalmars-d-learn

On Monday, 20 December 2021 at 21:26:45 UTC, rempas wrote:

  // Suppose all 'args' are of type "string" for this example


still use

foreach(arg; args)

and skip that index variable.


How to properly use variadic templates (functions)?

2021-12-20 Thread rempas via Digitalmars-d-learn
I'm trying to implement "printf" and I'm getting an error. The 
smallest possible code to demonstrate the error is:


```
import core.stdc.stdio;

void print(T...)(string prompt, T args) {
  // Suppose all 'args' are of type "string" for this example
  ulong carg = 0;
  for (ulong i = 0; i < args[carg].length; i++) {
putc(args[carg][i], stdout);
  }
}

void main() {
  print("Prompt (ignored)", "Hello", " world!\n");
}
```

The error output I'm getting is:

```
test.d(6): Error: variable `carg` cannot be read at compile time
test.d(7): Error: variable `carg` cannot be read at compile time
test.d(12): Error: template instance `test.print!(string, 
string)` error instantiating

```

There is another error in my original code. Let's see the 
following line:


`u64 str_len = strlen(args[carg]); //u64 = ulong`

Now this should also give me the error that the variable "carg" 
cannot be read at compile time (which it probably does as it 
gives me an error when trying to use "str_len" inside a "for 
loop") but instead it gives me the following error:


```
Error: function `core.stdc.string.strlen(scope const(char*) s)` 
is not callable using argument types `(string)`
   cannot pass argument `_param_1` of type `string` to 
parameter `scope const(char*) s`

```

Any ideas?


Re: FreeBSD 13 : wrong kernel version and size of kevent_t

2021-12-20 Thread rempas via Digitalmars-d-learn

On Sunday, 19 December 2021 at 09:49:29 UTC, eugene wrote:

test program:

```d
import std.stdio;
import core.sys.freebsd.config;
import core.sys.freebsd.sys.event;

void main(string[] args) {
writefln("FreeBSD_version = %s", __FreeBSD_version);
writefln("sizeof(kevent_t) = %s", kevent_t.sizeof);
}
```

output:

@bsd:~/d> ./freebsdver
FreeBSD_version = 1104000
sizeof(kevent_t) = 32

@bsd:~/d> uname -K
1300139

/usr/include/d/dmd/core/sys/freebsd/sys/event.h **do** contain 
correct definition of kevent_t for versions 12+ (with ulong[4] 
ext field), but because of wrong kernel version size of 
kevent_t is also incorrect.


I don't have FreeBSD and I can't check the output and header 
files myself but from what you are saying this seems to me as a 
bug. I would recommend you to file an 
[issue](https://issues.dlang.org/) just so the developers 
themself can notice it. We have nothing to loss in any case


Re: How to insert code in place with templates/mixins?

2021-12-20 Thread rempas via Digitalmars-d-learn

On Monday, 20 December 2021 at 18:58:39 UTC, bachmeier wrote:


You can see the ["String mixins" section 
here](http://ddili.org/ders/d.en/mixin.html) for more details. 
Mixins are generated at compile time, so if you're referring to 
a string mixin inside a runtime loop, the code will not be 
generated every time the loop runs.


Thanks! Yeah after I commented, I thought a little bit about how 
they generate code at compile time like you said and understand 
that what I'm saying doesn't make much sense. Have a nice day my 
friend!


Re: Small structs: interfacing with C++ potentially broken

2021-12-20 Thread Jan via Digitalmars-d-learn

On Monday, 20 December 2021 at 11:58:03 UTC, Tim wrote:

On Monday, 20 December 2021 at 10:24:00 UTC, Jan wrote:
Is this a known issue, or is there a way to instruct DMD to 
use a specific calling convention for a given type?


This looks like a bug. It seems to work without constructor in 
C++, but still crashes with a constructor in D. It also seems 
to work if a trivial destructor is added in D: ~this(){}


This could be related to POD types in C++. Structs with 
constructor or destructor are probably passed differently, but 
dmd only looks at the destructor.


Well, that at least gives me a way to work around this issue.


Re: How to insert code in place with templates/mixins?

2021-12-20 Thread bachmeier via Digitalmars-d-learn

On Monday, 20 December 2021 at 18:23:44 UTC, rempas wrote:
On Monday, 20 December 2021 at 18:12:35 UTC, Stanislav Blinov 
wrote:


https://dlang.org/spec/traits.html#identifier


Thanks!!! Finally I was able to do it! The code is the 
following (well not in my final project but it's a 
demonstration):


```
enum add_char(string c) =
  `if (stdout_index < STDOUT_BUF_LEN) {
stdout_buffer[stdout_index++] =` ~ c ~ `;
continue;
  } else {
sys_write(1, stdout_buffer.ptr, cast(i32)stdout_index);
stdout_index = 0;
stdout_buffer[stdout_index++] =` ~ c ~ `;
continue;
  }`;

void main() {
  mixin(add_char!(__traits(identifier, c)));
}
```

I don't know if there is another way to do it but this works 
for me. Also another thing that I want to ask is if the "mixin" 
is generated every time inside a loop and if there is a better 
way to do that?


You can see the ["String mixins" section 
here](http://ddili.org/ders/d.en/mixin.html) for more details. 
Mixins are generated at compile time, so if you're referring to a 
string mixin inside a runtime loop, the code will not be 
generated every time the loop runs.


Re: How to insert code in place with templates/mixins?

2021-12-20 Thread rempas via Digitalmars-d-learn
On Monday, 20 December 2021 at 18:12:35 UTC, Stanislav Blinov 
wrote:


https://dlang.org/spec/traits.html#identifier


Thanks!!! Finally I was able to do it! The code is the following 
(well not in my final project but it's a demonstration):


```
enum add_char(string c) =
  `if (stdout_index < STDOUT_BUF_LEN) {
stdout_buffer[stdout_index++] =` ~ c ~ `;
continue;
  } else {
sys_write(1, stdout_buffer.ptr, cast(i32)stdout_index);
stdout_index = 0;
stdout_buffer[stdout_index++] =` ~ c ~ `;
continue;
  }`;

void main() {
  mixin(add_char!(__traits(identifier, c)));
}
```

I don't know if there is another way to do it but this works for 
me. Also another thing that I want to ask is if the "mixin" is 
generated every time inside a loop and if there is a better way 
to do that?


Re: How to insert code in place with templates/mixins?

2021-12-20 Thread rempas via Digitalmars-d-learn

On Monday, 20 December 2021 at 18:06:32 UTC, rempas wrote:

On Monday, 20 December 2021 at 11:58:58 UTC, Tejas wrote:


Ehh, it still fails; should've explicitly put the length of 
the array and the `extern (C)` in `main`


```d
module demo;

[ ... ]

extern(C) /+added this because you used -betterC+/ void main() 
{


while (true) {
mixin(add_char!'%');
mixin(add_char!'$');
}
}
```


Thanks! A mixin is not necessary, it will do the same thing 
without it.


Well it seem that it actually needs it...


Re: How to insert code in place with templates/mixins?

2021-12-20 Thread Stanislav Blinov via Digitalmars-d-learn

On Monday, 20 December 2021 at 18:03:09 UTC, rempas wrote:

> Now the problem is that I want it to get the name of so 
> symbol and add it to a string literal.
Let's check this example: enum state(alias name) = `name` ~ ` = 
10;`;


https://dlang.org/spec/traits.html#identifier




Re: How to insert code in place with templates/mixins?

2021-12-20 Thread rempas via Digitalmars-d-learn

On Monday, 20 December 2021 at 11:58:58 UTC, Tejas wrote:


Ehh, it still fails; should've explicitly put the length of the 
array and the `extern (C)` in `main`


```d
module demo;

[ ... ]

extern(C) /+added this because you used -betterC+/ void main() {

while (true) {
mixin(add_char!'%');
mixin(add_char!'$');
}
}
```


Thanks! A mixin is not necessary, it will do the same thing 
without it.


Re: How to insert code in place with templates/mixins?

2021-12-20 Thread rempas via Digitalmars-d-learn

On Monday, 20 December 2021 at 11:30:09 UTC, rumbu wrote:


Enums (that's why the string is declarated as enum) are 
evaluated at compile time, the concatenation op will not end in 
your code as instruction, so you can do anything outside 
betterC rules as long you do it at compile time. You are just 
building some code to use later, the compiler does not generate 
any instruction for it.


In the example above you can press the AST button to see 
exactly how your code is generated.


Wnen you have doubts about a generated string you can always 
test it with ```pragma msg```. In this case, if you write:


```
pragma(msg, add_char!'%');
```

you will have in the output exactly what the compiler will 
generate for your mixin.


That's cool! And I was wondering how I can make sting literal 
concatenation at compile time. Now the problem is that I want it 
to get the name of so symbol and add it to a string literal. 
Let's check this example: enum state(alias name) = `name` ~ ` = 
10;`;


I want this to add the token of that will be used as name in the 
string. For example, I want `state!val;` to get "expanded" as 
`val = 10;` rather than `10 = 10;`. So I don't want it to take 
the value of "val" but the word/token "val" itself. I tried using 
`alias` instead of `char` for the parameter but it didn't worked. 
Do you know how I can do that?


Re: Small structs: interfacing with C++ potentially broken

2021-12-20 Thread Tejas via Digitalmars-d-learn

On Monday, 20 December 2021 at 11:58:03 UTC, Tim wrote:

On Monday, 20 December 2021 at 10:24:00 UTC, Jan wrote:
Is this a known issue, or is there a way to instruct DMD to 
use a specific calling convention for a given type?


This looks like a bug. It seems to work without constructor in 
C++, but still crashes with a constructor in D. It also seems 
to work if a trivial destructor is added in D: ~this(){}


This could be related to POD types in C++. Structs with 
constructor or destructor are probably passed differently, but 
dmd only looks at the destructor.


I think it's got something to do with 
[this](https://forum.dlang.org/post/capevemlbdanirtcj...@forum.dlang.org)


Re: How to insert code in place with templates/mixins?

2021-12-20 Thread Tejas via Digitalmars-d-learn

On Monday, 20 December 2021 at 11:30:09 UTC, rumbu wrote:

On Monday, 20 December 2021 at 10:49:20 UTC, rempas wrote:

On Monday, 20 December 2021 at 09:30:30 UTC, rumbu wrote:

Thanks a lot for the info. When I try to use this code, I'm 
getting the following error:


```
Error: expression expected, not `%`
Error: expression expected, not `%`
```


My fault, I forgot to put some char delimiters. You can find 
tested code here:


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

So I suppose there is a problem with string concatenation. I 
couldn't use it anyway because it is inefficient and because 
I'm using betterC. Do you know any other way that I can 
concatenate strings that does not depend an the Garbage 
Collector or the standard library?


Enums (that's why the string is declarated as enum) are 
evaluated at compile time, the concatenation op will not end in 
your code as instruction, so you can do anything outside 
betterC rules as long you do it at compile time. You are just 
building some code to use later, the compiler does not generate 
any instruction for it.


In the example above you can press the AST button to see 
exactly how your code is generated.


Wnen you have doubts about a generated string you can always 
test it with ```pragma msg```. In this case, if you write:


```
pragma(msg, add_char!'%');
```

you will have in the output exactly what the compiler will 
generate for your mixin.


Ehh, it still fails; should've explicitly put the length of the 
array and the `extern (C)` in `main`


```d
module demo;

//i am just declaring these to have them.
size_t stdout_index;
enum STDOUT_BUF_LEN = 42;
char[STDOUT_BUF_LEN] stdout_buffer; /+indexing an uninitialized 
dynamic array resulted in out of bounds error even for index == 
0+/

alias i32 = int;
void sys_write(int i, void* p, int index) {}
//


enum add_char(char c) =

  `if (stdout_index < STDOUT_BUF_LEN) {
stdout_buffer[stdout_index++] ='` ~ c ~ `';
continue;
  } else {
sys_write(1, stdout_buffer.ptr, cast(i32)stdout_index);
stdout_index = 0;
stdout_buffer[stdout_index++] ='` ~ c ~ `';
continue;
  }`;

extern(C) /+added this because you used -betterC+/ void main()
{

while (true) {
mixin(add_char!'%');
mixin(add_char!'$');
}



}
```


Re: Small structs: interfacing with C++ potentially broken

2021-12-20 Thread Tim via Digitalmars-d-learn

On Monday, 20 December 2021 at 10:24:00 UTC, Jan wrote:
Is this a known issue, or is there a way to instruct DMD to use 
a specific calling convention for a given type?


This looks like a bug. It seems to work without constructor in 
C++, but still crashes with a constructor in D. It also seems to 
work if a trivial destructor is added in D: ~this(){}


This could be related to POD types in C++. Structs with 
constructor or destructor are probably passed differently, but 
dmd only looks at the destructor.


Re: How to insert code in place with templates/mixins?

2021-12-20 Thread rumbu via Digitalmars-d-learn

On Monday, 20 December 2021 at 10:49:20 UTC, rempas wrote:

On Monday, 20 December 2021 at 09:30:30 UTC, rumbu wrote:

Thanks a lot for the info. When I try to use this code, I'm 
getting the following error:


```
Error: expression expected, not `%`
Error: expression expected, not `%`
```


My fault, I forgot to put some char delimiters. You can find 
tested code here:


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

So I suppose there is a problem with string concatenation. I 
couldn't use it anyway because it is inefficient and because 
I'm using betterC. Do you know any other way that I can 
concatenate strings that does not depend an the Garbage 
Collector or the standard library?


Enums (that's why the string is declarated as enum) are evaluated 
at compile time, the concatenation op will not end in your code 
as instruction, so you can do anything outside betterC rules as 
long you do it at compile time. You are just building some code 
to use later, the compiler does not generate any instruction for 
it.


In the example above you can press the AST button to see exactly 
how your code is generated.


Wnen you have doubts about a generated string you can always test 
it with ```pragma msg```. In this case, if you write:


```
pragma(msg, add_char!'%');
```

you will have in the output exactly what the compiler will 
generate for your mixin.







Re: How to insert code in place with templates/mixins?

2021-12-20 Thread rempas via Digitalmars-d-learn

On Monday, 20 December 2021 at 09:30:30 UTC, rumbu wrote:


because you cannot have statements directly in a template (the 
fact that is a mixin template is irelevant), only declarations.


If you want to just insert some random code, use strings. You 
can create a templated enum to store your parametrized string. 
Please note how your parameter (c) becomes part of the 
resulting string through concatenation (~):


```
enum add_char(char c) =

  `if (stdout_index < STDOUT_BUF_LEN) {
stdout_buffer[stdout_index++] =` ~ c ~ `;
continue;
  } else {
sys_write(1, stdout_buffer.ptr, cast(i32)stdout_index);
stdout_index = 0;
stdout_buffer[stdout_index++] =` ~ c ~ `;
continue;
  }`;

```

and when you want the code inserted:

```
mixin(add_char!'%');
```

If you want to be sure that your string is syntactically 
correct, use token strings 
(https://dlang.org/spec/lex.html#token_strings)


Thanks a lot for the info. When I try to use this code, I'm 
getting the following error:


```
Error: expression expected, not `%`
Error: expression expected, not `%`
```

So I suppose there is a problem with string concatenation. I 
couldn't use it anyway because it is inefficient and because I'm 
using betterC. Do you know any other way that I can concatenate 
strings that does not depend an the Garbage Collector or the 
standard library?


Small structs: interfacing with C++ potentially broken

2021-12-20 Thread Jan via Digitalmars-d-learn

I have a small struct that I'm trying to interface to.

C++
```cpp
struct __declspec(dllexport) SmallStruct
{
  float value = 0;
  //float value2 = 0;
  //float value3 = 0;

  SmallStruct(float val)
: value(val)
  {
  }

  static SmallStruct GetValue(float input)
  {
return SmallStruct(input * 3.141f);
  }
};
```

And on the D side:

```cpp
extern(C++) struct SmallStruct
{
  float value = 0;
//   float value2 = 0;
//   float value3 = 0;

  static SmallStruct GetValue(float input);
};
```

Then I use it like this:

```cpp
SmallStruct s = SmallStruct.GetValue(3);
```

Running this crashes on the C++ side, with an access violation 
writing data.
Looking at the disassembly, it seems that C++ expects the 
Smallstruct return object to be passed in, whereas D assumes that 
the SmallStruct is passed through registers.


This is with MVSC 2019 compiled for x64 and DMD v2.098.0.

If I enable 'value2' and 'value3', it seems that both compilers 
start to agree on the calling convention.


Is this a known issue, or is there a way to instruct DMD to use a 
specific calling convention for a given type?




Re: How to insert code in place with templates/mixins?

2021-12-20 Thread rumbu via Digitalmars-d-learn

On Monday, 20 December 2021 at 08:45:50 UTC, rempas wrote:
Here I am having a problem with templates again. No matter how 
much I read, I can't seem to understand how templates/mixins 
work.


So any ideas why this doesn't work?


because you cannot have statements directly in a template (the 
fact that is a mixin template is irelevant), only declarations.


If you want to just insert some random code, use strings. You can 
create a templated enum to store your parametrized string. Please 
note how your parameter (c) becomes part of the resulting string 
through concatenation (~):


```
enum add_char(char c) =

  `if (stdout_index < STDOUT_BUF_LEN) {
stdout_buffer[stdout_index++] =` ~ c ~ `;
continue;
  } else {
sys_write(1, stdout_buffer.ptr, cast(i32)stdout_index);
stdout_index = 0;
stdout_buffer[stdout_index++] =` ~ c ~ `;
continue;
  }`;

```

and when you want the code inserted:

```
mixin(add_char!'%');
```

If you want to be sure that your string is syntactically correct, 
use token strings (https://dlang.org/spec/lex.html#token_strings)





Re: dynamic array + copy ctor

2021-12-20 Thread vit via Digitalmars-d-learn
On Sunday, 19 December 2021 at 23:21:00 UTC, Stanislav Blinov 
wrote:

On Sunday, 19 December 2021 at 22:29:21 UTC, vit wrote:

Hello,
Why is copy ctor in this example not called?


Because D runtime isn't properly married to copy constructors 
yet. I.e. it's a bug, a variant of this one: 
https://issues.dlang.org/show_bug.cgi?id=20879


Thanks,
That bug is 1.5 years old. Are this problems with copy ctors hard 
to fix?

Are copy ctors still experimental functionality?


How to insert code in place with templates/mixins?

2021-12-20 Thread rempas via Digitalmars-d-learn
Here I am having a problem with templates again. No matter how 
much I read, I can't seem to understand how templates/mixins 
work. So I'm having the following code (just a snippet of the 
real code):


```
if (c != '%') {
  if (stdout_index < STDOUT_BUF_LEN) {
stdout_buffer[stdout_index++] = c;
continue;
  } else {
sys_write(1, stdout_buffer.ptr, cast(i32)stdout_index);
stdout_index = 0;
stdout_buffer[stdout_index++] = c;
continue;
  }
}
```

And I want to create a macro (using the C terms) to make the code 
inside the first if statement (`if (c != '%')`) into a template 
that will be able to used and added in place (not as a function 
as I don't want to function call). I tried to make it both a 
template and a mixin template and It will not compile, rather it 
will give my the following error:


```
Error: declaration expected, not `if`
Error: declaration expected, not `continue`
Error: declaration expected, not `else`
Error: basic type expected, not `0`
Error: found `0` when expecting `;`
Error: no identifier for declarator 
`stdout_buffer[stdout_index++]`

Error: declaration expected, not `=`
Error: declaration expected, not `continue`
Error: unrecognized declaration
```

It should be clear what I tried to still I will post what I tried 
in case someone is curious to see:


```
mixin template add_char() {
  if (stdout_index < STDOUT_BUF_LEN) {
stdout_buffer[stdout_index++] = c;
continue;
  } else {
sys_write(1, stdout_buffer.ptr, cast(i32)stdout_index);
stdout_index = 0;
stdout_buffer[stdout_index++] = c;
continue;
  }
}
```

So any ideas why this doesn't work?