d

How do you enumerate the names and types inside a struct or class at compile time in D?

空扰寡人 提交于 2019-12-07 05:48:56
问题 How do you enumerate the names and types inside a struct or class at compile time? i.e. to do the following: struct Foo { int x; int y; } string serialise!(A)(A a) { ...magic... } auto f = Foo(1,2); serialise(f); -> "x:1, y:2" Thanks, Chris. 回答1: Like this: foreach (index, field; myStruct.tupleof) { // field.stringof is "field", slice is to cut off "myStruct." pragma(msg, "Name: " ~ myStruct.tupleof[index].stringof[9..$]); pragma(msg, "Type: " ~ typeof(field).stringof); } Practical example:

How is this pure function able to modify non-private state?

余生长醉 提交于 2019-12-07 05:08:15
问题 TDPL, p. 167: as long as the mutable state in a function is entirely transitory (i.e., allocated on the stack) and private (i.e., not passed along by reference to functions that may taint it), then the function can be considered pure. import std.stdio : writeln; struct M{ int[4] _data; pure ref int opIndex(size_t i){ return _data[i]; } } pure M foo(ref M m){ m[0] = 1234; return m; } void main(){ M m1 = M([7, 7, 7, 7]); writeln(m1); foo(m1); writeln(m1); } // output: // M([7, 7, 7, 7]) // M(

How to link to D Libraries in a D program

故事扮演 提交于 2019-12-06 21:28:32
问题 I´m new to the D Programming Language and have a very simple problem. I want to compile a D Script Library once and then use it in my other D projects. In C I linked to the .lib files and created headers for them, but in D I don´t find things like that (are there even some sort of headers in D?) I use D-IDE as my IDE and DMD2 as my compiler. 回答1: there are .di (D interface) files which can be used as header these can be generated from your sources with the -H compiler switch however the

How to convert strings to floats with perfect accuracy?

Deadly 提交于 2019-12-06 20:42:55
问题 I'm trying to write a function in the D programming language to replace the calls to C's strtold. (Rationale: To use strtold from D, you have to convert D strings to C strings, which is inefficient. Also, strtold can't be executed at compile time.) I've come up with an implementation that mostly works, but I seem to lose some precision in the least significant bits. The code to the interesting part of the algorithm is below and I can see where the precision loss comes from, but I don't know

CDB command for setting a breakpoint based on a line number

送分小仙女□ 提交于 2019-12-06 20:31:36
Is there a set of CDB commands for setting a breakpoint based on a line number? It seems that there is no "direct" one. Actually it seems that CDB knowledge is falling into a black hole and it's getting harder and harder to find resources on the Internet. My CDB setup cdb -c "l+*;.lines" and later I add the source lsf mySource.d Currently I put hard-coded breakpoints with a mixin enum brk = "debug{asm{int 3;}}"; // code mixin(brk); But it's not a viable solution. The syntax to break in source line mode, is well explained here or here , e.g: bp `mod!source.c:12`; And to stop on a particular

Parallel Iterators in the D language

落花浮王杯 提交于 2019-12-06 06:59:38
I am trying to implement a graph data structure in the D language which supports parallel iteration over the node and edge sets. alias ulong index; alias index node; alias ulong count; class Graph { index z; // max node index count n; // number of nodes count m; // number of edges node[][] adja; // adjacency list count[] deg; // node degree this(count n = 0) { this.z = n; this.n = n; this.m = 0; this.adja = new node[][](this.z, 0); this.deg = new count[](this.z); } Here's a sequential node iterator method: /** * Iterate over all nodes of the graph and call handler (lambda closure). */ void

How to fix “unrecognized option '-plugin`” when using gdc to compile D program?

家住魔仙堡 提交于 2019-12-06 05:02:53
问题 I download the GDC for Linux , and try to build a simple D Program. After executing " gdc hello.d -o hello ", it outputs: [root@localhost nan]# gdc hello.d -o hello /usr/bin/ld: unrecognized option '-plugin' /usr/bin/ld: use the --help option for usage information collect2: error: ld returned 1 exit status Then I use " gdc -v hello.d -o hello " command, and try to find the root cause. It displays: ...... COLLECT_GCC_OPTIONS='-v' '-o' 'hello' '-shared-libgcc' '-mtune=generic' '-march=x86-64'

D exit statement

倖福魔咒の 提交于 2019-12-06 03:53:55
问题 Does D have an exit statement, similar to the one in java, python, c/c++. Which will (big shocker) exit execution of the program? Something like exit(); 回答1: If you want exit , then use C's exit function. import core.stdc.stdlib; void main() { exit(-1); } I'm not quite sure how that affects the D runtime and whatnot though. It might be that stuff doesn't get cleaned up like you'd normally want, or it might be just fine. I wouldn't really advise using it in general though, since there are

How to enter by-name argument to a function in D?

限于喜欢 提交于 2019-12-06 03:47:18
问题 I am struggling on passing arguments by name to a function that has default parameter values defined: import std.stdio; void main() { void foo(int x=1, int y=2, int z=3) { writefln("x=%s, y=%s, z=%s", x, y, z); } foo(10, 20, 30); // ok, prints: x=10, y=20, z=30 foo(z=30); // Error: undefined identifier 'z' } This is quite a basic need. Such a function can have 10 parameters or more and might be called multiple times with different set of arguments. It would be unbearable to list all the args

How to create a Dynamic Library in D?

自闭症网瘾萝莉.ら 提交于 2019-12-06 00:46:38
问题 I want to create a Dynamic library (cross-platform) in D, so I did some Googling. After some time I found this page. I am absolutely stunned by how much complexities there are in writing, compiling and even linking to a DLL. Isn't there a uniform way of creating a shared library like you would in C? (just leave out the main function and pass some flags to the linker) 回答1: Well, I decided to spend some time today messing with this and I kinda sorta have something that works, at least if the