Dictionary of Templated Functions

2022-09-09 Thread jwatson-CO-edu via Digitalmars-d-learn

Hello,
I'm trying to create a dictionary of templated function pointers. 
 The functions should return `bool` and take a differently-typed 
dynamics arrays `T[]` as an argument.  Based on my understanding 
of the docs, I've made two failed attempts:



**Attempt 1**:
https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/D-templates-tutorial.md
```
template ArrArgs(T){
alias bool function(T[])[string] FuncDict; // compiles
FuncDict lookup; // This compiles but name `lookup` is not 
understood

}

// Compiler does not allow me to declare `FuncDict lookup;` here

void main(){
lookup["foo"] = function bool( string[] args ){ return true; 
}; // undefined identifier `lookup`

}
```


**Attempt 2**:
https://dlang.org/library/std/meta/alias.html
```
alias FuncDict = bool function(T[])[string]; // undefined 
identifier `T`


FuncDict lookup;

void main(){
lookup["foo"] = function bool( string[] args ){ return true; 
};

}
```

I'm unsure of what feature or structure of D will accomplish this.
An example or a link to an example of associated arrays of 
templated function pointers would help me a great deal.


Re: Validate static asserts

2022-09-09 Thread user1234 via Digitalmars-d-learn

On Friday, 9 September 2022 at 17:35:44 UTC, Dennis wrote:
On Friday, 9 September 2022 at 16:41:54 UTC, Andrey Zherikov 
wrote:
What's about new `compileOutput` trait that returns compiler 
output?

```d
static assert(__traits(compileOutput, {  }) == 
"message");

```


As a compiler dev, that sounds terrifying. It would make 
basically every change to dmd a breaking change.


Ah yes, it is so stupid that error message are part of the 
semantics


Re: Validate static asserts

2022-09-09 Thread Ali Çehreli via Digitalmars-d-learn

On 9/9/22 10:35, Dennis wrote:
> On Friday, 9 September 2022 at 16:41:54 UTC, Andrey Zherikov wrote:
>> What's about new `compileOutput` trait that returns compiler output?
>> ```d
>> static assert(__traits(compileOutput, {  }) == "message");
>> ```
>
> As a compiler dev, that sounds terrifying. It would make basically every
> change to dmd a breaking change.

For that very reason, I wrote the function 'assertErrorStringContains()' 
a couple of days ago to ensure *my* strings were in the output:


A precondition:

void test_1(int i)
in (i > 0, fooError("The value must be positive", i, 42))
{
// ...
}

A unit test that ensures it fails and checks string pieces appear in the 
output:


/*
The .msg text of the error contains both the error string and 
the data

that is included in the error.
*/
assertErrorStringContains(() => test_1(-1), [ "The value must be 
positive",

  "-1, 42" ]);
Here is assertErrorStringContains:

// Assert that the expression throws an Error object and that its 
string

// representation contains all expected strings.
void assertErrorStringContains(void delegate() expr, string[] expected)
{
bool thrown = false;

try
{
expr();

}
catch (Error err)
{
thrown = true;

import std.algorithm : any, canFind, splitter;
import std.conv : to;
import std.format : format;

auto lines = err.to!string.splitter('\n');
foreach (exp; expected)
{
assert(lines.any!(line => line.canFind(exp)),
   format!"Failed to find \"%s\" in the output: 
%-(\n  |%s%)"(

   exp, lines));
}
}

assert(thrown);
}

Ali



Re: Validate static asserts

2022-09-09 Thread Dennis via Digitalmars-d-learn
On Friday, 9 September 2022 at 16:41:54 UTC, Andrey Zherikov 
wrote:
What's about new `compileOutput` trait that returns compiler 
output?

```d
static assert(__traits(compileOutput, {  }) == 
"message");

```


As a compiler dev, that sounds terrifying. It would make 
basically every change to dmd a breaking change.


Re: Validate static asserts

2022-09-09 Thread Andrey Zherikov via Digitalmars-d-learn

On Friday, 9 September 2022 at 15:22:30 UTC, Ali Çehreli wrote:
I added and removed '&& false' to every 'static assert' 
condition manually one by one. :/


It's not CI-friendly :(

Perhaps a new compiler switch can compile every 'static assert' 
with an automatic 'false' and dump all their text to the output.


What's about new `compileOutput` trait that returns compiler 
output?

```d
static assert(__traits(compileOutput, {  }) == 
"message");

```


Re: Validate static asserts

2022-09-09 Thread Ali Çehreli via Digitalmars-d-learn

On 9/9/22 07:35, Andrey Zherikov wrote:

