d

signature constraint for generic types

雨燕双飞 提交于 2019-12-12 11:46:42
问题 struct S(int a, int b) { } void fun(T)(T t) { } I want fun to work with S only. What would the signature constraint look like? I can't make fun a member of S , and with void fun(T)(T t) if(is(T : S)) { } I get Error: struct t1.S(int a,int b) is used as a type 回答1: S is not a type. It's a template for a type. S!(5, 4) is a type. It's quite possible that different instantiations of S generate completely different code, so the definition of S!(5, 4) could be completely different from S!(2, 5) .

GtkD with D lang on Fedora

风格不统一 提交于 2019-12-12 11:35:24
问题 I use Fedora20 and I install gtkd via sudo yum install gtkd How to compile gtk this code ? import gtk.MainWindow; import gtk.Label; import gtk.Main; void main(string[] args) { Main.init(args); MainWindow win = new MainWindow("Hello World"); win.setDefaultSize(200, 100); win.add(new Label("Hello World")); win.showAll(); Main.run(); } but i get this error ➜ /tmp dmd -L-lgtkd -L-ldl main.d main.d(1): Error: module MainWindow is in file 'gtk/MainWindow.d' which cannot be read import path[0] =

Mixin's names parameterization with template argument

笑着哭i 提交于 2019-12-12 11:29:02
问题 Is it possible to generate a name for a function within a mixin template? Something like this: mixin template Generator(string name) { @property void mixin(name) pure nothrow // mixin(name) is not valid :( { //some cool stuff here } } 回答1: I'm hoping somebody can come up with something cleaner, but this should do what you want: mixin template Generator(string name) { mixin("alias " ~ name ~ " = _fun;"); @property void _fun pure nothrow { //some cool stuff here } } This unfortunately injects

Why does std.algorithm.fill not accept character arrays?

試著忘記壹切 提交于 2019-12-12 10:49:01
问题 If I try to use std.algorithm.fill(Range1, Range2)(Range1 range, Range2 filler), I keep getting the error message that no template match could be found. It looks like the compiler is trying to match with fill (Range, Value) rather than the other one. auto test = new char[256]; fill(test, "abc".dup); Is it not possible to fill a character array using fill? The error test.d(13): Error: template std.algorithm.fill(Range,Value) if (isForwardRange!(Range) && is(typeof(range.front = filler))) does

How to convert associative array to key:value?

自闭症网瘾萝莉.ら 提交于 2019-12-11 10:55:39
问题 I need to convert associative array, to put them to Json, but I can't understanf how to do it. Method to!string add unnecessary slashes. int[string] name; name["Python"] = 5; Json tags = Json.emptyObject; //Json object tags["tags"] = name.to!string; writeln(tags); {"tags":"[\"Python\":1]"} I need to get: {"tags":{"Python":1}} Also I am thinking about using tuples so if there is any solution for them I would like to look at it. 回答1: to!string is the wrong approach. You don't want to convert to

Syntax to heap allocate anything?

て烟熏妆下的殇ゞ 提交于 2019-12-11 06:55:01
问题 Is there a syntax, template or function that allows me to essentially turn any value into a pointer to that value? I.e. copy it to the gc heap and return a pointer to it? "new" doesn't work for all types, std.experimental.allocator doesn't work in ctfe, and both seem to have troubles making pointers to delegates. 回答1: You can put the data in question inside a struct , then use the new keyword on that struct. T* copy_to_heap(T)(T value) { // create the struct with a value inside struct S { T

GtkD undefined reference

試著忘記壹切 提交于 2019-12-11 05:16:59
问题 My code: import gtk.MainWindow; import gtk.Main; void main(string[] args) { Main.init(args); auto win=new MainWindow("Hello World"); win.setDefaultSize(200,100); win.showAll(); Main.run(); } When I try to compile with DMD (or gdc), I get the errors: dmd ./test.d -L-L/usr/local/include/d/gtkd-2/lib test.o:(.data+0x10): undefined reference to `_D3gtk10MainWindow12__ModuleInfoZ' test.o:(.data+0x18): undefined reference to `_D3gtk4Main12__ModuleInfoZ' test.o: In function `_Dmain': ./test.d:(.text

D Inline Assembler: error with function call

这一生的挚爱 提交于 2019-12-11 04:54:51
问题 I got a very special problem. For a VM I need to copy code from the instruction functions to a ubyte array and then execute this array (the technic is similiar to the inline macro vm in gcc), basically it works like this: __gshared void * sp = null, sb = null; //stack pointer and stack base __gshared void add() //the function is just there to access the instruction code { asm{db "INSTRUCTIONCODESTART";} //this is a key to know where the instruction code starts //instruction code here (sample

C to D: struct as type and initialize?

蓝咒 提交于 2019-12-11 02:32:31
问题 I have these C macros and want to convert them to pure D (as apposed to interfacing with the original C file). #define __KS_TYPE(type_t) \ typedef struct __kstream_t { \ unsigned char *buf; \ int begin, end, is_eof; \ type_t f; \ } kstream_t; #define __KS_BASIC(type_t, __bufsize) \ static inline kstream_t *ks_init(type_t f) \ { \ kstream_t *ks = (kstream_t*)calloc(1, sizeof(kstream_t)); \ ks->f = f; \ ks->buf = (unsigned char*)malloc(__bufsize); \ return ks; \ } \ static inline void ks

D: how to extract data from archive?

試著忘記壹切 提交于 2019-12-11 02:18:04
问题 I have very simple code that should extract data from archive: import std.stdio; import std.string; import std.file; import std.algorithm; import std.zip; void main() { string ar = `D:\ftp\s2-imfset_2015\IFPET-150101.zip`; auto zip = new ZipArchive(ar.read); foreach(ArchiveMember am; zip.directory) { writeln(am.expandedData); } } (thanks for explaining about each and map difference ). But when I run it it's print [] on console. 回答1: You need to call zip.expand(am) before the expanded data is