Re: Exit before second main with -funittest

2021-07-30 Thread Brian TIffin via Digitalmars-d-learn

On Friday, 30 July 2021 at 22:51:00 UTC, Ali Çehreli wrote:

On 7/30/21 3:08 PM, Brian Tiffin wrote:

> Don't take to C++, never have, even while watching
> cfront and Walter having the first** brain to craft a native
compiler.

[...]

> ** I'm not sure Walter was first first, or close first, or...

He wrote the first C++ compiler. (cfront was a C++-to-C 
transpiler.) I hope I am relaying correctly: Walter claims that 
without his compiler that made C++ accessible to the public, 
C++ might have remained in research organizations.


Ali


Thanks, Ali.

Yeah, early C++ was C, watched it evolve. Programming in Forth, 
C, Icon during those years.  Found, find, C++ too hot for most to 
handle, so why?


And yes, I'd agree that Walter's native C++ compiler was the big 
ticket item, in C++ evolution.


And a deeper thanks, Ali, for the book.

Just in case you ever want to try Unicon programming, or need to 
glue to some COBOL data, I put these on offer, umm, meaning free.


- Unicon Programming 
- GnuCOBOL FAQ 

Hoping those might let me return the favour for your D book. Nice 
work. ;-)


Have good.


Re: D compiler equivilent of "gcc -E" for debug/view template instantiation(generated) code?

2021-07-30 Thread dangbinghoo via Digitalmars-d-learn

On Friday, 30 July 2021 at 10:20:36 UTC, Stefan Koch wrote:

On Friday, 30 July 2021 at 09:30:13 UTC, Mike Parker wrote:

On Friday, 30 July 2021 at 08:38:24 UTC, dangbinghoo wrote:



but, where's these switch option documented? it seems it not 
appears in dmd --help or man dmd, or online document 
https://dlang.org/dmd-linux.html





That's what he meant by "hidden" switch. I don't know why it 
isn't documented, but it probably should be.


The reason why it's not documented is because we don't want 
people to rely on the format of the output.

I guess if we put that disclaimer in the docs we could ...


ok, that is reasonable. thanks!


Registering-unregistering threads

2021-07-30 Thread solidstate1991 via Digitalmars-d-learn
I'm doing some audio-related work, and one thing I need is to 
unregister from (and maybe later temporarily re-register to) the 
GC, since it would cause some issues, and it would be nice if I 
still could use the GC during disk operations, etc.


Info on it is quite scarce and a bit confusing. If I unregister 
from the RT, will that mean it'll be GC independent, or will have 
other consequences too?


Re: Exit before second main with -funittest

2021-07-30 Thread Ali Çehreli via Digitalmars-d-learn

On 7/30/21 3:08 PM, Brian Tiffin wrote:

> Don't take to C++, never have, even while watching
> cfront and Walter having the first** brain to craft a native compiler.

[...]

> ** I'm not sure Walter was first first, or close first, or...

He wrote the first C++ compiler. (cfront was a C++-to-C transpiler.) I 
hope I am relaying correctly: Walter claims that without his compiler 
that made C++ accessible to the public, C++ might have remained in 
research organizations.


Ali



Re: Exit before second main with -funittest

2021-07-30 Thread Brian Tiffin via Digitalmars-d-learn

On Friday, 30 July 2021 at 08:26:47 UTC, Bastiaan Veelo wrote:

On Friday, 30 July 2021 at 05:51:41 UTC, Brian Tiffin wrote:

[... interesting account of the D experience ...]


**Kudos team and contributors.**

Can't really suggest that many improvements to resources 
needed for learning D, as a hobbyist not on a clock, being new 
still and low enough to not know what detail interactions 
might be less approachable.  So far, very approachable.  Not 
confusing, but tantalizing caves to explore and integrate into 
knowing about potentials, super powers, and dark corners that 
may need to be accounted for.


And friendly people to pester with random brain train new here 
questions.


Have good, make well, and thanks.


I enjoyed reading all of that.

-- Bastiaan.


Welcome, and thanks.

Need to add something; typo, Getting close to 100 hundreds hours 
of reading an twiddling. Should be 100 hours of reading and 
twiddling.  ;-)


And a detail that struck me in the night, just past.

