Re: How to get instance member value from getSymbolsByUDA
On Saturday, 26 February 2022 at 12:01:14 UTC, max haughton wrote: Getting the UDAs from inside a symbol must be done via a recursive procedure in the same manner one would identify the aforementioned symbol i.e. you have to go through the fields looking for UDAs *then* use getUDAs. This is because UDAs cannot convey information without their context, so the trait doesn't look recursively. I don't need to get access to the UDA value, I need the value of the field decorated with UDA. Finally I can get access to the member using `__traits(getMember, req, paramSymbols[0].stringof)`.
Re: How to get instance member value from getSymbolsByUDA
On Saturday, 26 February 2022 at 11:26:54 UTC, max haughton wrote: On Saturday, 26 February 2022 at 10:39:18 UTC, Remi Thebault wrote: Hi all, I'm trying to establish a REST API by using the type system (used in both client and server code). [...] https://dlang.org/phobos/std_traits.html#getUDAs How do I use `getUDAs` in this context? I have `getUDAs!(req, Param).length == 0`. I think it only works on the struct attributes, not on the fields attributes.
How to get instance member value from getSymbolsByUDA
Hi all, I'm trying to establish a REST API by using the type system (used in both client and server code). Considering the code ```d struct Request { Method method; string url; int apiLevel; } @Request(Method.GET, "/my-resource/%s", 1) struct MyResourceGet { @Param string name; // other members... } string requestUrl(ReqT)(ReqT req) if (isRequest!ReqT) { import std.format : format; import std.traits : getSymbolsByUDA; Request reqAttr = RequestAttr!ReqT; alias paramSymbols = getSymbolsByUDA!(ReqT, Param); // return format(reqAttr.url, ); } unittest { MyResourceGet req; req.name = "thisone"; assert(requestUrl(req) == "/my-resource/thisone"); } ``` In `requestUrl`, how do I actually get the value of `req.name` from `paramsSymbols` and `req`?
Re: Build your own Trie entry table
On Tuesday, 27 December 2016 at 19:19:49 UTC, Dmitry Olshansky wrote: On Monday, 26 December 2016 at 10:12:20 UTC, Remi Thebault wrote: Now I want to use this table to efficiently create a Trie in my code, the same way std.uni does, but found out that Trie constructor is private. Please file a bug report for Phobos basically stating your use case and the visibility problem. Done: https://issues.dlang.org/show_bug.cgi?id=17038 Remi
Build your own Trie entry table
Hello I want to map a dchar to its Bidi_Class. I've built an utility that reads UnicodeData.txt into an AA and builds a trie with std.uni.codepointTrie from it. I use Trie.store() to export the trie entry table into a D module. (I believe in a similar manner than phobos' unicode_tables.d) Now I want to use this table to efficiently create a Trie in my code, the same way std.uni does, but found out that Trie constructor is private. I've copy-pasted the asTrie function (and also TrieEntry struct) in my code and it works well (which I don't understand because my code still ends up calling the private ctor). Can you give indication on the workflow one should follow for this use case? Thanks Rémi
Re: Dynamic bindings to global variables
On Sunday, 2 August 2015 at 16:20:29 UTC, remi thebault wrote: On Sunday, 2 August 2015 at 15:38:34 UTC, Mike Parker wrote: You have to declare it as a pointer, then retrieve the pointer in the same way you do the function pointers, via the system loader (GetProcAddress/dlsym). and how would you make this source compatible with the static binding version? I guess it is not possible extern(C): version(Dynamic) { __gshared wl_interface *wl_display_interface; } else { extern __gshared wl_interface wl_display_interface; } // use case: some_func expects a pointer to wl_interface // the C version is using addressof operator, (as do the static D bindings) some_func(&wl_display_interface); I'll manage this through static @property-ies thanks for your help Rémi
Re: Dynamic bindings to global variables
On Sunday, 2 August 2015 at 15:38:34 UTC, Mike Parker wrote: You have to declare it as a pointer, then retrieve the pointer in the same way you do the function pointers, via the system loader (GetProcAddress/dlsym). and how would you make this source compatible with the static binding version? I guess it is not possible extern(C): version(Dynamic) { __gshared wl_interface *wl_display_interface; } else { extern __gshared wl_interface wl_display_interface; } // use case: some_func expects a pointer to wl_interface // the C version is using addressof operator, (as do the static D bindings) some_func(&wl_display_interface);
Dynamic bindings to global variables
Hello I wrote static bindings to a C library. I want to also offer a version(Dynamic) of the bindings. I follow and use derelict-util to get the address of function pointers. In the library I bind, there is also global variables. here's one example in the static bindings: extern __gshared wl_interface wl_display_interface; (wl_interface is a struct, not a pointer alias) How would one make dynamic binding of this? Is it possible to retrieve the address of the symbol and use std.conv.emplace? thank you
Re: Error: 'this' is only defined in non-static member functions, not main
On Wednesday, 29 July 2015 at 22:12:38 UTC, anonymous wrote: Slapping `static` on `get` seems to make it work: static size_t get() {return member.offsetof;} Good slap, thanks! you solved my problem I guess the compiler thinks that since `item.link` is an instance member, it needs a `this`. And then `get` would need a `this` too as it's using `item.link` via `member`. When the compiler sees that there is no `this`, it errors out. An explicit `static` forces the compiler to assume no `this`. And it would error out if you actually tried to make use of it. Naively, I'd think the compiler should be able to figure that out itself. I guess that is a dmd bug. I'll fill a report in case of.
Error: 'this' is only defined in non-static member functions, not main
Hello I have this weird error trying to achieve something simple: module list_test; // import wayland.util; template Id(alias a) { alias Id = a; } template ParentOf(alias member) { alias ParentOf = Id!(__traits(parent, member)); } template wl_container_of(alias member) { ParentOf!(member)* get(wl_list* ptr) { return cast(ParentOf!(member)*)(cast(ptrdiff_t)(ptr)-member.offsetof); } } struct wl_list { wl_list *prev; wl_list *next; } struct item { int num; wl_list link; this(int num) { this.num = num; } } int main (string[] args) { auto i1 = item(1); auto i2 = item(2); auto i3 = item(3); wl_list lst; // wl_list_init(&lst); // wl_list_insert(&lst, &i1.link); // wl_list_insert(&lst, &i2.link); // wl_list_insert(&i2.link, &i3.link); item *it = wl_container_of!(item.link).get(&i2.link); // error on this line return 0; } If I change the definition of wl_container_of to: template wl_container_of(alias member) { ParentOf!(member)* wl_container_of(wl_list* ptr) { return cast(ParentOf!(member)*)(cast(ptrdiff_t)(ptr)-member.offsetof); } } and call to: wl_container_of!(item.link)(&i2.link); I now get a different error: Error: need 'this' for 'wl_container_of' of type 'pure nothrow @nogc @system item*(wl_list* ptr)' I run dmd v2.067 on linux 64bits. Any idea? thanks Rémi
Re: Crash writing and reading back class instance address to void*
Classes are reference types. You take reference of local reference (it's address on stack). Use just cast(void*)t Thank you!
Crash writing and reading back class instance address to void*
Hi Starting to use GtkD TreeModel, I write an instance of an abstract class to TreeIter.userData. When reading back the void pointer and casting to my abstract class leads to crash when instance is used (Task is the abstract class): int fillIter(TreeIter iter, Task t) { if (!t || !iter) return 0; iter.stamp = stamp_; writeln("writing ", cast(void*)&t); iter.userData = cast(void*)&t; return 1; } Task taskFromIter(TreeIter iter) { if (!iter || iter.stamp != stamp_) return null; writeln("reading ", iter.userData); writeln(cast(Task)iter.userData); return cast(Task)iter.userData; } the code prints writing 18FC98 reading 18FC98 and crashes at the 2nd writeln call in taskFromIter function with message object.Error@(0): Access violation The instance is referenced somewhere else in the model, so it should not get garbage collected. should I check this anyway? Any idea? thanks Rémi
Re: Multidimensional slice
On Saturday, 9 August 2014 at 21:03:45 UTC, H. S. Teoh via Digitalmars-d-learn wrote: I think you need 2.066 or later to get this to work. After adding (size_t dim) to opSlice, your code compiles fine with git HEAD. Hi Thanks for the quick reply. Indeed I can get it to work with 2.066 Remi
Re: Multidimensional slice
Hello D-community Sorry to dig an old post, but I have the exact same need. I have C++ background and I started to use D a few days ago only (a pity I didn't start sooner!) My needs are mostly around numerical calculations. I have a safe and efficient matrix type in C++ that I am porting to D. Implementation is really easier, no doubt. I have coded slicing following this page http://dlang.org/operatoroverloading.html#Slice but the code doesn't compile. here is a reduced and simplified code: struct slice { uint start; uint size; } struct Example { int[] array; uint rows; uint cols; uint start; uint stride; this(int[] array, uint rows, uint cols, uint start=0, uint stride=uint.max) { this.array = array; this.rows = rows; this.cols = cols; this.start = start; this.stride = stride == uint.max ? cols : stride; } uint opDollar(uint dim)() { static assert(dim <= 1); static if (dim == 0) return rows; else return cols; } slice opSlice(uint from, uint to) { return slice(from, to-from); } int opIndex(uint r, uint c) { return array[start + r*stride + c]; } //int[] opIndex(slice rs, uint c) { //// ... //} //int[] opIndex(uint r, slice cs) { //// ... //} Example opIndex(slice rs, slice cs) { uint r = rs.size; uint c = cs.size; uint s = start + rs.start*stride + cs.start; return Example(array, r, c, s, stride); } } int main() { auto m = Example([ 11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34, 41, 42, 43, 44, 51, 52, 53, 54 ], 5, 4); assert (m[3, 2] == 43); auto m2 = m[slice(2, 3), slice(2, 2)]; // <- this is the construct I use in C++ assert (m2[1, 0] == 43); assert (m2.rows == 3 && m2.cols == 2); // m3 should refer to the same slice as m2 auto m3 = m[2..5, 2..4]; // <- compiler syntax error is here assert (m3[1, 0] == 43); assert (m3.rows == 3 && m3.cols == 2); return 0; } the compiler kicks me out with a syntax error: Error: found ',' when expecting ']' Error: semicolon expected following auto declaration, not '2' Error: found '..' when expecting ';' following statement Error: found ']' when expecting ';' following statement Have I done something wrong? Or may be has the documentation been quicker than the compiler implementation? Or a compiler bug? thanks Rémi