> might not compile due to many different reasons

I faced a related situation recently: My error string generation was 
buggy, which taught me that the compiler does not even compile the 
string part of 'static assert' in the 'true' case.


The following program compiles! :)

void main() {
static assert (true, "hello" / WAT);
}

> Is there a way to validate static asserts in unit tests?

I added and removed '&& false' to every 'static assert' condition 
manually one by one. :/


Perhaps a new compiler switch can compile every 'static assert' with an 
automatic 'false' and dump all their text to the output.


Ali



Re: Validate static asserts

2022-09-09 Thread Paul Backus via Digitalmars-d-learn
On Friday, 9 September 2022 at 14:35:33 UTC, Andrey Zherikov 
wrote:
I have bunch of `static assert(, )` in my 
code and would like to validate that specific code triggers 
specific assert by checking what `` is thrown.


It sounds like maybe your goal here is to test that attempting to 
compile a specific piece of code will result in a specific error 
message being *shown to the user*.


Unfortunately, the D compiler does not allow you to introspect on 
error messages, so it is impossible to write a `unittest` that 
covers this requirement. Instead, you will have to write an 
external script or program that attempts to compile a test 
program and checks the output for the expected error message.


Re: Validate static asserts

2022-09-09 Thread Steven Schveighoffer via Digitalmars-d-learn

On 9/9/22 10:35 AM, Andrey Zherikov wrote:
I have bunch of `static assert(, )` in my code and 
would like to validate that specific code triggers specific assert by 
checking what `` is thrown.


Right now I do `static assert(!__traits(compiles, {  }));` but 
since `` might not compile due to many different reasons, I 
might not be testing original `static assert` and might miss breaking 
change.


One way to do this is to extract `` and `` into some 
function and test it outside of `static assert`:

```d
auto check()
{
     return tuple(false, // check result  ('false' is just for example)
  "message");
}

void f()
{
     enum result = check();
     static assert(result.condition, result.message);
}

unittest
{
     enum result = check();
     static assert(result.condition);
     static assert(result.message == "message");
}
```
But I don't like this approach because unit test doesn't really test 
`f()` (it tests duplicated code) so it can't guarantee that `f()` works 
as expected.



Is there a way to validate static asserts in unit tests?


Even this doesn't validate that you get the right message for the 
expected failure.


You can just test the message generation (and use a function for that). 
That's easier than doing some weird tuple thing.


But validating that the correct message comes out of a failed 
compilation can only be done outside compilation.


-Steve


Validate static asserts

2022-09-09 Thread Andrey Zherikov via Digitalmars-d-learn
I have bunch of `static assert(, )` in my 
code and would like to validate that specific code triggers 
specific assert by checking what `` is thrown.


Right now I do `static assert(!__traits(compiles, {  
}));` but since `` might not compile due to many 
different reasons, I might not be testing original `static 
assert` and might miss breaking change.


One way to do this is to extract `` and `` 
into some function and test it outside of `static assert`:

```d
auto check()
{
return tuple(false, // check result  ('false' is just for 
example)

 "message");
}

void f()
{
enum result = check();
static assert(result.condition, result.message);
}

unittest
{
enum result = check();
static assert(result.condition);
static assert(result.message == "message");
}
```
But I don't like this approach because unit test doesn't really 
test `f()` (it tests duplicated code) so it can't guarantee that 
`f()` works as expected.



Is there a way to validate static asserts in unit tests?


Poste some interesting vibe.d webpages

2022-09-09 Thread Alain De Vos via Digitalmars-d-learn
I find documentation of vibe.d between worse and bad, while the 
framework is relative OK.

There are a few good links on the internet.
I post two of them.
Feel free to add other web links in order to increase our 
knowledge.


https://vibed.org/blog/posts/a-scalable-chat-room-service-in-d

https://aberba.com/2016/form-upload-in-vibe-d/



Re: OpenXR library bindings etc

2022-09-09 Thread rikki cattermole via Digitalmars-d-learn
Apart from not linking against OpenXR, I'm not seeing anything obviously 
wrong in that binding.


OpenXR library bindings etc

2022-09-09 Thread brian via Digitalmars-d-learn
I'd like to use D for some visualisation in XR, but without 
OpenXR, I'm stuck before I even start.


I have tried before to write the library bindings 
(https://github.com/infinityplusb/OpenXR-D), but got stuck and 
honestly don't know enough about what needs doing to fix it.
Apparently it is beyond my capability to do alone, regardless of 
how "easy" library bindings apparently are in D.


Is anyone interested in writing/collaborating on writing a 
library binding to OpenXR, before I resort to C++/C#/Python?