d

What is the syntax for declaring a constant string[char] AA?

懵懂的女人 提交于 2019-12-14 03:46:05
问题 The following declaration: const(string[char]) AA1 = [ 'a' : "fkclopel", 'b' : "poehfftw" ]; void main(string args[]){} gives me: C:...\temp_0186F968.d(1,27): Error: non-constant expression ['a':"fkclopel", 'b':"poehfftw"] while it would work with other type kinds. 回答1: You can initialize associative array constants inside a module constructor: const /+ or immutable +/ (string [char]) AA1; static this () { AA1 = [ 'a' : "fkclopel", 'b' : "poehfftw" ]; } import std.stdio; void main () {writeln

Removing mutations for D metaprogramming/compiletime array generation

邮差的信 提交于 2019-12-14 02:38:46
问题 My plan is to write a mutation-less code in D-language so that my values are available by runtime. Someone spoke to me about loop-unrolling and compile time code generation but I have no clear idea how to do that. I have made the D-template below but it has no guarantee to be evaluated at compile-time because on the two assignment statements(mutations) . Advice would be greatly appreciated. Suggestions could be preferably in D or C++ without macros. import std.stdio; import std.string; import

Pattern-matching inside a template constraint

烈酒焚心 提交于 2019-12-13 15:05:52
问题 This question is based on Andrei's answer to my question on signature constraints. struct S(int x, int y) { void fun(T)(T t) if (is(T U == S!(a, b), int a, int b)) { } } template s(int a, int b) { enum result = S!(a,b)(); alias result s; } void main() { auto s1 = S!(1, 1)(); auto s2 = S!(2, 2)(); auto s3 = s!(3, 3); auto s4 = s!(4, 4); s1.fun(s1); // ok s1.fun(s2); // ok s1.fun(s3); // compile error s3.fun(s1); // ok s3.fun(s3); // compile error s3.fun(s4); // compile error } I don't

How can I grab single key hit in D Programming Language + Tango?

非 Y 不嫁゛ 提交于 2019-12-13 13:54:32
问题 I read this article and try to do the exercise in D Programming Language, but encounter a problem in the first exercise. (1) Display series of numbers (1,2,3,4, 5....etc) in an infinite loop. The program should quit if someone hits a specific key (Say ESCAPE key). Of course the infinite loop is not a big problem, but the rest is. How could I grab a key hit in D/Tango? In tango FAQ it says use C function kbhit() or get(), but as I know, these are not in C standard library, and does not exist

Linker Error; Cannot Link: _D16TypeInfo_HAyayAa6__initZ

随声附和 提交于 2019-12-13 00:26:47
问题 When linking my code, I get the following error: ../Build/main.o:(.data._D16TypeInfo_yHAyaAa6__initZ+0x10): undefined reference to `_D16TypeInfo_HAyayAa6__initZ' I have no idea where the error is occurring, so there is not much code wise I can offer you. There is a lot of code. 回答1: Try adding this code to your file with main(): void aaHack() { import std.stdio; writeln(typeid(immutable(char[])[string])); } and recompile, see if it works. I'm sure your error is caused by an old bug in

Error in Defining an associative array in D

雨燕双飞 提交于 2019-12-12 22:16:06
问题 So I would like to implement a hash lookup for translating codons to amino acids in D. When i write int[string] codon_table = [ "ATG": 'M', "TTT": 'F', "TTC": 'F', "TTA": 'L', "TTG": 'L', "CTT": 'L', "CTC": 'L', "CTA": 'L', "CTG": 'L', "ATT": 'I', "ATC": 'I', "ATA": 'I', "GTT": 'V', "GTC": 'V', "GTA": 'V', "GTG": 'V', "TCT": 'S', "TCC": 'S', "TCA": 'S', "TCG": 'S', "CCT": 'P', "CCC": 'P', "CCA": 'P', "CCG": 'P', "ACT": 'T', "ACC": 'T', "ACG": 'T', "GCT": 'A', "GCC": 'A', "GCA": 'A', "GCG": 'A

Need help parallel traversing a dag in D

落花浮王杯 提交于 2019-12-12 20:20:23
问题 Edit: OK, here's a much much simpler example illustrating my problem. why is only the first task ever put onto the queue? import std.stdio; import std.parallelism; void simpleWorker(uint depth, uint maxDepth, TaskPool pool){ writeln("Depth is: ",depth); if (++depth < maxDepth){ pool.put( task!simpleWorker(depth,maxDepth,pool)); } } void main(){ auto pool = new TaskPool(); pool.put(task!simpleWorker(0,5,pool)); pool.finish(true); writeln("Done"); } Original: I need to traverse this DAG. When I

Templated Multi-Dimensional Arrays

你离开我真会死。 提交于 2019-12-12 18:46:00
问题 I'm trying to experiment with templates and tried to implement templated arrays, something that can be declared like: Array!(float, 3, 2, 1) myArray; I've browsed through several implementations of this problem in C++ but I can't seem to convert it to D as I have little experience with the language (with D). Anyways these are the stuff I tried, unfortunately none of them worked: 1. Compile-Time Functions - to generate code of the format "DataType[D0][D1]...[Dn] Identifier" import std.conv;

Late static binding in d

泪湿孤枕 提交于 2019-12-12 13:50:05
问题 I'm working on a generic collection class template, let's say List(T) where i'd like to be able to do something like php's late static binding. Might be best illustrated with some simplified sample code. This code compiles fine as is on dmd, but it needs a small change to be the way I want. module main; import std.stdio; import std.string; class List(T) { private T[] _list; public void append(T t) { _list ~= t; } // this is where some help is needed... public List select(bool delegate(T t) dg

Template argument inference in D

╄→гoц情女王★ 提交于 2019-12-12 13:33:20
问题 I am writing some vector functions which operate on static D arrays like so: real[N] unit(uint N)(real[N] v) { real[N] u = (v[] / norm(v)); //explicit type necessary to force slice-operator return u; //to return static-length array } real[3] v = unit!(3)([1,2,3]); //works real[3] w = unit([1,2,3]); //error, compiler doesn't infer the template parameter real[3] x = [1,2,3]; auto i = unit(x); //also works, forces statically allocated array So, my question is, is there a way to get the compiler