Sumtype warning

2021-07-11 Thread JG via Digitalmars-d-learn

I am getting the following message:
Warning: struct SumType has method toHash, however it cannot be 
called with const(SumType!(A,B,C)) this


Could someone point in the right direction to understand what I 
am doing that causes this?




Re: Sumtype warning

2021-07-11 Thread jfondren via Digitalmars-d-learn

On Sunday, 11 July 2021 at 09:20:23 UTC, JG wrote:

I am getting the following message:
Warning: struct SumType has method toHash, however it cannot be 
called with const(SumType!(A,B,C)) this


Could someone point in the right direction to understand what I 
am doing that causes this?


The two restrictions on toHash are

1. if you're compiling with betterC, it's not present. So you 
wouldn't get this message.
2. otherwise, the types all have to pass `allSatisfy!(isHashable, 
Map!(ConstOf, Types))`, where isHashable is defined in 
std.sumtype to `private enum isHashable(T) = __traits(compiles, 
() nothrow @safe { hashOf(T.init); });`


So the problem is one of
1. A, B, or C doesn't have a .toHash
2. A, B, or C has an improper .toHash that isn't nothrow and @safe
3. A, B, or C has a .toHash but it doesn't work for const(A) etc.


Scope of enum

2021-07-11 Thread DLearner via Digitalmars-d-learn

Please see the two code snippets below:
```
// test01.d

enum MemSiz  = 240;

void main() {

   import k_mod;
}
```

and

```
// k_mod.d

ubyte[MemSiz]  MemPool;
```

A number of tests need to be run on code in `k_mod`,
with different sizes of the static array `MemPool` in each test.
So each test has the enum `MemSiz`, but set to different values.

However, DMD does not recognise the enum `MemSiz` within `k_mod`,
 failing with 'undefined identifier'.

Is there a way of forcing DMD to extend the scope of `MemSiz` to 
include `k_mod`?


Best regards





Re: Scope of enum

2021-07-11 Thread jfondren via Digitalmars-d-learn

On Sunday, 11 July 2021 at 10:58:58 UTC, DLearner wrote:
Is there a way of forcing DMD to extend the scope of `MemSiz` 
to include `k_mod`?


Best regards


```
$ cat k_mod.d
import test01;

ubyte[MemSiz] MemPool;

$ cat test01.d
enum MemSiz = 240;

void main() {
import std.stdio, k_mod;
writeln(typeid(MemPool));
}

$ dmd test01.d k_mod.d
$ ./test01
ubyte[240]
```


Re: Scope of enum

2021-07-11 Thread DLearner via Digitalmars-d-learn

On Sunday, 11 July 2021 at 12:01:27 UTC, jfondren wrote:

On Sunday, 11 July 2021 at 10:58:58 UTC, DLearner wrote:
Is there a way of forcing DMD to extend the scope of `MemSiz` 
to include `k_mod`?


Best regards


```
$ cat k_mod.d
import test01;

ubyte[MemSiz] MemPool;

$ cat test01.d
enum MemSiz = 240;

void main() {
import std.stdio, k_mod;
writeln(typeid(MemPool));
}

$ dmd test01.d k_mod.d
$ ./test01
ubyte[240]
```