Template function definition syntax is a really sweet and easy 
invention of syntax.  Generic, native typing becomes a solved 
problem.  Feels solved.


Not an expert.  Don't take to C++, never have, even while 
watching cfront and Walter having the first** brain to craft a 
native compiler. I find the angle bracket source code of C++ hard 
to grok at speed.  D, is like a soft cascade flow.


** I'm not sure Walter was first first, or close first, or...  
Regardless, an impressive feat.  Thanks, for all the things, if 
you read this Walter.


Have good.


Re: translate C struct char array into D

2021-07-30 Thread russhy via Digitalmars-d-learn

On Friday, 30 July 2021 at 14:05:58 UTC, workman wrote:

I get want to define this struct in D:

```c
struct test1 {
struct test1 *prev;
struct test1 *next;
size_t v1;
size_t v2;
size_t v3;
char data[];
};
```

```d
struct test1 {
test1 *prev;
test1 *next;
size_t v1;
size_t v2;
size_t v3;
char* data;
};
```

when I compare the size, test1.sizeof is 48 and sizeof(struct 
test1) from C is 40.


Anyone can explain what should I do with this ?

If I use test1 as member of other struct, the total size will 
not match.



```d
struct test1 {
test1 *prev;
test1 *next;
size_t v1;
size_t v2;
size_t v3;
char[0] data;
}
```

data.ptr to access its pointer



Re: Performance issue with fiber

2021-07-30 Thread hanabi1224 via Digitalmars-d-learn

On Friday, 30 July 2021 at 14:41:06 UTC, Daniel Kozak wrote:

I have rewrite it to be same as dart version


