Re: Profiling

2021-02-10 Thread drug via Digitalmars-d-learn

On 2/10/21 2:52 PM, JG wrote:

On Tuesday, 9 February 2021 at 18:33:16 UTC, drug wrote:

On Tuesday, 9 February 2021 at 07:45:13 UTC, JG wrote:
I was trying to profile a d program. So I ran: dub build 
--build=profile. I then ran the program and it produced trace.log and 
trace.def. I then ran d-profile-viewer and got the following error:


std.conv.ConvException@/home/jg/dlang/ldc-1.24.0/bin/../import/std/conv.d(2382): 
Unexpected '-' when converting from type char[] to type ulong


I'm guessing only but it looks like slurp is used to read text output 
but the format used does not correspond to the real data format.


Thanks for the suggestions. However, I would prefer not to spend time 
trying to debug d-profile-viewer at the moment.


As a follow up question I would like to know what tool people use to 
profile d programs?


Could you provide trace.log that I can take a look? I have no much time 
but some bugs can be fixed fast. It would be ideal if you create the 
issue in the package repo and provides the test case. Also there is 
probability the author will fix this.


Re: Profiling

2021-02-09 Thread drug via Digitalmars-d-learn

On Tuesday, 9 February 2021 at 07:45:13 UTC, JG wrote:
I was trying to profile a d program. So I ran: dub build 
--build=profile. I then ran the program and it produced 
trace.log and trace.def. I then ran d-profile-viewer and got 
the following error:


std.conv.ConvException@/home/jg/dlang/ldc-1.24.0/bin/../import/std/conv.d(2382):
 Unexpected '-' when converting from type char[] to type ulong


I'm guessing only but it looks like slurp is used to read text 
output but the format used does not correspond to the real data 
format.


Re: Using mir to work with matrices

2021-02-01 Thread drug via Digitalmars-d-learn

On 1/29/21 8:20 PM, 9il wrote:

On Friday, 29 January 2021 at 15:35:49 UTC, drug wrote:
Between is there a plan to implement some sort of static slice where 
the lengths of the dimensions are known in compile time? Compiler help 
is very useful.


No. BLAS/LAPACK API's can't use compile-time information. User matrix 


I see

loops can be optimized by the compiler using constants and without 
introducing new types. If you need a stack-allocated matrix, then a 1D 
stack-allocated array can be used >

import mir.slice.slice;

double[12] payload;
auto matrix = payload[].sliced(3, 4);



Yes, it's how I did it.


Re: Using mir to work with matrices

2021-01-29 Thread drug via Digitalmars-d-learn

On 1/29/21 4:50 PM, 9il wrote:

On Tuesday, 26 January 2021 at 14:43:08 UTC, drug wrote:
It is not easy to understand what mir library one should use to work 
with matrices. mir-glas turns out unsupported now and I try to use 
mir-blas. I need to reimplement my Kalman filter version to use more 
high dimension matrix than 4x4 plus Kronecker product. Is mir-blas 
recommended to work with matrices?


Yes, it is wrapper around a common BLAS libraries such as OpenBLAS or 
Intel MKL.




I've implemented the filter using mir-lapack. But then I found lubeck - 
I didn't try it but it seemed that it provided more high level 
functionality like matrix inversion (`inv` wrapper is much handy than 
`getrf_` and `dgetri_` combination I used directly). So my first thought 
was that lubeck should be more preferable but then I found there was 
lubeck2 so I didn't know what to think further. But I pretty well 
understand the situation and the reasons behind it. Thank you for you 
efforts!


Between is there a plan to implement some sort of static slice where the 
lengths of the dimensions are known in compile time? Compiler help is 
very useful.


Using mir to work with matrices

2021-01-26 Thread drug via Digitalmars-d-learn
It is not easy to understand what mir library one should use to work 
with matrices. mir-glas turns out unsupported now and I try to use 
mir-blas. I need to reimplement my Kalman filter version to use more 
high dimension matrix than 4x4 plus Kronecker product. Is mir-blas 
recommended to work with matrices?


Re: Convert double to long if lossless

2021-01-20 Thread drug via Digitalmars-d-learn

On 1/19/21 9:28 PM, Per Nordlöw wrote:

On Tuesday, 19 January 2021 at 16:14:17 UTC, drug wrote:

   https://dlang.org/phobos/std_bitmanip.html#FloatRep


Doesn't this pattern already cover all possible cases of `value` needed?

void f(double value)
{
     auto lvalue = cast(long)value;
     if (lvalue == value) // `value` lacks fraction and in range 
[long.min .. long.max]

     {
     // use long lvalue
     return;
     }
     // use double value
}



Sure. You don't even need to use isFinite like I wrote above because 
special values like inf and nan become some long values and will never 
equal to itself.


Your question motivated me to do a little workout for my brains. Sorry 
if it confused you.


Re: Convert double to long if lossless

2021-01-19 Thread drug via Digitalmars-d-learn

On 1/19/21 6:50 PM, Ali Çehreli wrote:

On 1/19/21 6:04 AM, drug wrote:

 > Another (low level) way is to shift mantissa left by exponent value.

Luckily, we already have a helper in Phobos:

   https://dlang.org/phobos/std_bitmanip.html#FloatRep

Ali



That makes life simpler, thanks for sharing this:
```D
import std;

void main()
{
auto valueRange = [
10.001,
10.0001, // in fact this is just 10.0
];

foreach(value; valueRange)
{
auto dr = DoubleRep(value);
const hasFractional = !!(dr.fraction << (dr.exponent-1023+12));
writefln("has `%.18f` fractional part: %s", value, hasFractional);
}
}
```

but I think that casting to long and comparing it to the value is easier 
to understand and more portable:

```D
import std;

void main()
{
auto valueRange = [
10.001,
10.0001, // in fact this is 10.0
];

foreach(value; valueRange)
{
const hasFractional = (value != cast(long)value);
writefln("has `%.18f` fractional part: %s", value, hasFractional);
}
}
```

P.S. shouldn't compiler emit the error if a literal can't be represented 
lossless?


Re: Convert double to long if lossless

2021-01-19 Thread drug via Digitalmars-d-learn

On 1/19/21 5:04 PM, drug wrote:

On 1/19/21 4:48 PM, Per Nordlöw wrote:

On Tuesday, 19 January 2021 at 13:36:58 UTC, Steven Schveighoffer wrote:

Use a cast instead.

const lvalue = cast(long)value;


Ahh, good point.

Followd by a compare of the original value I presume.


don't forget to check by std.math.isFinite before casting

Another (low level) way is to shift mantissa left by exponent value. If 
the remainder equals to zero then no fractal part was encoded in the 
floating value.


*fractional part


Re: Convert double to long if lossless

2021-01-19 Thread drug via Digitalmars-d-learn

On 1/19/21 4:48 PM, Per Nordlöw wrote:

On Tuesday, 19 January 2021 at 13:36:58 UTC, Steven Schveighoffer wrote:

Use a cast instead.

const lvalue = cast(long)value;


Ahh, good point.

Followd by a compare of the original value I presume.


don't forget to check by std.math.isFinite before casting

Another (low level) way is to shift mantissa left by exponent value. If 
the remainder equals to zero then no fractal part was encoded in the 
floating value.


Re: Directory recursive walking

2021-01-14 Thread drug via Digitalmars-d-learn

On 1/14/21 7:06 PM, dog2002 wrote:

On Thursday, 14 January 2021 at 16:01:43 UTC, drug wrote:

On 1/14/21 6:55 PM, drug wrote:


But this method consumes a huge amount of memory (up to 4 GB and 
more). Is there a more appropriate way to walk directories 
recursively that does not consume a lot of memory?


DirEntry is a struct. First of all I would try this:
```D
foreach(ref entry; dirEntries(path, SpanMode.shallow, false))
```


Does your directory just contain large amount of files?


Yes. I forgot to add it in the original post.


How much files do you have? DirEntry size is 168 bytes only and dirEntry 
is lazy range so I'm curious what is the reason of huge memory 
consumption. Do you use Windows 32 bits between?


Re: Directory recursive walking

2021-01-14 Thread drug via Digitalmars-d-learn

On 1/14/21 7:30 PM, dog2002 wrote:

On Thursday, 14 January 2021 at 16:18:28 UTC, drug wrote:

On 1/14/21 7:06 PM, dog2002 wrote:

On Thursday, 14 January 2021 at 16:01:43 UTC, drug wrote:

[...]


Yes. I forgot to add it in the original post.


Does using `ref` changed anything?
Try following:
```
import std;

void DirIteration(ref DirEntry dir) {
    try {
    foreach(ref entry; dirEntries(dir, SpanMode.shallow, false)) { 
//SpanMode.shallow allows skip directories if any error happens

    if (entry.isFile && !entry.isSymlink)
    writeln(entry); //Or something instead of this
    if (entry.isDir)
    DirIteration(entry);
    }
    }
    catch (Throwable) {}
}

void main()
{
    auto de = DirEntry(".");
    DirIteration(de);
}
```


No, it doesn't. Seems like memory can't clear.


It is a recursion. Memory will be freed only after completion. Then I 
would try to get rid of recursion.


Re: Directory recursive walking

2021-01-14 Thread drug via Digitalmars-d-learn

On 1/14/21 7:06 PM, dog2002 wrote:

On Thursday, 14 January 2021 at 16:01:43 UTC, drug wrote:

On 1/14/21 6:55 PM, drug wrote:


But this method consumes a huge amount of memory (up to 4 GB and 
more). Is there a more appropriate way to walk directories 
recursively that does not consume a lot of memory?


DirEntry is a struct. First of all I would try this:
```D
foreach(ref entry; dirEntries(path, SpanMode.shallow, false))
```


Does your directory just contain large amount of files?


Yes. I forgot to add it in the original post.


Does using `ref` changed anything?
Try following:
```
import std;

void DirIteration(ref DirEntry dir) {
try {
foreach(ref entry; dirEntries(dir, SpanMode.shallow, false)) { 
//SpanMode.shallow allows skip directories if any error happens

if (entry.isFile && !entry.isSymlink)
writeln(entry); //Or something instead of this
if (entry.isDir)
DirIteration(entry);
}
}
catch (Throwable) {}
}

void main()
{
auto de = DirEntry(".");
DirIteration(de);
}
```


Re: Directory recursive walking

2021-01-14 Thread drug via Digitalmars-d-learn

On 1/14/21 6:55 PM, drug wrote:


But this method consumes a huge amount of memory (up to 4 GB and 
more). Is there a more appropriate way to walk directories recursively 
that does not consume a lot of memory?


DirEntry is a struct. First of all I would try this:
```D
foreach(ref entry; dirEntries(path, SpanMode.shallow, false))
```