Doesn't seem to work for me (Windows):
```
C:\Users\SoftDev\Documents\BDM\D\Examples\CTFE\T2>type test01.d
//  test01.d

enum MemSiz  = 240;

void main() {

   import k_mod;
}


C:\Users\SoftDev\Documents\BDM\D\Examples\CTFE\T2>type k_mod.d
// k_mod.d


ubyte[MemSiz]  MemPool;


C:\Users\SoftDev\Documents\BDM\D\Examples\CTFE\T2>dmd -i test01.d 
k_mod.d

k_mod.d(4): Error: undefined identifier `MemSiz``
```


Re: Scope of enum

2021-07-11 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 11 July 2021 at 12:37:20 UTC, DLearner wrote:

C:\Users\SoftDev\Documents\BDM\D\Examples\CTFE\T2>type k_mod.d
// k_mod.d


ubyte[MemSiz]  MemPool;


You didn't import the other module here.


D's imports aren't like C's includes. Each module is independent 
and can only see what it itself imports.


A module has no idea who is importing it. It only knows what it 
imports inside itself.


Re: mixin template's alias parameter ... ignored ?

2021-07-11 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 11 July 2021 at 05:20:49 UTC, someone wrote:

```d
mixin template templateUGC (
   typeStringUTF,
   alias lstrStructureID
   ) {

   public struct lstrStructureID {

  typeStringUTF whatever;

   }



This creates a struct with teh literal name `lstrStructureID`. 
Just like any other name. So it is NOT the value of the variable.



```d
   public struct mixin(lstrStructureID) { ... }
```

because the argument seems to require a complete statement.


Indeed, you'd have to mixin the whole thing like

mixin("public struct " ~ lstrStructureId ~ " { ... } ");


Re: assert(false) and GC

2021-07-11 Thread Steven Schveighoffer via Digitalmars-d-learn

On 7/10/21 12:32 PM, Mathias LANG wrote:

On Saturday, 10 July 2021 at 01:38:06 UTC, russhy wrote:

On Saturday, 10 July 2021 at 01:23:26 UTC, Steven Schveighoffer wrote:


I think it's the throwing/catching of the `Throwable` that is 
allocating. But I don't know from where the allocation happens.




i think you are right


Try to use `@nogc` instead, it'll show you it does not allocate.
A caveat though: A failing assert used to allocate before v2.097.0 (the 
latest version), it does not anymore.


However, as your test captures all GC allocation, it likely triggers 
while the runtime is initializing.


No, because if you remove the assert, and just include the override of 
gc functions, nothing triggers. I can't remember which release, but the 
runtime has been changed to not use the GC until the application needs it.


The assert is triggering the GC, either on the throw or the catch (i.e. 
printing out the stack trace).


I tend to think the latter, but hard to prove with this simple test.

If you want to know where it is, just debug it and break in the GC 
allocation function.


-Steve


Re: mixin template's alias parameter ... ignored ?

2021-07-11 Thread Steven Schveighoffer via Digitalmars-d-learn

On 7/11/21 8:49 AM, Adam D Ruppe wrote:

On Sunday, 11 July 2021 at 05:20:49 UTC, someone wrote:

```d
mixin template templateUGC (
   typeStringUTF,
   alias lstrStructureID
   ) {

   public struct lstrStructureID {

  typeStringUTF whatever;

   }



This creates a struct with teh literal name `lstrStructureID`. Just like 
any other name. So it is NOT the value of the variable.



```d
   public struct mixin(lstrStructureID) { ... }
```

because the argument seems to require a complete statement.


Indeed, you'd have to mixin the whole thing like

mixin("public struct " ~ lstrStructureId ~ " { ... } ");


when I've done this kind of stuff, what I usually do is:

```d
struct Thing {
  ... // actual struct
}

mixin("alias ", lstrStructureID, " = Thing;");
```

the downside is that the actual struct name symbol will be `Thing`, or 
whatever you called it. But at least you are not writing lots of code 
using mixins.


-Steve


Re: Scope of enum

2021-07-11 Thread DLearner via Digitalmars-d-learn

On Sunday, 11 July 2021 at 12:47:48 UTC, Adam D Ruppe wrote:

On Sunday, 11 July 2021 at 12:37:20 UTC, DLearner wrote:

C:\Users\SoftDev\Documents\BDM\D\Examples\CTFE\T2>type k_mod.d
// k_mod.d


ubyte[MemSiz]  MemPool;


You didn't import the other module here.


D's imports aren't like C's includes. Each module is 
independent and can only see what it itself imports.


A module has no idea who is importing it. It only knows what it 
imports inside itself.


Adding the second import worked - thank you.

But there is a point of principle:
To me, doesn't really matter about what goes in `test01.d`, just 
test harness.


But adding `import test01.d` to `k_mod.d` looks like 'mixing' the 
real code in `k_mod.d` with other code that is just for the 
support of the test harness.

And that support would have to be changed for each test.
Surely we want the target code, if possible, to be unchanged from 
test to test?


Is there a 'D' way of avoiding the issue?




Re: mixin template's alias parameter ... ignored ?

2021-07-11 Thread zjh via Digitalmars-d-learn

On Sunday, 11 July 2021 at 12:49:28 UTC, Adam D Ruppe wrote:

This creates a struct with teh literal name `lstrStructureID`. 
Just like any other name. So it is NOT the value of the 
variable.


Could you explain more detail?


Re: Scope of enum

2021-07-11 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 11 July 2021 at 13:21:35 UTC, DLearner wrote:

Is there a 'D' way of avoiding the issue?


Pass the size as a parameter to the thing instead of trying to 
combine things. like



mixin template Thing(size_t size) {
   ubyte[size] pool;
}


and where you want it like


mixin Thing!(100);


and somewher eelse

mixin Thing!(500);


and they're separate variables with different sizes that you can 
use.



idk what your thing needs in context though


Re: mixin template's alias parameter ... ignored ?

2021-07-11 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 11 July 2021 at 13:30:27 UTC, zjh wrote:

Could you explain more detail?


It is just normal code with a normal name. The fact there's 
another variable with the same name doesn't change anything.


Re: Can I get the time "Duration" in "nsecs" acurracy?

2021-07-11 Thread Steven Schveighoffer via Digitalmars-d-learn

On 7/9/21 5:13 PM, rempas wrote:

On Friday, 9 July 2021 at 20:54:21 UTC, Paul Backus wrote:

On Friday, 9 July 2021 at 20:43:48 UTC, rempas wrote:
I'm reading the library reference for 
[core.time](https://dlang.org/phobos/core_time.html#Duration) and It 
says that the duration is taken in "hnsecs" and I cannot understand 
if we can change that and choose the precision. Does anyone know if 
we can do that?


It is stored internally in "hnsecs", but you can convert it to other 
units using the `total` method [1]; for example, 
`myDuration.total!"nsecs"`.


[1] https://dlang.org/phobos/core_time.html#.Duration.total


It doesn't work for me. I have the following code:

```
MonoTime start = MonoTime.currTime();
// Doing stuff
MonoTime end = MonoTime.currTime();
Duration dur = end - start;
dur = dur.total!"nsecs";
```

and I get the following error message:

"Error: cannot implicitly convert expression \`dur.total()\` of type 
\`long\` to \`Duration`"


BTW `Duration` is truncating here. `MonoTime` stores its values in 
implementation-defined ticks, which can be nanoseconds or any other 
thing that the specific clock uses. Whereas `Duration` uses strictly hnsecs.


If you want to get sub-hnsec resolution, use the `ticks` member of 
`MonoTime` and `ticksPerSecond` and do the math for the units.


See the warning 
[here](https://dlang.org/phobos/core_time.html#.MonoTimeImpl.opBinary).


-Steve


Re: mixin template's alias parameter ... ignored ?

2021-07-11 Thread zjh via Digitalmars-d-learn

```d
mixin template templateUGC ( typeStringUTF, alias 
lstrStructureID){

   public struct lstrStructureID {
  typeStringUTF w;
   }
}
mixin templateUGC!(string,  "gudtUGC08");
```
You say `This creates a struct with teh literal name 
lstrStructureID`.

I tried,can compile,but I don't know generate what.

Could you explain more detail?


Re: mixin template's alias parameter ... ignored ?

2021-07-11 Thread zjh via Digitalmars-d-learn

On Sunday, 11 July 2021 at 14:04:14 UTC, zjh wrote:

just genenrate `lstrStructureID` struct.



catching segfault using try_ catch

2021-07-11 Thread seany via Digitalmars-d-learn

Is it possible to catch a segfault - using try/catch loop?
Thank you


Re: catching segfault using try_ catch

2021-07-11 Thread Paul Backus via Digitalmars-d-learn

On Sunday, 11 July 2021 at 20:18:18 UTC, seany wrote:

Is it possible to catch a segfault - using try/catch loop?
Thank you


I know it's possible on Linux using the `etc.linux.memoryerror` 
module [1]. Not sure about Windows.


[1] https://druntime.dpldocs.info/etc.linux.memoryerror.html


Re: catching segfault using try_ catch

2021-07-11 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 11 July 2021 at 21:15:30 UTC, Paul Backus wrote:
I know it's possible on Linux using the `etc.linux.memoryerror` 
module [1]. Not sure about Windows.


With -m32, it just works. With -m32mscoff I'm not sure. With 
-m64, as far as I know, there is no way.


Re: catching segfault using try_ catch

2021-07-11 Thread seany via Digitalmars-d-learn

 On Sunday, 11 July 2021 at 21:15:30 UTC, Paul Backus wrote:
I know it's possible on Linux using the `etc.linux.memoryerror` 
module [1]. Not sure about Windows.



Linux would be sufficient.
Is there an example i can use Thank you.


Re: catching segfault using try_ catch

2021-07-11 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 11 July 2021 at 23:34:38 UTC, seany wrote:

Is there an example i can use Thank you.


You just call the registerMemoryHandler() function from that 
module at some point in your main function before doing other 
work.


Re: mixin template's alias parameter ... ignored ?

2021-07-11 Thread someone via Digitalmars-d-learn
On Sunday, 11 July 2021 at 13:14:23 UTC, Steven Schveighoffer 
wrote:



when I've done this kind of stuff, what I usually do is:

```d
struct Thing {
  ... // actual struct
}

mixin("alias ", lstrStructureID, " = Thing;");
```

the downside is that the actual struct name symbol will be 
`Thing`, or whatever you called it. But at least you are not 
writing lots of code using mixins.


-Steve


Thanks for your tip Steve, I ended with something similar, I'll 
be posting my whole example below.


Re: mixin template's alias parameter ... ignored ?

2021-07-11 Thread someone via Digitalmars-d-learn

On Sunday, 11 July 2021 at 12:49:28 UTC, Adam D Ruppe wrote:


Indeed, you'd have to mixin the whole thing like

mixin("public struct " ~ lstrStructureId ~ " { ... } ");


As I mentioned in my previous reply to Ali this could be viable 
for one-liners-or-so, but for chunks of code having, say, a 
couple hundred lines for one UDT, it will become 
debug/maintenance-hell soon ... so clearly; it is a no-go ... for 
my specific case at least.


Re: mixin template's alias parameter ... ignored ?

2021-07-11 Thread someone via Digitalmars-d-learn

On Sunday, 11 July 2021 at 05:54:48 UTC, Ali Çehreli wrote:


Ali


Primarily to Ali & Steve for their help, be advised, this post 
will be somehow ... long.


Some bit of background to begin with: a week or so ago I posted 
asking advice on code safeness, and still I didn't reply to the 
ones that kindly answered. Seeing some replies, and encountering 
a code issue regarding string manipulation, I pretty soon figured 
out that I still did not have solid knowledge on many basic 
things regarding D, so I put the brakes on, and went to square 
one and started reading and researching some things a bit more 
... slowly.


One of the things that struck me this week is that UniCode string 
manipulation in many cases is more complex that I previously 
thought, because there is no precise-concept of what is a 
character in UniCode, at least, not the way we are used to with 
plain-old-ASCII. After reading a lot of about it (this was good: 
https://manishearth.github.io/blog/2017/01/14/stop-ascribing-meaning-to-unicode-code-points/) I learned of code-units, code-points, abstract-graphemes, graphemes-clusters, and the like.


And I learned the inner details of the UTF encodings and that 
UTF-32 is best (almost required) for string processing (easier, 
faster, etc) and of course UTF-8 for definitive storage, and 
UTF-16 to the trashcan unless you need to interface with Windows 
(I was previously using UTF-8 within all my code for processing).


So, in order to manipulate a string, say, left(n), right(n), 
substr(n,m), ie: the usual stuff for many languages/libraries, I 
need to operate on grapheme-clusters and not in code-points and 
never ever on code-units, at least, for unexpected text, ie: 
incoming text, user-input, etc, the things that we can not 
control beforehand.


Both primary D books, Andrei's and Ali's ones, as the D 
documentation, have plenty of examples but they are mainly 
focused on simple things like strings having 
nothing-out-of-the-ordinary. They perform string manipulation 
mainly slicing the source string (ie: the char array) with the 
functions of std.range like take, takeOne, etc.


I needed to set this things once-and-for-all for my code and thus 
I decided to build a grapheme-aware UDT that once instantiated 
with any given string will provide the usual string manipulation 
functions so I can forget the minutiae about them. The unittest 
at the bottom has many usage examples.


The whole UDT needed to be templated for the three string types 
(string, dstring, wstring -and nothing else) and this was what 
produced this post to begin with. This issue was solved, not the 
way I liked to, but solved. The code works alas for something 
that smells like a phobos bug (# 20483) using foreach with 
grapheme arrays (foreach always missing the last one).


I ended up with the following (as usual advice/suggestions 
welcomed):


```d
/// testing D on 2021-06~07

import std.algorithm : map, joiner;
import std.array : array;
import std.conv : to;
import std.range : walkLength, take, tail, drop, dropBack;
import std.stdio;
import std.uni : Grapheme, byGrapheme;

alias stringUGC = Grapheme;
alias stringUGC08 = gudtUGC!(stringUTF08);
alias stringUGC16 = gudtUGC!(stringUTF16);
alias stringUGC32 = gudtUGC!(stringUTF32);
alias stringUTF08 = string;  /// same as immutable(char )[];
alias stringUTF16 = dstring; /// same as immutable(dchar)[];
alias stringUTF32 = wstring; /// same as immutable(wchar)[];

void main() {}

//mixin templateUGC!(stringUTF08, r"gudtUGC08"w); /// if these 
were possible there will be no need for stringUGC## aliases in 
main()

//mixin templateUGC!(stringUTF16, r"gudtUGC16"w);
//mixin templateUGC!(stringUTF32, r"gudtUGC32"w);

//template templateUGC (
//   typeStringUTF,
//   alias lstrStructureID
//   ) {

public struct gudtUGC(typeStringUTF) { /// UniCode grapheme 
cluster‐aware string manipulation


   void popFront() { ++pintSequenceCurrent; }
   bool empty() { return pintSequenceCurrent == 
pintSequenceCount; }
   typeStringUTF front() { return toUTFtake(pintSequenceCurrent); 
}


   private stringUGC[] pugcSequence;
   private size_t pintSequenceCount = cast(size_t) 0;
   private size_t pintSequenceCurrent = cast(size_t) 0;

   @property public size_t count() { return pintSequenceCount; }

   this(scope const typeStringUTF lstrSequence) {

  decode(lstrSequence);

   }

   @safe public size_t decode(
  scope const typeStringUTF lstrSequence
  ) {

  scope size_t lintSequenceCount = cast(size_t) 0;

  if (lstrSequence is null) {

 pugcSequence = null;
 pintSequenceCount = cast(size_t) 0;
 pintSequenceCurrent = cast(size_t) 0;

  } else {

 pugcSequence = lstrSequence.byGrapheme.array;
 pintSequenceCount = pugcSequence.walkLength;
 pintSequenceCurrent = cast(size_t) 1;

 lintSequenceCount = pintSequenceCount;

  }

  return lintSequenceCount;

   }

   @safe public typeStringUTF encode() { /// Un

Re: Trivial simple OpenGl working example

2021-07-11 Thread Виталий Фадеев via Digitalmars-d-learn

On Saturday, 10 July 2021 at 19:43:07 UTC, Danny Arends wrote:

On Saturday, 10 July 2021 at 12:41:19 UTC, Виталий Фадеев wrote:

[...]


Just disable shader compilation using dub, the shader only 
needs to be compiled once. Since you're on linux it fails to 
execute glslc.exe (the windows executable). If you compiled 
them yourself you can remove it from dub prebuild commands


Thanks, Danny Arends!
I fixed the configuration in dub.json, and in the application.d I 
fixed VK_API_VERSION_1_0. It worked!


Look in to the pull requests:

https://github.com/DannyArends/CalderaD/pulls



How do I check if a variable is a multidimensional (2D) array?

2021-07-11 Thread Kirill via Digitalmars-d-learn
I know there is isArray!T and similar functionality in 
std.traits. But I couldn't find the functionality that can help 
me check if I have a multidimensional array. Is there any? How do 
I create my own?


Thanks in advance.


Re: How do I check if a variable is a multidimensional (2D) array?

2021-07-11 Thread jfondren via Digitalmars-d-learn

On Monday, 12 July 2021 at 04:25:00 UTC, Kirill wrote:
I know there is isArray!T and similar functionality in 
std.traits. But I couldn't find the functionality that can help 
me check if I have a multidimensional array. Is there any? How 
do I create my own?


Thanks in advance.


from https://github.com/PhilippeSigaud/D-templates-tutorial

```d
template rank(T) {
static if (is(T t == U[], U))
enum size_t rank = 1 + rank!(U);
else
enum size_t rank = 0;
}

unittest {
int a;
int[] b;
int[][] c;
assert(rank!(typeof(a)) == 0);
assert(rank!(typeof(b)) == 1);
assert(rank!(typeof(c)) == 2);
}
```

as an example of a recursive template. There's also an 
implementation for ranges.


Re: How do I check if a variable is a multidimensional (2D) array?

2021-07-11 Thread Kirill via Digitalmars-d-learn

On Monday, 12 July 2021 at 05:08:29 UTC, jfondren wrote:

On Monday, 12 July 2021 at 04:25:00 UTC, Kirill wrote:
I know there is isArray!T and similar functionality in 
std.traits. But I couldn't find the functionality that can 
help me check if I have a multidimensional array. Is there 
any? How do I create my own?


Thanks in advance.


from https://github.com/PhilippeSigaud/D-templates-tutorial

```d
template rank(T) {
static if (is(T t == U[], U))
enum size_t rank = 1 + rank!(U);
else
enum size_t rank = 0;
}

unittest {
int a;
int[] b;
int[][] c;
assert(rank!(typeof(a)) == 0);
assert(rank!(typeof(b)) == 1);
assert(rank!(typeof(c)) == 2);
}
```

as an example of a recursive template. There's also an 
implementation for ranges.


Thanks!


Re: mixin template's alias parameter ... ignored ?

2021-07-11 Thread ag0aep6g via Digitalmars-d-learn

On 12.07.21 03:37, someone wrote:
I ended up with the following (as usual advice/suggestions welcomed): 
[...]> alias stringUTF16 = dstring; /// same as immutable(dchar)[];> 
alias stringUTF32 = wstring; /// same as immutable(wchar)[];
Bug: You mixed up `wstring` and `dstring`. `wstring` is UTF-16. 
`dstring` is UTF-32.


[...]
public struct gudtUGC(typeStringUTF) { /// UniCode grapheme 
cluster‐aware string manipulation


Style: `typeStringUTF` is a type, so it should start with a capital 
letter (`TypeStringUTF`).


[...]

    private size_t pintSequenceCount = cast(size_t) 0;
    private size_t pintSequenceCurrent = cast(size_t) 0;


Style: There's no need for the casts (throughout).

[...]
    @safe public typeStringUTF encode() { /// UniCode grapheme cluster 
to UniCode UTF‐encoded string


   scope typeStringUTF lstrSequence = null;

[...]

   return lstrSequence;

    }


Bug: `scope` makes no sense if you want to return `lstrSequence` 
(throughout).


    @safe public typeStringUTF toUTFtake( /// UniCode grapheme cluster 
to UniCode UTF‐encoded string

   scope const size_t lintStart,
   scope const size_t lintCount = cast(size_t) 1
   ) {

Style: `scope` does nothing on `size_t` parameters (throughout).

[...]

   if (lintStart <= lintStart + lintCount) {

[...]

  scope size_t lintRange1 = lintStart - cast(size_t) 1;


Possible bug: Why subtract 1?


  scope size_t lintRange2 = lintRange1 + lintCount;

  if (lintRange1 >= cast(size_t) 0 && lintRange2 <= 
pintSequenceCount) {


Style: The first half of that condition is pointless. `lintRange1` is 
unsigned, so it will always be greater than or equal to 0. If you want 
to defend against overflow, you have to do it before subtracting.


[...]

  }

   }

[...]

    }

[...]
    @safe public typeStringUTF toUTFpadL( /// UniCode grapheme cluster 
to UniCode UTF‐encoded string

   scope const size_t lintCount,
   scope const typeStringUTF lstrPadding = cast(typeStringUTF) r" "


Style: Cast is not needed (throughout).


   ) {

[...]

    }

[...]

}

[...]