Thanks! There're both generator version and fiber version on the 
site(if possible), the 2 versions are not really comparable to 
each other (generator solutions should be much faster). There's 
another dart implementation with Isolate 
[here](https://github.com/hanabi1224/Programming-Language-Benchmarks/blob/main/bench/algorithm/coro-prime-sieve/2.dart), it's unlisted because of very bad performance. (Isolate is the closest thing in dart to thread or fiber but it's much much more expensive to even spawn)


I'd like to list D's generator solution but please note that it's 
only comparable to the kotlin/c#/python generator solutions while 
the fiber one is still a separate issue.


Re: How to put an arbitrary string to clipboard in D?

2021-07-30 Thread rikki cattermole via Digitalmars-d-learn



On 31/07/2021 7:33 AM, tastyminerals wrote:
I made a GUI app using tkd library. I am reading the string from one of 
the app widgets and would like to put it into the clipboard. Does anyone 
have an idea how to copy a string to the clipboard in D?


copyText on a Text widget appears to do what you want.

https://github.com/nomad-software/tkd/blob/9ca40d117649bb9a9db108d8f92e92870b9dc77e/source/tkd/widget/text.d#L477

https://wiki.tcl-lang.org/page/tk_textCopy


How to put an arbitrary string to clipboard in D?

2021-07-30 Thread tastyminerals via Digitalmars-d-learn
I made a GUI app using tkd library. I am reading the string from 
one of the app widgets and would like to put it into the 
clipboard. Does anyone have an idea how to copy a string to the 
clipboard in D?


Re: translate C struct char array into D

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

On Friday, 30 July 2021 at 14:05:58 UTC, workman wrote:

I get want to define this struct in D:

```c
struct test1 {
struct test1 *prev;
struct test1 *next;
size_t v1;
size_t v2;
size_t v3;
char data[];
};
```


The easy way: put a slice there instead of a fake array and 
accept that data's contents will be allocated separately. Slices 
will be more convenient to deal with in the language anyway.


The C89 way: add a zero-length array and deal with its .ptr in 
appropriately sized allocations that you manage yourself (or get 
from a C API).


A third way: something with 
https://dlang.org/phobos/core_lifetime.html#.emplace


```d
import core.memory : pureMalloc, pureFree;
import std.conv : to;

struct Pascal {
ubyte len;
char[0] data;

static Pascal* alloc(string s) {
const len = s.length.to!ubyte;
auto res = cast(Pascal*) pureMalloc(Pascal.sizeof + len);
res.len = len;
res.data.ptr[0 .. len] = s[0 .. len];
return res;
}

char[] toString()() {
return data.ptr[0 .. len];
}
}

unittest {
auto s = Pascal.alloc("hello");
scope (exit) pureFree(s);
assert(s.toString == "hello");
assert((*s).sizeof == ubyte.sizeof);
}
```


Re: translate C struct char array into D

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

On Friday, 30 July 2021 at 15:51:12 UTC, Tejas wrote:

On Friday, 30 July 2021 at 14:40:17 UTC, Paul Backus wrote:

On Friday, 30 July 2021 at 14:05:58 UTC, workman wrote:

[...]


`char data[]` in the C struct is not a pointer, but actually a 
[C99 flexible array member][1], and does not count towards the 
struct's `sizeof`.


D does not have flexible array members, but you can simulate 
one using a struct method:


```d
struct test1 {
// member variables...

char* data() {
return cast(char*) ( + 1);
}
}
```

[1]: http://port70.net/~nsz/c/c99/n1256.html#6.7.2.1p16


By the way how is this safe? And how exactly are we even 
assigning the data to a variable/reference in this case? Can 
you please show an example demonstrating this?


In order to use a flexible array member, you have to allocate 
extra memory beyond the end of the struct, and it's entirely your 
responsibility to ensure that you do not go out-of-bounds when 
accessing that memory.


Re: translate C struct char array into D

2021-07-30 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jul 30, 2021 at 03:41:32PM +, Tejas via Digitalmars-d-learn wrote:
> On Friday, 30 July 2021 at 14:40:17 UTC, Paul Backus wrote:
[...]
> > ```d
> > struct test1 {
> > // member variables...
> > 
> > char* data() {
> > return cast(char*) ( + 1);
> > }
> > }
> > ```
> > 
> > [1]: http://port70.net/~nsz/c/c99/n1256.html#6.7.2.1p16
> 
> B-but the performance penalty of having an extra level of indirection?
> Absolutely unacceptable!!

Premature optimization.

Any optimizer worth its salt would be able to inline such a trivial
function as .data above.  Check the output of LDC -O, for example.
(Don't talk to me about dmd, if you care about performance you'd use LDC
instead.)


T

-- 
This is a tpyo.


Re: translate C struct char array into D

2021-07-30 Thread Tejas via Digitalmars-d-learn

On Friday, 30 July 2021 at 14:40:17 UTC, Paul Backus wrote:

On Friday, 30 July 2021 at 14:05:58 UTC, workman wrote:

[...]


`char data[]` in the C struct is not a pointer, but actually a 
[C99 flexible array member][1], and does not count towards the 
struct's `sizeof`.


D does not have flexible array members, but you can simulate 
one using a struct method:


```d
struct test1 {
// member variables...

char* data() {
return cast(char*) ( + 1);
}
}
```

[1]: http://port70.net/~nsz/c/c99/n1256.html#6.7.2.1p16


By the way how is this safe? And how exactly are we even 
assigning the data to a variable/reference in this case? Can you 
please show an example demonstrating this?


Re: translate C struct char array into D

2021-07-30 Thread Tejas via Digitalmars-d-learn

On Friday, 30 July 2021 at 14:40:17 UTC, Paul Backus wrote:

On Friday, 30 July 2021 at 14:05:58 UTC, workman wrote:

[...]


`char data[]` in the C struct is not a pointer, but actually a 
[C99 flexible array member][1], and does not count towards the 
struct's `sizeof`.


D does not have flexible array members, but you can simulate 
one using a struct method:


```d
struct test1 {
// member variables...

char* data() {
return cast(char*) ( + 1);
}
}
```

[1]: http://port70.net/~nsz/c/c99/n1256.html#6.7.2.1p16


B-but the performance penalty of having an extra level of 
indirection? Absolutely unacceptable!!


Re: translate C struct char array into D

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

On Friday, 30 July 2021 at 14:05:58 UTC, workman wrote:

I get want to define this struct in D:

```c
struct test1 {
struct test1 *prev;
struct test1 *next;
size_t v1;
size_t v2;
size_t v3;
char data[];
};
```

```d
struct test1 {
test1 *prev;
test1 *next;
size_t v1;
size_t v2;
size_t v3;
char* data;
};
```

when I compare the size, test1.sizeof is 48 and sizeof(struct 
test1) from C is 40.


Anyone can explain what should I do with this ?

If I use test1 as member of other struct, the total size will 
not match.


`char data[]` in the C struct is not a pointer, but actually a 
[C99 flexible array member][1], and does not count towards the 
struct's `sizeof`.


D does not have flexible array members, but you can simulate one 
using a struct method:


```d
struct test1 {
// member variables...

char* data() {
return cast(char*) ( + 1);
}
}
```

[1]: http://port70.net/~nsz/c/c99/n1256.html#6.7.2.1p16


Re: Performance issue with fiber

2021-07-30 Thread Daniel Kozak via Digitalmars-d-learn
On Wed, Jul 28, 2021 at 11:41 PM hanabi1224 via Digitalmars-d-learn <
digitalmars-d-learn@puremagic.com> wrote:

> On Wednesday, 28 July 2021 at 16:26:49 UTC, drug wrote:
> > I profiled the provided example (not `FiberScheduler`) using
> > perf. Both dmd and ldc2 gave the same result - `void
> > filterInner(int, int)` took ~90% of the run time. The time was
> > divided between:
> >   `int std.concurrency.receiveOnly!(int).receiveOnly()` - 58%
> >   `void std.concurrency.send!(int).send(std.concurrency.Tid,
> > int)` - 31%
> >
> > So most of the time is messages passing.
> >
> > Between the fibers creating took very few time. Perf output
> > contains information only of `void
> > std.concurrency.FiberScheduler.create(void delegate()).wrap()`
> > which took less than 0.5%. But I wouldn't say that I did the
> > profiling ideally so take it with a grain of salt.
>
> Very interesting findings! After making the Fiber fix, I also
> made profiling with valgrind, the result shows MessageBox related
> staff contributes to ~13.7% of total cycles, swapContex related
> staff add up to a larger percentage (My rough estimation is
>  >50%), I'd like to share the result svg but did not figure out
> how to upload here.
>

I have rewrite it to be same as dart version

import std;

void main(string[] args) {
auto n = args.length > 1 ? args[1].to!int() : 5;

auto r = new Generator!int(
{
for(auto i = 2;;i++)
yield(i);
});

for(auto i=0;i

translate C struct char array into D

2021-07-30 Thread workman via Digitalmars-d-learn

I get want to define this struct in D:

```c
struct test1 {
struct test1 *prev;
struct test1 *next;
size_t v1;
size_t v2;
size_t v3;
char data[];
};
```

```d
struct test1 {
test1 *prev;
test1 *next;
size_t v1;
size_t v2;
size_t v3;
char* data;
};
```

when I compare the size, test1.sizeof is 48 and sizeof(struct 
test1) from C is 40.


Anyone can explain what should I do with this ?

If I use test1 as member of other struct, the total size will not 
match.




Re: D compiler equivilent of "gcc -E" for debug/view template instantiation(generated) code?

2021-07-30 Thread Stefan Koch via Digitalmars-d-learn

On Friday, 30 July 2021 at 09:30:13 UTC, Mike Parker wrote:

On Friday, 30 July 2021 at 08:38:24 UTC, dangbinghoo wrote:



but, where's these switch option documented? it seems it not 
appears in dmd --help or man dmd, or online document 
https://dlang.org/dmd-linux.html





That's what he meant by "hidden" switch. I don't know why it 
isn't documented, but it probably should be.


The reason why it's not documented is because we don't want 
people to rely on the format of the output.

I guess if we put that disclaimer in the docs we could ...


Re: D compiler equivilent of "gcc -E" for debug/view template instantiation(generated) code?

2021-07-30 Thread dangbinghoo via Digitalmars-d-learn

On Friday, 30 July 2021 at 09:30:13 UTC, Mike Parker wrote:

On Friday, 30 July 2021 at 08:38:24 UTC, dangbinghoo wrote:



but, where's these switch option documented? it seems it not 
appears in dmd --help or man dmd, or online document 
https://dlang.org/dmd-linux.html





That's what he meant by "hidden" switch. I don't know why it 
isn't documented, but it probably should be.


sorry, I just not noticed "hidden", thanks for all of you!


Re: D compiler equivilent of "gcc -E" for debug/view template instantiation(generated) code?

2021-07-30 Thread Mike Parker via Digitalmars-d-learn

On Friday, 30 July 2021 at 08:38:24 UTC, dangbinghoo wrote:



but, where's these switch option documented? it seems it not 
appears in dmd --help or man dmd, or online document 
https://dlang.org/dmd-linux.html





That's what he meant by "hidden" switch. I don't know why it 
isn't documented, but it probably should be.


Re: D compiler equivilent of "gcc -E" for debug/view template instantiation(generated) code?

2021-07-30 Thread dangbinghoo via Digitalmars-d-learn

On Friday, 30 July 2021 at 06:46:18 UTC, WebFreak001 wrote:

On Friday, 30 July 2021 at 06:00:41 UTC, dangbinghoo wrote:


Not sure if this is exactly what you want but there is a hidden 
switch in dmd called `-vcg-ast` that prints out all the 
templates instantiated.




but, where's these switch option documented? it seems it not 
appears in dmd --help or man dmd, or online document 
https://dlang.org/dmd-linux.html



thanks!


dbh.


Re: Exit before second main with -funittest

2021-07-30 Thread Bastiaan Veelo via Digitalmars-d-learn

On Friday, 30 July 2021 at 05:51:41 UTC, Brian Tiffin wrote:

[... interesting account of the D experience ...]


**Kudos team and contributors.**

Can't really suggest that many improvements to resources needed 
for learning D, as a hobbyist not on a clock, being new still 
and low enough to not know what detail interactions might be 
less approachable.  So far, very approachable.  Not confusing, 
but tantalizing caves to explore and integrate into knowing 
about potentials, super powers, and dark corners that may need 
to be accounted for.


And friendly people to pester with random brain train new here 
questions.


Have good, make well, and thanks.


I enjoyed reading all of that.

-- Bastiaan.


Re: D compiler equivilent of "gcc -E" for debug/view template instantiation(generated) code?

2021-07-30 Thread dangbinghoo via Digitalmars-d-learn

On Friday, 30 July 2021 at 06:46:18 UTC, WebFreak001 wrote:

On Friday, 30 July 2021 at 06:00:41 UTC, dangbinghoo wrote:

Not sure if this is exactly what you want but there is a hidden 
switch in dmd called `-vcg-ast` that prints out all the 
templates instantiated.


On run.dlang.io you can use the "AST" button to view this 
conveniently online.


thanks a lot! this is truelly what I exactly want! it's great!



Re: D compiler equivilent of "gcc -E" for debug/view template instantiation(generated) code?

2021-07-30 Thread WebFreak001 via Digitalmars-d-learn

On Friday, 30 July 2021 at 06:00:41 UTC, dangbinghoo wrote:
hi, is there any D compiler option or other method to view the 
final template instantiation but not compiled (in asm or 
binary) code?


if there's a way, it might be very usefull for newbies like me 
to learn and understand the the Meta programming of dlang.




thanks!

dbh


Not sure if this is exactly what you want but there is a hidden 
switch in dmd called `-vcg-ast` that prints out all the templates 
instantiated.


On run.dlang.io you can use the "AST" button to view this 
conveniently online.


Re: Exit before second main with -funittest

2021-07-30 Thread Ali Çehreli via Digitalmars-d-learn

On 7/29/21 9:22 PM, Mathias LANG wrote:

> On Friday, 30 July 2021 at 03:45:21 UTC, Ali Çehreli wrote:
>> Almost all of my programs are in the following pattern:
>>
>> ```D
>> import std.stdio;
>>
>> void main(string[] args) {
>>   version (unittest) {
>> // Don't execute the main program when unit testing
>> return;
>>   }
>> }
>> ```
>
> Are you aware that this isn't necessary since v2.090.0 ?
> https://dlang.org/changelog/2.090.0.html#unittest-default

No, I wasn't aware. Thanks! Part of the reason must be because these 
programs started with 2.084. :)


Ali




D compiler equivilent of "gcc -E" for debug/view template instantiation(generated) code?

2021-07-30 Thread dangbinghoo via Digitalmars-d-learn
hi, is there any D compiler option or other method to view the 
final template instantiation but not compiled (in asm or binary) 
code?


if there's a way, it might be very usefull for newbies like me to 
learn and understand the the Meta programming of dlang.




thanks!

dbh