Does your directory just contain large amount of files?


Re: Directory recursive walking

2021-01-14 Thread drug via Digitalmars-d-learn

On 1/14/21 6:46 PM, dog2002 wrote:
I need to make some operations with all the files in a directory and 
subdirectories. Currently, I do it like this:


import std;

void DirIteration(string path) {
     try {
     foreach(entry; dirEntries(path, SpanMode.shallow, false)) { 
//SpanMode.shallow allows skip directories if any error happens

     if (entry.isFile && !entry.isSymlink)
     writeln(entry); //Or something instead of this
     if (entry.isDir)
     DirIteration(entry);
     }
     }
     catch (Throwable) {}
}

void main()
{
     DirIteration("C:\\Users\\angrypuppy\\MyDir");
}

But this method consumes a huge amount of memory (up to 4 GB and more). 
Is there a more appropriate way to walk directories recursively that 
does not consume a lot of memory?


DirEntry is a struct. First of all I would try this:
```D
foreach(ref entry; dirEntries(path, SpanMode.shallow, false))
```


Re: How to debug D on Linux

2021-01-13 Thread drug via Digitalmars-d-learn

On 1/13/21 4:47 PM, Roguish wrote:


Also, what does it mean to "always emit a stackframe" (compiler option 
-gs) ?


Short answer - sometimes the compiler does not emit a stackframe (due to 
optimization for example). In general if you are able to debug your 
binary by gdb then you don't need to worry this flag.

I skip long answer, sorry :-) But you can google about stack frame.


Re: How to debug D on Linux

2021-01-13 Thread drug via Digitalmars-d-learn

On 1/13/21 4:47 PM, Roguish wrote:

On Wednesday, 13 January 2021 at 13:30:48 UTC, Roguish wrote:

Anything else I need to know when debugging on Linux, without an IDE?


One specific question I have is: what's the difference between -g and 
-debug and -d-debug?


Also, what does it mean to "always emit a stackframe" (compiler option 
-gs) ?


https://stackoverflow.com/questions/17392200/debugging-dmd-generate-program-through-gdb


Re: Can I output strings using core.stdc.stdio?

2020-12-23 Thread drug via Digitalmars-d-learn

On 12/23/20 3:23 PM, Godnyx wrote:


Any ideas?


Just fix your typos:

```D
import std : printf, toStringz;

void put(A...)(string prompt, A args) {
static foreach (ulong i; 0..args.length) {
static if (is(typeof(args[i]) == string))
printf("%s\n", args[i].toStringz);
static if (is(typeof(args[i]) == int))
printf("%i\n", args[i]);
static if (is(typeof(args[i]) == bool)) {
if (args[i])
printf("true");
else
printf("false");
}
}
}

void main() {
put("Prompt:", "Hello, my age is: ", 19, true);
}
```

P.S. replace `arg` by `args[i]`



Re: Running unit tests from DUB single file packages

2020-12-22 Thread drug via Digitalmars-d-learn

On 12/22/20 8:32 PM, jmh530 wrote:

On Tuesday, 22 December 2020 at 15:06:09 UTC, drug wrote:

[snip]


But what do you mean exactly by "work with dependency"? As I 
understand, `dub test` does not run unit tests in dependencies and 
single file packages work with dependencies in general. Do you mean 
something else? I'm finishing the new PR to fix #2051 finally and I'd 
like to know if there is something else I should include in it.


https://github.com/dlang/dub/pull/2064


Thanks. It looks like your UT with taggedalgebraic does exactly what I 
was looking for.


My problem is that run.dlang.org will skip unittests when you have 
dependencies. I had made some progress on fixing this a few months ago 
[1], but put it on the back burner when I ran into similar issues that 
the OP was dealing with. The problem ultimately came down to dub test 
not working with --single, which it looks like this latest PR will fix 
for good.


[1] https://github.com/dlang-tour/core-exec/pull/56


Ah, I see. Then I would say that `dub test` doesn't run unit tests from 
the main source file (where the main function is placed). And this is 
the reason, I guess. Now `dub test` runs unit tests from the main source 
file if the package is a single file one. I think that dependencies is 
irrelevant here, at least I didn't do anything special for them. Let's 
see what happens.


Re: Running unit tests from DUB single file packages

2020-12-22 Thread drug via Digitalmars-d-learn

On 12/22/20 10:52 AM, drug wrote:

On 12/21/20 7:31 PM, jmh530 wrote:

On Monday, 21 December 2020 at 11:31:49 UTC, drug wrote:

[snip]
Unfortunately I'm very busy. But I check it again and it turns out 
that the fix does not resolve the problem completely. This PR just 
remove the single file from testing so currently dub does not run 
unit tests in the single file package at all. The first variant 
(https://github.com/dlang/dub/pull/2050) fixes the issue indeed. I 
need to reevaluate these PRs and close the issue. I'll do it later.


Thanks for taking a look.


Not at all.

But what do you mean exactly by "work with dependency"? As I understand, 
`dub test` does not run unit tests in dependencies and single file 
packages work with dependencies in general. Do you mean something else? 
I'm finishing the new PR to fix #2051 finally and I'd like to know if 
there is something else I should include in it.


https://github.com/dlang/dub/pull/2064


Re: Running unit tests from DUB single file packages

2020-12-21 Thread drug via Digitalmars-d-learn

On 12/21/20 7:31 PM, jmh530 wrote:

On Monday, 21 December 2020 at 11:31:49 UTC, drug wrote:

[snip]
Unfortunately I'm very busy. But I check it again and it turns out 
that the fix does not resolve the problem completely. This PR just 
remove the single file from testing so currently dub does not run unit 
tests in the single file package at all. The first variant 
(https://github.com/dlang/dub/pull/2050) fixes the issue indeed. I 
need to reevaluate these PRs and close the issue. I'll do it later.


Thanks for taking a look.


Not at all.

But what do you mean exactly by "work with dependency"? As I understand, 
`dub test` does not run unit tests in dependencies and single file 
packages work with dependencies in general. Do you mean something else? 
I'm finishing the new PR to fix #2051 finally and I'd like to know if 
there is something else I should include in it.


Re: Running unit tests from DUB single file packages

2020-12-21 Thread drug via Digitalmars-d-learn

On 12/20/20 9:31 PM, jmh530 wrote:

On Wednesday, 2 December 2020 at 12:51:11 UTC, drug wrote:

[snip]


Thanks! Let's see if it gets merged or if a slightly more involved
solution is needed.



Remake it - https://github.com/dlang/dub/pull/2052
This has more chances to be merged


Looks like this got merged and will be part of the newest version, which 
is great news. Have you checked that it works with dependencies?


Unfortunately I'm very busy. But I check it again and it turns out that 
the fix does not resolve the problem completely. This PR just remove the 
single file from testing so currently dub does not run unit tests in the 
single file package at all. The first variant 
(https://github.com/dlang/dub/pull/2050) fixes the issue indeed. I need 
to reevaluate these PRs and close the issue. I'll do it later.


Re: binary search

2020-12-06 Thread drug via Digitalmars-d-learn

Phobos provides this by SortedRange:
https://dlang.org/phobos/std_range.html#.SortedRange

Example of usage:
https://run.dlang.io/is/WW2bn0


Re: Running unit tests from DUB single file packages

2020-12-02 Thread drug via Digitalmars-d-learn

On 12/1/20 5:18 PM, Johannes Loher wrote:

Am 01.12.20 um 14:55 schrieb drug:

On 12/1/20 2:40 PM, Johannes Loher wrote:

...
However, I am having trouble running the unit tests when using the
...


This can be one of solution https://github.com/dlang/dub/pull/2050



Thanks! Let's see if it gets merged or if a slightly more involved
solution is needed.



Remake it - https://github.com/dlang/dub/pull/2052
This has more chances to be merged


Re: Running unit tests from DUB single file packages

2020-12-01 Thread drug via Digitalmars-d-learn

On 12/1/20 2:40 PM, Johannes Loher wrote:

...
However, I am having trouble running the unit tests when using the
...


This can be one of solution https://github.com/dlang/dub/pull/2050



Re: Druntime undefined references

2020-11-02 Thread drug via Digitalmars-d-learn

On 11/2/20 1:50 PM, Severin Teona wrote:

Hi guys!

I build the druntime for an ARM Cortex-M based microcontroller and I 
trying to create an application and link it with the druntime. I am also 
using TockOS[1], which does not implement POSIX thread calls and other 
OS-dependent implementations. As I was looking through the errors I got, 
I found some functions and I don’t know what they do, or how should I 
solve the errors.


The first one is:
libdruntime-ldc.a(dwarfeh.o): in function `_d_eh_personality_common': 
dwarfeh.d:(.text._d_eh_personality_common[_d_eh_personality_common]+0x2c): 
undefined reference to `_d_eh_GetIPInfo'


and the second one is:
dl.d:(.text._D4core8internal3elf2dl12SharedObject14thisExecutableFNbNiZSQCgQCeQByQBxQBx[_D4core8internal3elf2dl12SharedObject14thisExecutableFNbNiZSQCgQCeQByQBxQBx]+0x1a): 
undefined reference to `dl_iterate_phdr'
(the druntime was build as a static library, because I can’t use dynamic 
libraries on a microcontroller)


Does anyone know what these exactly do and how important/essential are 
they? Is there any way I could solve them?


Thank a lot.

[1]: https://www.tockos.org



cant' help much but just in case - see 
https://github.com/ldc-developers/ldc/pull/2405#issuecomment-346187456, 
may be it helps


also you can define this symbol yourself with assert(0) to check if it 
is called at all


what about  - quick glance at 
https://linux.die.net/man/3/dl_iterate_phdr says us that this symbol is 
used only with shared objects so you can try to define it yourself with 
assert(0) like this (not tested!!!):

```D
extern(C):
alias Callback = int function (void*, size_t, void*);
int dl_iterate_phdr(Callback callback, void* data)
{
assert(0);
}
```
this lets you to link druntime at least and if those symbols were called 
abort the execution


Re: Skipping or Stepping Through an Array?

2020-10-21 Thread drug via Digitalmars-d-learn

There are two other way:
```D
import std;

void main()
{
int[] a = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ];

// using foreach
foreach (i; 0..a.length)
write(a[i], ", ");
writeln;

// using stride
writeln(stride(a, 2));
}
```


Re: GC-less string formatting

2020-10-20 Thread drug via Digitalmars-d-learn

On 10/20/20 9:42 AM, Per Nordlöw wrote:
If I want GC-free string formatting and outputting to stdout/stderr what 
packages should I prefer at code.dlang.org?


Probably https://code.dlang.org/packages/nogc


Re: question on dub and postgresql

2020-10-05 Thread drug via Digitalmars-d-learn

On 10/5/20 12:05 PM, Alaindevos wrote:

On Monday, 5 October 2020 at 08:54:39 UTC, Daniel Kozak wrote:
On Mon, Oct 5, 2020 at 10:25 AM Alaindevos via Digitalmars-d-learn < 
digitalmars-d-learn@puremagic.com> wrote:



Can I say python has pip, ruby has bundle and D has dub.
Meaning they perform the same function ?
Or am I wrong?



Yes and no. Dub is Dlang dependency solution but it is not installer 
as pip is





As I use unix the parameters for include and library are a real
pain.
I have totally no idea how to connect to a postgresql database.
Where and how do I start to connect to a postgresql database ?
I write a .d program to connect.
But then I need to include the database-connection-library.d file.
Where do I place it ?
And link a database-connection. object file.
How is it installed ? There is a only a git i can clone ?
Do I need to create and edit a specific dub json config file ?
It is very unclear how to start.



Yes if you want to use any of dub packages you need to add it as a 
dependency to your dub.json (dub.sdl)


A name dependency solution is at least vage.
How do I install ,
https://github.com/denizzzka/dpq2
on unix so in the code i write the .d files of that library and after 
compilation linked to the libary shared objects. For that shared object 
must be installed. How ?


Take a look at https://code.dlang.org/packages/dpq2
This is how packages work in D
To add dpq2 dependency to your project you need to execute the command:
```
dub add dpq2
```
it adds the last version of the dpq2 package. After this you are able to 
use this package in your project just import it in your source files.


Re: help: cannot build profdump, dub error (bug): Enforcement failed

2020-09-29 Thread drug via Digitalmars-d-learn

On 9/29/20 4:38 PM, drug wrote:


It reproduces. As a workaround you can use
```
dub run profdump
```
this command works as expected, I guess it is a bug of dub


Do not execute this command in cloned `profdump` repository - it will 
fail too. It works if is called from other places, for example in home.




Re: help: cannot build profdump, dub error (bug): Enforcement failed

2020-09-29 Thread drug via Digitalmars-d-learn

On 9/29/20 3:41 PM, mw wrote:

I remember I used to able to build this package:

https://github.com/AntonMeep/profdump

but now, I cannot.


Since that package haven't changed for 2 years, maybe it's a dub bug?

System information

$ uname -a
Linux  4.15.0-117-generic #118-Ubuntu SMP Fri Sep 4 20:02:41 UTC 2020 
x86_64 x86_64 x86_64 GNU/Linux


$ /usr/bin/dub --version
DUB version 1.23.0, built on Sep 27 2020

$ /usr/bin/dmd --version
DMD64 D Compiler v2.094.0
Copyright (C) 1999-2020 by The D Language Foundation, All Rights 
Reserved written by Walter Bright


$  /usr/bin/dub build -v
Using dub registry url 'https://code.dlang.org/'
Refreshing local packages (refresh existing: true)...
Looking for local package map at /var/lib/dub/packages/local-packages.json
...
...
No valid package found in current working directory: Enforcement failed
Enforcement failed

Bug Description

Enforcement failed
How to reproduce?

```
git clone https://github.com/AntonMeep/profdump

cd profdump

dub build

```

https://github.com/dlang/dub/issues/2017



It reproduces. As a workaround you can use
```
dub run profdump
```
this command works as expected, I guess it is a bug of dub


Re: Why is dtor called for lazy parameter?

2020-09-18 Thread drug via Digitalmars-d-learn

On 9/18/20 4:30 PM, drug wrote:

Can't you put a var on the stack?
```
auto instance = create();
instance
     .do_lazy()
     .do_something();
```


Ah, I totally missed your point. Now I see you can't...


Re: Why is dtor called for lazy parameter?

2020-09-18 Thread drug via Digitalmars-d-learn

Can't you put a var on the stack?
```
auto instance = create();
instance
.do_lazy()
.do_something();
```


Re: UDA inheritance

2020-09-10 Thread drug via Digitalmars-d-learn

On 9/10/20 4:06 PM, Joseph Rushton Wakeling wrote:

Hello folks,

Is there any way to define UDAs such that they automatically inherit 
other UDA definitions?


For example, suppose I define:

     enum BaseUDA { A, B }

Is there a way to define `AnotherUDA` such that if `hasUDA!(T, 
AnotherUDA)` then it is a given that `hasUDA!(T, BaseUDA)` will also be 
true?  (And similarly for the `A`, `B` specializations?)


The use-case here is to create a UDA that defines some general 
distinction of code properties, and to allow downstream code to define 
its own more specialized cases of that distinction.


Thanks in advance for any thoughts or advice!

Thanks and best wishes,

   -- Joe


Just a thought - couldn't you use classes for this? Get an UDA and check 
if it is a descendant of the specific class.


Re: Where can I find the DEFINITION of vibe IOMode ??

2020-09-05 Thread drug via Digitalmars-d-learn

05.09.2020 23:19, Andy Balba пишет:


https://github.com/vibe-d/eventcore/blob/a027c233c2542de8372bbff25d0a4804f32a094e/source/eventcore/driver.d#L1087 



@drug: thank you..I'm now left with resolving @blocking


you are welcome!

Probably this? 
https://github.com/vibe-d/vibe-core/blob/6ceb462ab68079ab24bfc57b1168dae610099357/source/vibe/core/stream.d#L88




Re: Where can I find the DEFINITION of vibe IOMode ??

2020-09-05 Thread drug via Digitalmars-d-learn

05.09.2020 22:17, Andy Balba пишет:

Where do I fine where IOMode is defined..  as in the last line of ...

module vibe.core.stream;

import vibe.internal.traits : checkInterfaceConformance, 
validateInterfaceConformance;

import vibe.internal.interfaceproxy;
import core.time;
import std.algorithm;
import std.conv;

public import eventcore.driver : IOMode;

After endless searches, I find many references to it, but never a 
DEFINITION of it





https://github.com/vibe-d/eventcore/blob/a027c233c2542de8372bbff25d0a4804f32a094e/source/eventcore/driver.d#L1087


Re: Install multiple executables with DUB

2020-09-04 Thread drug via Digitalmars-d-learn

On 9/4/20 10:27 AM, glis-glis wrote:

On Thursday, 3 September 2020 at 14:34:48 UTC, Jacob Carlborg wrote:
Oh, multiple binaries, I missed that. You can try to add multiple 
configurations [1]. Or if you have executables depending on only one 
source file, you can use single-file packages [2].


Thanks, but this still means I would have to write an install-script 
running


`dub build --single`

on each script, right?
I looked at tsv-utils [1] which seems to be a similar use-case as mine, 
and they declare each tool as a subpackage. The main package runs a 
d-file called `dub_build.d` which compiles all subpackages. Fells like 
an overkill to me, I'll probably just stick to a makefile.



[1] 
https://github.com/eBay/tsv-utils/blob/master/docs/AboutTheCode.md#building-and-makefile 



I don't know is it suitable for your use case but I do the following:
1. add a recipe in every tool files like:
```
drug@drug: ~/utils$ cat ./tools/count.d
#!/usr/bin/env dub
/+ dub.sdl:
name"count"
targetType  "executable"
targetPath  "../bin"
targetName  "count"

dependency "abcdLibrary" version="*" path="path/to/abcdLibrary"
+/

int main()
{
...
}
```
2. place all tool files into one directory `tools`
3. build them using `dub build --single tools/count.d`
4. Now I have all binary in `bin` directory, all sources in `tools` 
directory and have no subpackages at all


Re: Problem with gfm.math.matrix (some gamedevs out there ?)

2020-09-03 Thread drug via Digitalmars-d-learn

On 9/3/20 3:36 PM, Thomas wrote:

Hi!

I am following some examples in C++ about how OpenGL is working. There 
are great tutorials out there and most of it works also with D.
For my code I am using gfm.math.matrix (Version 8.0.3) to be able to 
calculate the mouse position in a 3d world.

Now I have some problems with the projection matrix by inverting it.

My example code:
-
import std.stdio;

int main()
{

     import gfm.math.matrix;

     const int width = 800;
     const int height = 600;

     auto projectionMatrix = mat4!(float).identity();
     auto ratio = cast(float)width / cast(float)height;

     projectionMatrix = mat4!(float).perspective( 45.0f, ratio, 0.0f, 
100.0f );


     writeln("projectionMatrix: ", projectionMatrix );

     auto inversedMatrix = mat4!(float).identity();
     inversedMatrix = projectionMatrix.inverse();  // <-- why this does 
not work ?

     writeln("inversedMatrix: ", inversedMatrix );

     return 0;
}

The projection matrix will be calculated correctly with
[1.3, 0, 0, 0, 0, 1.79259, 0, 0, 0, 0, -1, -0, 0, 0, -1, 0] assuming 
that the screen size is 800x600.


But using the .inverse() function in gfm returns only a matrix with 
following values:
[-nan, -nan, -nan, -nan, -nan, -nan, -nan, -nan, -nan, -nan, -nan, -nan, 
-nan, -nan, inf, -inf]


I don't know what I am doing wrong here:
   - do I call the function wrong way ? (but there is no other way)
   - is there a bug in the function ? (I do not believe that because the 
library is battle proved)


So I looked a couple of hours for an answer, but did not find anything 
useful.


Is somebody out there who could help me out ? Maybe one developer of the 
d gfm library ?


Thank you for you time!

Thomas


1. zNear should not be equal to zero, this makes your matrix singular 
and inversion of singular matix is impossible so you get nans and infs. 
Make zNear a little bit more than zero and your code will work


2. FOV in perspective should be expressed in radians, not degrees

so this fixes your problem:
```
projectionMatrix = mat4!(float).perspective( 45.0f * 3.14 / 180., ratio, 
0.01f, 100.0f );

```


Re: Unable to open filename passed as command line argument

2020-09-03 Thread drug via Digitalmars-d-learn

On 9/3/20 1:47 PM, Curious wrote:

Given the following:

=a==
void main(string[] args)
{
     FILE* fp = fopen(args[1].ptr, "r");
     if (!fp) throw new Exception("fopen");
}

=b==
void main(string[] args)
{
     FILE* fp = fopen(args[1].dup.ptr, "r");
     if (!fp) throw new Exception("fopen");
}

Why does a fail but b succeed?


try `toStringz`:
```D
import std.string : toStringz;

void main(string[] args)
{
 FILE* fp = fopen(args[1].toStringz, "r");
 if (!fp) throw new Exception("fopen");
}
```
The reason is that args are D strings (that contains no terminating 0) 
but `fopen` gets C string (null terminated) so your `a` variant fails 
because the filename becomes wrong as there is no terminating 0. Your 
`b` variant works in fact accidentally because after duplication in new 
memory after filename 0 appears due to random reason (for example all 
that memory area zeroed by allocator).


Re: Accurately serializing and deserializing a SysTime in binary format

2020-07-21 Thread drug via Digitalmars-d-learn

On 7/21/20 2:44 PM, Ecstatic Coder wrote:


Ah thanks for telling me :)

The loaded byte array in the union type was indeed the same as the saved 
one, so I immediately thought it was crashing because of some hidden 
pointer for timezone or something which was then pointing to garbage at 
reloading, causing the crash of the ".toISOString" call.





Ah, sorry, I serialize exactly long value and use it as SysTime, for 
example:

```D
struct Foo
{
long value;

void toString(Writer)(ref Writer w) const
if (isOutputRange!(Writer, char))
{
import std.datetime: SysTime;

value.SysTime.toUTC.toISOExtString(w);
}
}
```

So it is not exactly what you said


Re: Accurately serializing and deserializing a SysTime in binary format

2020-07-21 Thread drug via Digitalmars-d-learn

On 7/20/20 10:04 PM, Ecstatic Coder wrote:
I'm currently implementing a small open source backup tool (dub), and 
therefore I need to accurately store the file modification SysTime in 
binary format, so that I can later load this SysTime from the snapshot 
file to compare it with the current file modification SysTime.


Having unfortunately not understood how to do this from the SysTime 
documentation, in despair, I've tried to directly serialize the 16 bytes 
of the SysTime value. This worked fine until I call the ".toISOString()" 
on the deserialized SysTime, which inevitably crashes the executable ;)


That is probably a bug. I serialize SysTime as long by means msgpack for 
exchanging between C++ client and D server and it works pretty nice.




Anyway, that's not really want I intended to do, as in practice a 
"ulong" already has enough  resolution for that purpose.


So sorry for my ignorance, but I would definitely need some help on how 
to :
- convert a file modification SysTime to a serializable number, for 
instance the number of hectonanoseconds since 1/1/1970 in UTC;
- convert that number back into a SysTime that I can compare to the 
modification SysTime of the same file.


Eric





Re: Storing a reference to the calling object

2020-05-23 Thread drug via Digitalmars-d-learn

23.05.2020 12:27, Tim пишет:

class Sprite{
     /// Postional components of the sprite
     int* x, y;
     SDL_Surface* image_surface;
     auto parent;

     this(const char* path, auto parent){
     writeln(*x);
     this.parent = parent;
     }

     void update(){
     // Copy loaded image to window surface
     writeln("Sprites x: ",  *x);
     SDL_Rect dstRect;
     dstRect.x = parent.x;
     dstRect.y = parent.y;
     SDL_BlitSurface(image_surface, null, g_surface, );
     }
}


You can make the Sprite class templated one:
```
class Sprite(T){
/// Postional components of the sprite
int* x, y;
SDL_Surface* image_surface;
T parent;

this(const char* path, T parent){
writeln(*x);
this.parent = parent;
}

void update(){
// Copy loaded image to window surface
writeln("Sprites x: ",  *x);
SDL_Rect dstRect;
dstRect.x = parent.x;
dstRect.y = parent.y;
SDL_BlitSurface(image_surface, null, g_surface, );
}
}

auto sprite(T)(const char* path, T parent)
{
return new Sprite!T(path, parent);
}
```
and use it like:
```
auto sprite = sprite(path, this);
```


Re: Getting FieldNameTuple including all base-classes.

2020-05-20 Thread drug via Digitalmars-d-learn

On 5/20/20 12:03 PM, realhet wrote:

On Wednesday, 20 May 2020 at 01:18:24 UTC, Paul Backus wrote:

On Tuesday, 19 May 2020 at 23:15:45 UTC, realhet wrote:
I think what you want is `std.meta.staticMap`. Something like this:

alias FieldNameTuple2(T) = staticMap!(FieldNameTuple, 
BaseClassesTuple!T);


class A { int a, a1; }
class B : A { int b; }
class C : B { int c, c1; }

alias AllClasses(T) = Reverse!(AliasSeq!(T, BaseClassesTuple!T[0..$-1]));
alias AllFieldNames(T) = staticMap!(FieldNameTuple, AllClasses!T);

void main(){
   static foreach(T; "ABC") mixin("AllFieldNames!"~T~".stringof.writeln;");
}

That statticMap unlike the normal map, it also joins the AliasSeq at the 
end. Just what I needed.


Thank You!


Just in case - you can avoid mixin using:
```D
  static foreach(T; AliasSeq!(A, B, C))
  AllFieldNames!T.stringof.writeln;
```

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


Re: How to get rid of "nothrow" ?

2020-05-18 Thread drug via Digitalmars-d-learn

On 5/18/20 11:47 PM, Vinod K Chandran wrote:

On Sunday, 17 May 2020 at 19:37:05 UTC, drug wrote:

17.05.2020 17:35, Vinod K Chandran пишет:


It worked. Thanks :) I have one more question. Which is better, to 
include all the switch cases inside a single try catch or write 
separate try catch for each switch cases ?


all the switch cases inside a single try catch is better


Thanks a lot. :)


Frankly speaking in some cases separate try catch blocks will be more 
suitable but while you do not see the difference in your case then use 
the single block for everything


Re: How to get rid of "nothrow" ?

2020-05-17 Thread drug via Digitalmars-d-learn

17.05.2020 17:35, Vinod K Chandran пишет:


It worked. Thanks :) I have one more question. Which is better, to 
include all the switch cases inside a single try catch or write separate 
try catch for each switch cases ?


all the switch cases inside a single try catch is better


Re: Error running concurrent process and storing results in array

2020-05-07 Thread drug via Digitalmars-d-learn

07.05.2020 17:49, data pulverizer пишет:

On Thursday, 7 May 2020 at 02:06:32 UTC, data pulverizer wrote:

On Wednesday, 6 May 2020 at 10:23:17 UTC, data pulverizer wrote:

D:  ~ 1.5 seconds


This is going to sound absurd but can we do even better? If none of 
the optimizations we have so far is using simd maybe we can get even 
better performance by using it. I think I need to go and read a simd 
primer.


After running the Julia code by the Julia community they made some 
changes (using views rather than passing copies of the array) and their 
time has come down to ~ 2.5 seconds. The plot thickens.


That's a good sign because I was afraid that super short D time was a 
result of wrong benchmark (too good to be truth). I'm glad D time really 
is both great and real. Your blog post definitely will be very interesting.


Re: Error running concurrent process and storing results in array

2020-05-06 Thread drug via Digitalmars-d-learn

06.05.2020 13:23, data pulverizer пишет:

On Wednesday, 6 May 2020 at 08:28:41 UTC, drug wrote:

What is current D time? ...


Current Times:

D:  ~ 1.5 seconds
Chapel: ~ 9 seconds
Julia:  ~ 35 seconds



Oh, I'm impressed. I thought that D time has been decreased by 1.5 
seconds but it is 1.5 seconds!



That would be really nice if you make the resume of your research.


Yes, I'll do a blog or something on GitHub and link it.

Thanks for all your help.


You're welcome! Helping others helps me too.


Re: Error running concurrent process and storing results in array

2020-05-06 Thread drug via Digitalmars-d-learn

06.05.2020 16:57, Steven Schveighoffer пишет:

```
foreach(i; 0..n) // instead of for(long i = 0; i < n;)
```
I guess that `proc` delegate cant capture `i` var of `foreach` loop so 
the range violation doesn't happen.


foreach over a range of integers is lowered to an equivalent for loop, 
so that was not the problem.


I was surprised but `foreach` version do not have range violation, so 
there is difference between `foreach` and `for` loops. I did not try 
DerivedThread at all, only suggested them to avoid var capture. I just 
changed `for` by `foreach` and range violation gone. Probably this is 
implementation details.




Re: How to port C++ std::is_reference to D ?

2020-05-06 Thread drug via Digitalmars-d-learn

06.05.2020 12:07, wjoe пишет:

Hello,

I'm choking on a piece of C++ I have no idea about how to translate to D.

   template      typename std::enable_if< std::is_const::value == true, 
void>::type* = nullptr>

     constexpr const char *modifier() const {
     return "[in] ";
     }

   template      typename std::enable_if< std::is_reference::value == true, 
void>::type* = nullptr>

     constexpr const char *modifier() const {
     return "[out] ";
     }

my attempt at it is like this:

   template modifier(T) {

   static if (is (T==const)) {

   const char* modifier = "[in] ";

   } else static if (/* T is a reference ?*/) { // [*]

   const char* modifier = "[out] ";
   }
   }

but even if I could e.g. say something like
   is(T == ref R, R),
   auto a = modifier!(ref T);
wouldn't work.





did you try https://dlang.org/spec/traits.html#isRef?


Re: Error running concurrent process and storing results in array

2020-05-06 Thread drug via Digitalmars-d-learn

06.05.2020 11:18, data pulverizer пишет:


CPU usage now revs up almost has time to touch 100% before the process 
is finished! Interestingly using `--boundscheck=off` without 
`--ffast-math` gives a timing of around 4 seconds and, whereas using 
`--ffast-math` without `--boundscheck=off` made no difference, having 
both gives us the 1.5 seconds. As Jacob Carlborg suggested I tried 
adding `-mcpu=native -flto=full 
-defaultlib=phobos2-ldc-lto,druntime-ldc-lto` but I didn't see any 
difference.


Current Julia time is still around 35 seconds even when using @inbounds 
@simd, and running julia -O3 --check-bounds=no but I'll probably need to 
run the code by the Julia community to see whether it can be further 
optimized but it's pretty interesting to see D so far in front. 
Interesting when I attempt to switch off the garbage collector in Julia, 
the process gets killed because my computer runs out of memory (I have 
over 26 GB of memory free) whereas in D the memory I'm using barely 
registers (max 300MB) - it uses even less than Chapel (max 500MB) - 
which doesn't use much at all. It's exactly the same computation, D and 
Julia's timing were similar before the GC optimization and compiler flag 
magic in D.


What is current D time? That would be really nice if you make the resume 
of your research.


Re: Error running concurrent process and storing results in array

2020-05-06 Thread drug via Digitalmars-d-learn

06.05.2020 10:42, data pulverizer пишет:

On Wednesday, 6 May 2020 at 07:27:19 UTC, data pulverizer wrote:

On Wednesday, 6 May 2020 at 06:54:07 UTC, drug wrote:
Thing are really interesting. So there is a space to improve 
performance in 2.5 times :-)
Yes, `array` is smart enough and if you call it on another array it 
is no op.
What means `--fast` in Chapel? Do you try `--fast-math` in ldc? Don't 
know if 05 use this flag


I tried `--fast-math` in ldc but it didn't make any difference the 
documentation of `--fast` in Chapel says "Disable checks; 
optimize/specialize".


Just tried removing the boundscheck and got 1.5 seconds in D!



Congrats! it looks like the thriller!
What about cpu usage? the same 40%?


Re: Error running concurrent process and storing results in array

2020-05-06 Thread drug via Digitalmars-d-learn

06.05.2020 09:43, data pulverizer пишет:

On Wednesday, 6 May 2020 at 05:50:23 UTC, drug wrote:
General advice - try to avoid using `array` and `new` in hot code. 
Memory allocating is slow in general, except if you use carefully 
crafted custom memory allocators. And that can easily be the reason of 
40% cpu usage because the cores are waiting for the memory subsystem.


I changed the Matrix object from class to struct and timing went from 
about 19 seconds with ldc2 and flags `-O5` to 13.69 seconds, but CPU 
usage is still at ~ 40% still using `taskPool.parallel(iota(n))`. The 
`.array` method is my method for the Matrix object just returning 
internal data array object so it shouldn't copy. Julia is now at about 
34 seconds (D was at about 30 seconds while just using dmd with no 
optimizations), to make things more interesting I also did an 
implementation in Chapel which is now at around 9 seconds with `--fast` 
flag.


Thing are really interesting. So there is a space to improve performance 
in 2.5 times :-)
Yes, `array` is smart enough and if you call it on another array it is 
no op.
What means `--fast` in Chapel? Do you try `--fast-math` in ldc? Don't 
know if 05 use this flag


Re: Error running concurrent process and storing results in array

2020-05-06 Thread drug via Digitalmars-d-learn

06.05.2020 09:24, data pulverizer пишет:

On Wednesday, 6 May 2020 at 05:44:47 UTC, drug wrote:


proc is already a delegate, so  is a pointer to the delegate, 
just pass a `proc` itself


Thanks done that but getting a range violation on z which was not there 
before.


```
core.exception.RangeError@onlineapp.d(3): Range violation

??:? _d_arrayboundsp [0x55de2d83a6b5]
onlineapp.d:3 void onlineapp.process(double, double, long, 
shared(double[])) [0x55de2d8234fd]

onlineapp.d:16 void onlineapp.main().__lambda1() [0x55de2d823658]
??:? void core.thread.osthread.Thread.run() [0x55de2d83bdf9]
??:? thread_entryPoint [0x55de2d85303d]
??:? [0x7fc1d6088668]
```



confirmed. I think that's because `proc` delegates captures `i` variable 
of `for` loop. I managed to get rid of range violation by using `foreach`:

```
foreach(i; 0..n) // instead of for(long i = 0; i < n;)
```
I guess that `proc` delegate cant capture `i` var of `foreach` loop so 
the range violation doesn't happen.


you use `proc` delegate to pass arguments to `process` function. I would 
recommend for this purpose to derive a class from class Thread. Then you 
can pass the arguments in ctor of the derived class like:

```
foreach(long i; 0..n)
new DerivedThread(double)(i), cast(double)(i + 1), i, z).start(); 
thread_joinAll();

```

not tested example of derived thread
```
class DerivedThread
{
this(double x, double y, long i, shared(double[]) z)
{
this.x = x;
this.y = y;
this.i = i;
this.z = z;
super();
}
private:
void run()
{
 process(x, y, i, z);
}
double x, y;
long i;
shared(double[]) z;
}
```



Re: Error running concurrent process and storing results in array

2020-05-05 Thread drug via Digitalmars-d-learn

06.05.2020 07:52, data pulverizer пишет:

On Wednesday, 6 May 2020 at 04:04:14 UTC, Mathias LANG wrote:

On Wednesday, 6 May 2020 at 03:41:11 UTC, data pulverizer wrote:
Yes, that's exactly what I want the actual computation I'm running is 
much more expensive and much larger. It shouldn't matter if I have 
like 100_000_000 threads should it? The threads should just be queued 
until the cpu works on it?


It does matter quite a bit. Each thread has its own resources 
allocated to it, and some part of the language will need to interact 
with *all* threads, e.g. the GC.
In general, if you want to parallelize something, you should aim to 
have as many threads as you have cores. Having 100M threads will mean 
you have to do a lot of context switches. You might want to look up 
the difference between tasks and threads.


Sorry, I meant 10_000 not 100_000_000 I square the number by mistake 
because I'm calculating a 10_000 x 10_000 matrix it's only 10_000 tasks, 
so 1 task does 10_000 calculations. The actual bit of code I'm 
parallelising is here:


```
auto calculateKernelMatrix(T)(AbstractKernel!(T) K, Matrix!(T) data)
{
   long n = data.ncol;
   auto mat = new Matrix!(T)(n, n);

   foreach(j; taskPool.parallel(iota(n)))
   {
     auto arrj = data.refColumnSelect(j).array;
     for(long i = j; i < n; ++i)
     {
   mat[i, j] = K.kernel(data.refColumnSelect(i).array, arrj);
   mat[j, i] = mat[i, j];
     }
   }
   return mat;
}
```

At the moment this code is running a little bit faster than threaded 
simd optimised Julia code, but as I said in an earlier reply to Ali when 
I look at my system monitor, I can see that all the D threads are active 
and running at ~ 40% usage, meaning that they are mostly doing nothing. 
The Julia code runs all threads at 100% and is still a tiny bit slower 
so my (maybe incorrect?) assumption is that I could get more performance 
from D. The method `refColumnSelect(j).array` is (trying to) reference a 
column from a matrix (1D array with computed index referencing) which I 
select from the matrix using:


```
return new Matrix!(T)(data[startIndex..(startIndex + nrow)], [nrow, 1]);
```

If I use the above code, I am I wrong in assuming that the sliced data 
(T[]) is referenced rather than copied? That so if I do:


```
auto myData = data[5...10];
```

myData is referencing elements [5..10] of data and not creating a new 
array with elements data[5..10] copied?


General advice - try to avoid using `array` and `new` in hot code. 
Memory allocating is slow in general, except if you use carefully 
crafted custom memory allocators. And that can easily be the reason of 
40% cpu usage because the cores are waiting for the memory subsystem.


Re: Error running concurrent process and storing results in array

2020-05-05 Thread drug via Digitalmars-d-learn

06.05.2020 07:25, data pulverizer пишет:

On Wednesday, 6 May 2020 at 03:56:04 UTC, Ali Çehreli wrote:
On 5/5/20 8:41 PM, data pulverizer wrote:> On Wednesday, 6 May 2020 at 
03:33:12 UTC, Mathias LANG wrote:

>> On Wednesday, 6 May 2020 at 03:25:41 UTC, data pulverizer
wrote:

> Is there something I need to do to wait for each thread to
finish
> computation?

thread_joinAll(). I have an example here:

http://ddili.org/ders/d.en/concurrency.html#ix_concurrency.thread_joinAll


This worked nicely thank you very much

... I want to point out that there is also std.parallelism, which may 
be better suited in many cases.


I actually started off using std.parallelism and it worked well but the 
CPU usage on all the threads was less than half on my system monitor 
meaning there is more performance to be wrung out of my computer, which 
is why I am now looking into spawn. When you suggested using 
thread_joinAll() I saw that is in `core.thread.osthread` module. It 
might be shaving the yak this point but I have tried using `Thread` 
instead of `spawn`:


```
void process(double x, double y, long i, shared(double[]) z)
{
   z[i] = x*y;
}

void main()
{
   import core.thread.osthread;
   import std.stdio: writeln;

   long n = 100;
   shared(double[]) z = new double[n];
   for(long i = 0; i < n; ++i)
   {
     auto proc = (){
   process(cast(double)(i), cast(double)(i + 1), i, z);
   return;
     };


proc is already a delegate, so  is a pointer to the delegate, just 
pass a `proc` itself



     new Thread().start();
   }
   thread_joinAll();
   writeln("z: ", z);
}
```
and I am getting the following error:

```
onlineapp.d(20): Error: none of the overloads of this are callable using 
argument types (void delegate() @system*), candidates are:
/dlang/dmd/linux/bin64/../../src/druntime/import/core/thread/osthread.d(646):
core.thread.osthread.Thread.this(void function() fn, ulong sz = 0LU)
/dlang/dmd/linux/bin64/../../src/druntime/import/core/thread/osthread.d(671):
core.thread.osthread.Thread.this(void delegate() dg, ulong sz = 0LU)
/dlang/dmd/linux/bin64/../../src/druntime/import/core/thread/osthread.d(1540):
core.thread.osthread.Thread.this(ulong sz = 0LU)

```







Re: Error running concurrent process and storing results in array

2020-05-05 Thread drug via Digitalmars-d-learn

06.05.2020 06:25, data pulverizer пишет:


```
onlineapp.d(14): Error: template std.concurrency.spawn cannot deduce 
function from argument types !()(void delegate(double x, double y, long 
i, shared(double[]) z) pure nothrow @nogc @safe, double, double, long, 
shared(double[])), candidates are:
/dlang/dmd/linux/bin64/../../src/phobos/std/concurrency.d(460):  
spawn(F, T...)(F fn, T args)
   with F = void delegate(double, double, long, shared(double[])) pure 
nothrow @nogc @safe,

    T = (double, double, long, shared(double[]))
   must satisfy the following constraint:
    isSpawnable!(F, T)
```



I think the problem is in `process` attributes (error message you posted 
is strange, is it the full message?)
Make your `process` function a template one to let the compiler to 
deduce its attributes. Or set them manually.


Re: sort a string

2020-05-01 Thread drug via Digitalmars-d-learn

01.05.2020 18:04, notna пишет:


hmmm, whích results in:
  Error: cannot use [] operator on expression of type dchar



try this:
```D
import std;
void main()
{
string word = "Привет";
dchar[] line3 = to!(dchar[])(word.dup) // make a copy to get a 
range of mutable char

   // and convert char to dchar
.sort  // sort it
.release;  // get the sorted range
assert(line3 == "Пвеирт");
}
```


Re: sort a string

2020-05-01 Thread drug via Digitalmars-d-learn

01.05.2020 15:29, Steven Schveighoffer пишет:


Don't do this, use to!(dchar[]) as you have above. This will create 
incorrect dchars for non-ascii text.


-Steve



Argh, as always you're right. Funny that I never did that and sadly that 
I posted wrong code. Thank you, Steven, for correction of my wrong 
posts, I appreciate it.


Re: sort a string

2020-05-01 Thread drug via Digitalmars-d-learn

01.05.2020 10:38, Chris Katko пишет:
I'm making anagrams. According to the nextPermutation() docs, I need to 
'sort by less' to get all permutations. ... Except the doc page doesn't 
mention how to do that, nor does std.algorithm.sort show how to sort a 
string. ... and the google results on the dlang forums from 2017 don't 
work.


I've tried .byCodeUnit. , .representation. I've tried sorting on the 
dchar. I've tried sorting the on string.


The closest I've gotten:

 string word = "bar";
 string line2 = toLower!(string)(word);
    dchar[] line3 = sort(line2.to!(dchar[]));

"Error: cannot implicitly convert expression sort(to(line2)) of type 
SortedRange!(dchar[], "a < b") to dchar[]"




import std;
void main()
{
string word = "bar";
dchar[] line3 = word.dup // make a copy to get a range of mutable 
elements

.map!"dchar(a)" // convert char to dchar
.array // convert range to random access range (dynamic array 
here) to enable sorting

.sort  // sort
.array; // convert SortedRange to dynamic array
assert(line3 == "abr");
}


Re: Help, what is the code mean?

2020-04-27 Thread drug via Digitalmars-d-learn

27.04.2020 18:28, data pulverizer пишет:
I'm probably not the first person to say this but. Isn't @trusted an odd 
label to give unsafe functions and open to abuse by unscrupulous 
programmers? It almost says "nothing to see, this here piece of code is 
a-ok". Shouldn't it be explicitly labelled as @unsafe?


It says "this piece of code is verified by its author manually so you 
(the compiler) can trust it is @safe"


Re: Idiomatic way to write a range that tracks how much it consumes

2020-04-26 Thread drug via Digitalmars-d-learn

27.04.2020 06:38, Jon Degenhardt пишет:


Is there a better way to write this?

--Jon


I don't know a better way, I think you enlist all possible ways - get a 
value using either `front` or special range member. I prefer the second 
variant, I don't think it is less consistent with range paradigms. 
Considering you need amount of consumed bytes only when range is empty 
the second way is more effective.


Re: GtkD - how to list 0..100K strings

2020-04-26 Thread drug via Digitalmars-d-learn

26.04.2020 11:06, mark пишет:

snipped



Sorry for offtopic, imho both Qt and Gtk are too complex in case of 
virtual list/tree view. I think the reason is they are retained mode 
gui. I gave up to use them for that and develop custom virtual tree view 
that is easy in use and currently capable to handle over 2M 
heterogeneous items. But it is in alpha state and for general use needs 
much more attention.




Re: Integration testing, unit-threaded, and threads

2020-04-23 Thread drug via Digitalmars-d-learn

23.04.2020 14:01, Russel Winder пишет:

Hi,

I need to start a process before the tests run, and terminate it after
the tests run. A module with a shared static constructor and shared
static destructor sounds like the way of doing this since the
constructor is run before main and the destructor after main. However I
am using unit-threaded, not for the threads but because it is great for
a whole load of other reasons. In fact the threads are becoming a
problem.

As far as I can tell main is terminating before all the tests are
complete which means the shared static destructor is executing before
all the tests are complete.


I would use Fixture (a class derived form TestCase) here and derive all 
tests from it. It allows you to start your process before all test in 
`setup()` Fixture member and terminate it after in `` and probably lets 
you run the tests simultaneously.



To test this hypothesis I want to run single threaded. unit-threaded
says this is possible using a command line option -s. The question is
which command line? dub test doesn't accept it and neither does the dub
command creating ut_main.d



Did you try `dub test -- -s`?


Re: Integration testing, unit-threaded, and threads

2020-04-23 Thread drug via Digitalmars-d-learn

23.04.2020 14:41, drug пишет:

terminate it after in ``


terminate it after in `shutdown()`


[dmd] Types and ScopeDsymbol

2020-04-01 Thread drug via Digitalmars-d-learn
Is it true that user defined types are resolved to ScopeDsymbol and 
basic types aren't?


Re: assert expression in release mode

2020-03-23 Thread drug via Digitalmars-d-learn

On 3/23/20 2:14 PM, Steven Schveighoffer wrote:


E.g. https://dlang.org/dmd-osx.html#switch-release

There are also other switches like -boundscheck and -check. It’s 
possible you have some library or project that is causing the behavior.


-Steve



Thank you, Steve.
In my case I guess it was my mistake because now I returned to the 
problem to fix it and I can not reproduce that behavior locally.


assert expression in release mode

2020-03-23 Thread drug via Digitalmars-d-learn
Before I lived in assurance that asserts are actual only in debug mode 
and in release mode they are nothing (except `assert(0)`). But today my 
whole life is going sour because I stumbled upon assert in release mode 
and of course that assert is failing.


This doc https://dlang.org/spec/expression.html#assert_expressions say 
nothing about debug mode vs release mode.


So the question is when and why assert expression is enabled in release 
mode (besides assert(0))


Re: What is the wrong with my C++ interfacing

2020-03-16 Thread drug via Digitalmars-d-learn

On 3/16/20 12:24 PM, Ferhat Kurtulmuş wrote:


Ok, here is a solution. I opened my lib (yielded by my auxilary cpp) 
using 7zip. There are two files containing symbol names, 1.txt and 
2.txt. I searched for names of member functions. They were not there 
because c++ compiler does not compile member functions of class 
templates because we don't actually use them. So, I simply include this 
line in my aux cpp file and recompiled it:


template class cv::Size_;

I reopened 1.txt and they are there now:

?area@?$Size_@H@cv@@QEBAHXZ
?aspectRatio@?$Size_@H@cv@@QEBANXZ

now everything works :D



Would be nice if you update wiki ;)


Re: What is the wrong with my C++ interfacing

2020-03-16 Thread drug via Digitalmars-d-learn

On 3/16/20 10:11 AM, Ferhat Kurtulmuş wrote:

On Sunday, 15 March 2020 at 22:25:27 UTC, Arine wrote:

On Sunday, 15 March 2020 at 21:27:32 UTC, Ferhat Kurtulmuş



extern(C++, cv){
extern(C++, class) struct Size_(_Tp){
    @disable this();
~this() { }

    final _Tp area() const;
    final double aspectRatio() const;
    final bool empty() const;

    _Tp width; //!< the width
    _Tp height; //!< the height
}
}

extern(C++){
    cv.Size_!int* createSizeIntWH(int w, int h);
}

void main()
{
Size_!int* sz = createSizeIntWH(200, 100);
writeln(sz.width);
}


This worked for me too. But member functions are still causing linker 
error. It seems like docs (https://dlang.org/spec/cpp_interface.html) do 
not cover those situations. Is there any other sources to read for it. 
Maybe you make a pull request to docs covering C++ interfacing tips in 
detail.


Not tested:

 extern(C++, class) struct Size_(_Tp){
 @disable this();
 ~this() { }
extern(C++): // <- IIRC linkage should be set for members separately 
from aggregate

 final _Tp area() const;
 final double aspectRatio() const;
 final bool empty() const;

 _Tp width; //!< the width
 _Tp height; //!< the height
 }


Re: What is the wrong with my C++ interfacing

2020-03-15 Thread drug via Digitalmars-d-learn

15.03.2020 23:53, Ferhat Kurtulmuş пишет:


I doubt it because in 
https://dlang.org/spec/cpp_interface.html#using_cpp_classes_from_d


cpp code:
Derived *createInstance(int i)

d code:
extern (C++){
     ...
     Derived createInstance(int i);
}



Ah, really, you use classes on D side.




Re: What is the wrong with my C++ interfacing

2020-03-15 Thread drug via Digitalmars-d-learn

15.03.2020 23:25, Ferhat Kurtulmuş пишет:

On Sunday, 15 March 2020 at 20:21:57 UTC, drug wrote:

15.03.2020 22:39, Ferhat Kurtulmuş пишет:




What is the D version of `createSizeIntWH`? In C++ it returns a 
pointer but in D version it returns an instance.


extern(C++){
     cv.Size_!int createSizeInt();
     cv.Size_!int createSizeIntWH(int w, int h);
     void deleteSizeInt(ref cv.Size_!int sz);
}



createSizeIntWH returns a pointer to instance, not an instance. Try:
cv.Size_!int* createSizeIntWH(int w, int h);


Re: What is the wrong with my C++ interfacing

2020-03-15 Thread drug via Digitalmars-d-learn

15.03.2020 22:39, Ferhat Kurtulmuş пишет:


The original C++ class
https://github.com/opencv/opencv/blob/master/modules/core/include/opencv2/core/types.hpp#L315: 


template class Size_
{
public:
     typedef _Tp value_type;

     //! default constructor
     Size_();
     Size_(_Tp _width, _Tp _height);
     Size_(const Size_& sz);
     Size_(Size_&& sz) CV_NOEXCEPT;
     Size_(const Point_<_Tp>& pt);

     Size_& operator = (const Size_& sz);
     Size_& operator = (Size_&& sz) CV_NOEXCEPT;
     //! the area (width*height)
     _Tp area() const;
     //! aspect ratio (width/height)
     double aspectRatio() const;
     //! true if empty
     bool empty() const;

     //! conversion of another data type.
     template operator Size_<_Tp2>() const;

     _Tp width; //!< the width
     _Tp height; //!< the height
};

// my auxiliary cpp code:
cv::Size_* createSizeIntWH(int _width, int _height){
     return new cv::Size_(_width, _height);
}

void deleteSizeInt(cv::Size_ *){
     delete sz;
}

// d code:
extern(C++, cv){
class Size_(_Tp){
     @disable this();

     final _Tp area() const;
     final double aspectRatio() const;
     final bool empty() const;

     _Tp width; //!< the width
     _Tp height; //!< the height
}
}

// my test code that fails:
Size_!int sz = createSizeIntWH(200, 100);
writeln(sz.width);


What is the D version of `createSizeIntWH`? In C++ it returns a pointer 
but in D version it returns an instance.




One of the problems is that sz.width is not printed as 200, but a random 
int.
Other problem is that if I try to call one of area, aspectRatio, and 
empty, it does not compile yielding a linker error:
error LNK2019: unresolved external symbol "public: int __cdecl 
cv::Size_::area(void)const " (?area@?$Size_@H@cv@@QEBAHXZ) 
referenced in function _Dmain

Somehow linker cannot locate that symbol. Is this a name mangling issue?
In the same library I could successfully interface cv::Mat which is a 
template-free definition. I suspect if D allows interfacing C++ class 
templates since docs do not cover this, but only struct templates?




Re: 2d graphic and multimedia

2020-03-12 Thread drug via Digitalmars-d-learn

On 3/12/20 7:33 AM, Noor Wachid wrote:
I usually go with SFML (C++) library to write simple visualization. Is 
there any similiar library in D?


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


betterC main() linkage

2020-03-07 Thread drug via Digitalmars-d-learn
the specification says that in betterC mode the main function should 
have extern(C) linkage. Accidentally I found that dmd does not demand it 
in contrast to ldc. Is it bug of dmd or the specification can be relaxed?


Re: use of struct vs class

2020-03-07 Thread drug via Digitalmars-d-learn

07.03.2020 15:58, Steven Schveighoffer пишет:


Hm... I'd say:

1. Don't use a pointer for the element. Just use the struct directly. 
Using a pointer is bad because it's now going to compare pointers, and 
not the element data. Not only that, but RBNodes are stored as 
heap-allocated structs, so you are wasting a lot of memory by allocating 
another heap allocated thing to get stored inside there.


You can use predicate to solve the issue like you suggest below. But I'm 
totally missed that RBNodes are heap-allocated, thanks!


2. RedBlackTree allows you to identify the relationship that you 
consider unique by providing a "less" function. Instead of instrumenting 
your Deb type, which might affect other usages, just do:


RedBlackTree!(Deb, (a, b) => a.name < b.name)
 > No need to add opCmp and opEquals (if that doesn't make sense in other
contexts).

-Steve





Re: Trying to understand a simple piece of code: dmd barray

2020-03-07 Thread drug via Digitalmars-d-learn

07.03.2020 15:05, Dibyendu Majumdar пишет:

Hi,

I am trying to understand 
https://github.com/dlang/dmd/blob/master/src/dmd/backend/barray.d.


Two questions:

1. What does this mean and why is it needed?

line 95: alias array this;


This means that `array` can be used instead of `this`
2. The struct has no property called length - but this is referenced. 
Where does this come from?

it comes from `array` being alias for `this`, see above


Thank you

Regards




Re: use of struct vs class

2020-03-07 Thread drug via Digitalmars-d-learn

07.03.2020 13:58, mark пишет:

change #1:

     if (line.empty) {
     if (deb != null && deb.valid)
     debs.insert(deb);
     else // report incomplete
     deb = null;
     continue;
     }
     if (deb == null)
     deb = new Deb;

change #2: gets rid of most errors:

     bool opEquals(const Deb* other) const @safe pure nothrow {

     int opCmp(ref const Deb* other) const {

Just changed to pointers & moved to a separate file.

So now I just have one error in line 12 of model.d:

RedBlackTree!Deb* debs; // name-ordered list of deb packages LINE 12


Performing "debug" build using 
/home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/ldc2 for x86_64.

gtk-d:gtkd 3.9.0: target for configuration "library" is up to date.
debfind ~master: building configuration "application"...
src/model.d(12,9): Error: template instance 
std.container.rbtree.RedBlackTree!(Deb) does not match template 
declaration RedBlackTree(T, alias less = "a < b", bool allowDuplicates = 
false)

   with T = Deb
   must satisfy the following constraint:
    is(typeof(binaryFun!less(T.init, T.init)))
/home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/ldc2 failed with exit code 1.



try
```
RedBlackTree!(Deb*) debs;
```


Re: use of struct vs class

2020-03-07 Thread drug via Digitalmars-d-learn

07.03.2020 13:20, mark пишет:

I have this struct (with details omitted
[ snip ]
Should Deb be a class rather than a struct?


Do you consider using pointers in AA:
```
Deb*[string] debForName;
```


Re: Safe cast

2020-03-06 Thread drug via Digitalmars-d-learn

It's too complex

On 3/6/20 3:45 PM, Виталий Фадеев wrote:

On Friday, 6 March 2020 at 12:35:29 UTC, Виталий Фадеев wrote:

Searching info for object casting with checking class type at runtime.

Like this:

class A
{
    //
}

class B
{
    int bVar;
}


unittest
{
    A a = new A();

    A x = cast( A )a; // ok
    A x = cast( B )a; // ok, but unsafe
Here x will be null. You can use `enforce(x !is null);` if you want 
exception.



    A x = safeCast( B )a; // throw exception
    A x = safeCast( A )a; // ok
}



Re: converting to/from char[]/string

2020-03-05 Thread drug via Digitalmars-d-learn

On 3/5/20 2:03 PM, mark wrote:

I want to use the Porter stemming algorithm.
There's a D implementation here: 
https://tartarus.org/martin/PorterStemmer/d.txt


The main public function's signature is:

char[] stem(char[] p, int i, int j)

But I work entirely in terms of strings (containing individual words), 
so I want to add another function with this signature:


string stem(string word)

I've tried this without success:

     public string stem(string word) {
     import std.conv: to;

     char[] chars = word.to!char[];
     int end = chars.length.to!int; >      return stem(chars, 0, 
end).to!string;
     }

Here are just a few of the errors:

src/porterstemmer.d(197,13): Error: cannot implicitly convert expression 
s.length of type ulong to int
src/porterstemmer.d(222,9): Error: cannot implicitly convert expression 
cast(ulong)this.m_j + s.length of type ulong to int
src/porterstemmer.d(259,12): Error: function 
porterstemmer.PorterStemmer.ends(char[] s) is not callable using 
argument types (string)
src/porterstemmer.d(259,12):    cannot pass argument "sses" of type 
string to parameter char[] s



Your code and errors seem to be not related.


Re: What does assigning void mean?

2020-03-05 Thread drug via Digitalmars-d-learn

On 3/5/20 10:47 AM, mark wrote:

In Adam Ruppe's D Cookbook there're these lines in a ref counting example:

RefCountedObject o = void; // What does this mean/do?
o.data = new Implementation();
o.data.refcount = 1;

I don't understand the first line; could someone explain please?



In D all vars are initialized by default. If you use assigning void then 
the var won't be initialized.


Re: Call method if declared only

2020-02-28 Thread drug via Digitalmars-d-learn

On 2/28/20 12:25 PM, Виталий Фадеев wrote:



Now I reading Template doc for up skill...



I recommend this nice tutorial 
https://github.com/PhilippeSigaud/D-templates-tutorial




Re: preset counter variable in a for loop --> 'has no effect' Error

2020-02-28 Thread drug via Digitalmars-d-learn

On 2/28/20 11:48 AM, Namal wrote:

Hello,

I don't understand why this simple code causes a compiler error..

import std.stdio;

void main(){

  int b = 0;

  for (b; b<3; b++){
    writeln(b);
  }
}

$Error: b has no effect

Same works perfectly fine in C++

#include 

int main(){
  int i = 0;

  for(i; i<3; i++)
    std::cout<

D compiler is smart enough to say that the first use of `b` in for loop 
is useless. Use either this variant:

```
import std.stdio;

void main(){

 int b = 0;

 for (; b<3; b++){
   writeln(b);
 }
}
```
or this:
```
import std.stdio;

void main(){

 for (int b; b<3; b++){
   writeln(b);
 }
}
```


is(typeof(...)) vs __traits(compiles, ...)

2020-02-21 Thread drug via Digitalmars-d-learn

Currently this code does not compiles:
```
unittest
{
class MyClass
{
T opCall(T)(T p)
{
return p;
}
}

import std.container.array : Array;

Array!MyClass arr;
}
```
but if you comment out `opCall` in MyClass this code compiles. This is 
caused by this in std.conv(4434):

```
static if (is(typeof(chunk = T(args
chunk = T(args);
```
The reason is that `is(typeof(chunk = T(args)))` returns true but does 
not compiles becase MyClass has `opCall`, compiler calls `opCall` but it 
needs `this` pointer that is unavailable. I replaced it by

```
static if (__traits(compiles, chunk = T(args)))
chunk = T(args);
```
it works but I'm not sure this good solution. The question is - 
shouldn't `typeof` return false in this case? if so then the right fix 
would be fix typeof.


Re: Getting the initial value of a class field in compile time.

2020-02-08 Thread Drug via Digitalmars-d-learn

On Saturday, 8 February 2020 at 23:37:56 UTC, realhet wrote:

Hello,

class A{
  int i = 42;
}

Is there a way to get that 42? (Input is the class A and the 
name of the field 'i')


A.i.init returns 0, so it doesn't care about the class.

classinfo.m_init can be an option, but maybe there's a nicer 
way to do this?


Thx!


Try A.init.i


Re: Deprecation message from phobos compiling a vibe.d app.

2020-01-30 Thread drug via Digitalmars-d-learn

On 1/30/20 2:50 PM, Martin Tschierschke wrote:

When building my small vibe.d app I am getting this messages twice:

/usr/include/dmd/phobos/std/range/primitives.d(174,38): Deprecation: 
alias byKeyValue this is deprecated - Iterate over .byKeyValue instead.
/usr/include/dmd/phobos/std/range/primitives.d(176,27): Deprecation: 
alias byKeyValue this is deprecated - Iterate over .byKeyValue instead.
/usr/include/dmd/phobos/std/range/primitives.d(174,38): Deprecation: 
alias byKeyValue this is deprecated - Iterate over .byKeyValue instead.
/usr/include/dmd/phobos/std/range/primitives.d(176,27): Deprecation: 
alias byKeyValue this is deprecated - Iterate over .byKeyValue instead.


Unfortunately I don't know what is triggering this and what to do about.
The build is working, so it is more a question of elegance.


That's probably triggered by some constraints. I had the issue like this 
with Nullable: https://github.com/dlang/phobos/pull/7324


Re: Call of C function breaks memoty layout

2020-01-24 Thread drug via Digitalmars-d-learn

On 1/24/20 4:37 PM, Andrey wrote:

Hello,
I'm trying to bind C library for Tcl/Tk in D code. There is a function 
called "Tcl_CreateInterp()" which I declared as extent(C). When I call 
this function then layout of memory become broken - one of my global 
wstring variables loses it's value.
I don't know why it is happens. If I comment the call - I get normal 
execution.


May be somebody had faced with such problem. What to do?

My code:

struct Tcl_Interp;
extern (C)
{
   Tcl_Interp* Tcl_CreateInterp() nothrow;
}
I don't know if it is the reason but Tcl_Interp definitely should be 
extern(C) too in general


Re: sdl 2 - text is not displayed correctly

2020-01-15 Thread drug via Digitalmars-d-learn

On 1/15/20 6:26 PM, TodNaz wrote:

Hello!
Maybe someone came across ... I use sdl 2 derelcit, and wanted to draw 
text (not English, but Russian, for example). And when called in 
TTF_RenderUTF8_Blended, the text is drawn with incorrect characters 
(with these: ""). And with wstribg and TTF_RendererUNICODE_Blended, 
the surface has the wrong size. Maybe someone knows what to do? Thanks 
in advance!


[Sorry for the bad English, the translator helped.]


Probably the reason is wrong font. Are you sure your font contains 
Cyrillic symbols? Try the following fonts: 
https://github.com/Immediate-Mode-UI/Nuklear/tree/master/extra_font


IIRC, DroidSans.ttf contains Cyrillic symbols, probably others too


Re: Tour: unneeded import

2020-01-13 Thread drug via Digitalmars-d-learn

On 1/13/20 3:11 PM, drug wrote:

On 1/13/20 2:45 PM, mark wrote:

In the D tour example shown here
https://tour.dlang.org/tour/en/basics/ranges
there is the following import:
import std.range : drop, generate, take;
But generate is not used so I think it ought to be deleted to avoid 
confusion.




done - https://github.com/dlang-tour/english/pull/303


You can easily do it yourself.
https://github.com/dlang-tour/core/tree/master/public/content
Here you select `en`
Then go to `basics/ranges.md`
Switch to master branch using combobox in top left corner
Then press the pen icon next to Raw, Blame, History buttons.
Edit text as needed
In bottom add comment for commit and commit
Then create pull request
Profit!


Re: Tour: unneeded import

2020-01-13 Thread drug via Digitalmars-d-learn

On 1/13/20 2:45 PM, mark wrote:

In the D tour example shown here
https://tour.dlang.org/tour/en/basics/ranges
there is the following import:
import std.range : drop, generate, take;
But generate is not used so I think it ought to be deleted to avoid 
confusion.




done - https://github.com/dlang-tour/english/pull/303


Re: Algebraic and implicit conversions

2020-01-06 Thread drug via Digitalmars-d-learn

06.01.2020 22:48, Steven Schveighoffer пишет:


I think his question is, why shouldn't this work? e.g., this works:



Ah, indeed. Sorry for noise.


Re: Algebraic and implicit conversions

2020-01-06 Thread drug via Digitalmars-d-learn

06.01.2020 20:08, Per Nordlöw пишет:

alias A = Algebraic!(long, string);
     A x;
     x = 42;


That's because you try to assign int, not long. This works:

alias A = Algebraic!(long, string);
A x;
x = 42L;


Re: What kind of Editor, IDE you are using and which one do you like for D language?

2019-12-22 Thread drug via Digitalmars-d-learn

On 12/23/19 10:16 AM, Soulsbane wrote:

On Sunday, 22 December 2019 at 17:20:51 UTC, BoQsc wrote:
There are lots of editors/IDE's that support D language: 
https://wiki.dlang.org/Editors


What kind of editor/IDE are you using and which one do you like the most?


VSCode with this extension:
https://marketplace.visualstudio.com/items?itemName=LaurentTreguier.vscode-dls 



I use this extension too


Re: How to organize a project with local scripts in subdirectories using dub?

2019-12-11 Thread drug via Digitalmars-d-learn

On 12/11/19 6:52 PM, Pavel Shkadzko wrote:


It works like this, yes. However, by default it builds a "library" while 
I want to run "evaluator" as a script which uses other scripts by 
calling their functions.


Doing:
dub --compiler=ldc2 --single evaluator.d // throws an error

source\evaluator.d(93,12): Error: module runme_1 is in file 'runme_1.d' 
which cannot be read



Add to dub.json:
"targetType": "executable"

and it will build executable. Or rename evaluator.d to app.d
It is confused that there is no app.d so it thinks that you build a 
library.





Re: How to organize a project with local scripts in subdirectories using dub?

2019-12-11 Thread drug via Digitalmars-d-learn

On 12/11/19 2:18 PM, Pavel Shkadzko wrote:

I have the following project structure.

my_proj/
     script_1/runme_1.d
     script_2/runme_2.d
     evaluator.d

The project has a library dependency:

"dependencies": {
     "mir": "~>3.2.0",
},

The "evaluator.d" script contains some general functions that convert 
dataset and pass it to "runme_1.d" and "runme_2.d" scripts the following 
way:


import mir.ndslice;

void main() {
     import script_1.runme_1 : runTask1;
     import script_2.runme_2 : runTask2;

     mir_slice!(real*, 1LU, cast(mir_slice_kind)2)[]) dataset = 
convertDataset("data.txt");

     runTask1(dataset);
     //runTask2(dataset); //disable for now
}

So, basically what I need is to be able to run "./evaluator" and get the 
results by running the corresponding scripts.


I am trying to build the project the following way:

dub build --compiler=ldc2 --single evaluator.d

I get the following error:

Performing "debug" build using ldc2 for x86_64.
mir-core 1.0.2: target for configuration "library" is up to date.
mir-algorithm 3.7.2: target for configuration "default" is up to date.
mir-linux-kernel 1.0.1: target for configuration "library" is up to date.
mir-random 2.2.8: target for configuration "extended" is up to date.
survey ~master: building configuration "application"...
lld-link: error: undefined symbol: 
_D5runme_114runTask1FS3mir7ndslice5slice__T9mir_sliceTPeVmi2VEQBoQBnQBi14mir_slice_kindi2ZQBvZv 


referenced by C:\Users\user\my_proj\evaluator.d:89
  .dub\obj\evaluator.obj:(_Dmain)

Error: linking with LLD failed
ldc2 failed with exit code 1.

How should I organize my project for this kind of setup?

(I am sorry for the absence of code formatting. I could not find any 
quick howto here)





I would restructure your
folders this way:
my_proj/
source/
script_1/runme_1.d
script_2/runme_2.d
evaluator.d

then using `dub --compiler=ldc2`


Re: Confusion Concerning Numerical Equation

2019-11-16 Thread drug via Digitalmars-d-learn

16.11.2019 13:01, Avery Stupidman пишет:

Hello,

I have a function "angle" that returns the angle (in degrees) of a right 
triangle given the length of the its lengths as type "real". It works, 
but when trying to test the equality of the angle with the seemingly 
identical value, I'm informed that the two are not equal. Thus, when 
angleDeg(sqrt(3.0)/2.0, 1.5) returns 60, the comparison 
"angleDeg(sqrt(3.0)/2.0, 1.5) == 60" resolves to false.


I'm most likely stupid and sleep deprived, but if I may have some 
elucidation, I'd be very grateful.


Some code:

real angleDeg(real x, real y)
{
 import std.math : PI, abs, atan2, sgn;

 auto angle = atan2(y, x);

 // Simplify angle if vector resides in quadrants II or III
 if (x < 0)
 {
     angle = (PI - abs(angle)) * sgn(angle);
 }

 return angle * 180 / PI;
}

import std.math : sqrt;

assert(angleDeg(sqrt(3.0)/2.0, 1.5) == 60.0); // Fails
In general you shouldn't compare floating point values for equality 
because of their nature, you can use something like approxEqual 
https://dlang.org/library/std/math/approx_equal.html


P.S. see also https://floating-point-gui.de/errors/comparison/


[DTrace probe] is there a [portable] way to add section to elf executable?

2019-10-23 Thread drug via Digitalmars-d-learn
I'd like to add (and modify) section to ELF executable to implement 
DTrace probes. DTrace does it in probe assembly:

```
__asm__ __volatile__ (
"990: nop
.pushsection .note.stapsdt,\"?\",\"note\"
.balign 4
.4byte 992f-991f, 994f-993f, 3
991: .asciz \"stapsdt\"
992: .balign 4
993: .8byte 990b
.8byte _.stapsdt.base
.8byte 0
.asciz \"myapp\"
.asciz \"func_call\"
.asciz \"%n[_SDT_S1]@%[_SDT_A1] %n[_SDT_S2]@%[_SDT_A2]\"
994: .balign 4
.popsection"
:: [_SDT_S1] "n" (SNIPPED_ONE),
   [_SDT_A1] "nor" ((a)),
   [_SDT_S2] "n" (SNIPPED_TWO),
   [_SDT_A2] "nor" ((b))
);
```

So I need a way to add no op operation and define some data in 
`.note.stapsdt` section. This page https://dlang.org/spec/iasm.html does 
not contain any information about sections


Re: struct Foo may not define both a rvalue constructor and a copy constructor

2019-10-16 Thread drug via Digitalmars-d-learn

On 10/16/19 6:40 PM, rikki cattermole wrote:

On 17/10/2019 4:29 AM, drug wrote:

struct Foo
{
 this(ref const(Foo) other) {}
 this(const(Foo) other) {}
}

I'm trying to update dmd version and starting from 2.086 my code 
doesn't compile due to the error above. What is the reason of that?


Copy constructors were added (sort of like postblit).

https://dlang.org/spec/struct.html#struct-copy-constructor


Is it temporarily situation?


No.


My question is why the struct may not define both a rvalue ctor and a 
copy ctor?


struct Foo may not define both a rvalue constructor and a copy constructor

2019-10-16 Thread drug via Digitalmars-d-learn

struct Foo
{
this(ref const(Foo) other) {}
this(const(Foo) other) {}
}

I'm trying to update dmd version and starting from 2.086 my code doesn't 
compile due to the error above. What is the reason of that? Is it 
temporarily situation?


Re: C#'s 'is' equivalent in D

2019-10-10 Thread drug via Digitalmars-d-learn

On 10/10/19 6:47 PM, Just Dave wrote:

In C# you can do something like:


     if (obj is Person)
     {
     var person = obj as Person;
     // do stuff with person...
     }

where you can check the type of an object prior to casting. Does D have 
a similar mechanism? It's so widely useful in the C# realm that they 
even added syntactic sugar to allow:


     if (obj is Person person)
     {
     // do stuff with person...
     }

I would presume since D has reference objects there must exist some 
mechanism for this...


```D
  if (auto person = cast(Person) obj)
  {
  // do stuff with person...
  }
```


Re: Packaging and Distributing Dlang Applications with GtkD Dependency?

2019-09-28 Thread drug via Digitalmars-d-learn

28.09.2019 5:15, Hossain Adnan пишет:

On Wednesday, 25 September 2019 at 11:46:04 UTC, Ron Tarrant wrote:

Hi y'all,

I've been Googling how to do this, but coming up with nothing 
definitive. Are there any articles for how to do this for:



Linux?


For Linux there are 3 new options:

1. Appimages (https://appimage.org/): This is very similar to Window's 
msi installer. You can host the app installer binary in bintray or in 
your website.


2. Flatpaks (https://flatpak.org/): Flatpak is quickly becoming more and 
more popular as it provides higher level customization for desktop 
applications in Linux. The flatpak API is not trivial but there are 
tutorials available to use flatpak and meson. A well known Linux 
application named Tilix (https://github.com/gnunn1/tilix) has a flatpak 
repo (https://github.com/gnunn1/tilix/tree/master/experimental/flatpak).


3. Snap (https://snapcraft.io/): Backed by Cannonical, snap provides a 
really easy way to distribute applications in Linux. I personally think 
Snaps are easier to create although I haven't invested time in 
distributing an app using snap. DMD, Dub and LDC are also shipped with it.


There are tutorials for using all of those three online, but not 
specific to Dlang. But if you use the Meson build system there are 
plenty of tutorials available.



don't you consider deb and rpm as options?


  1   2   3   4   >