DMD won't compile re-init of variable
Hi dlang community, I'm trying to implement a "reset" functionality which should revert all variables to the program start initial state. Example: import Graph; protected Edge[string] m_string2edge; int main() { // adding some elements // not important how it works // m_string2edge[name] = e; // resetting it m_string2edge = null; m_string2edge = new Edge[string]; // <- won't compile return 0; } How do I revert my variable to the init state? Thanks in advance, Simon
Re: DMD won't compile re-init of variable
On Thursday, 30 January 2020 at 21:18:04 UTC, MoonlightSentinel wrote: On Thursday, 30 January 2020 at 21:09:41 UTC, Simon wrote: Hi dlang community, I'm trying to implement a "reset" functionality which should revert all variables to the program start initial state. Example: import Graph; protected Edge[string] m_string2edge; int main() { // adding some elements // not important how it works // m_string2edge[name] = e; // resetting it m_string2edge = null; m_string2edge = new Edge[string]; // <- won't compile return 0; } How do I revert my variable to the init state? Thanks in advance, Simon You can use m_string2edge.clear() if you want to remove all entries from m_string2edge. See https://dlang.org/spec/hash-map.html#properties Hi MoonlightSentinel, in this case it won't compile: ..\Electrics.d(42): Error: function Electrics.clear() is not callable using argument types (Edge[string]) ..\Electrics.d(42):expected 0 argument(s), not 1
Re: DMD won't compile re-init of variable
On Friday, 31 January 2020 at 14:01:04 UTC, Minty Fresh wrote: On Thursday, 30 January 2020 at 21:36:53 UTC, Adam D. Ruppe wrote: On Thursday, 30 January 2020 at 21:09:41 UTC, Simon wrote: How do I revert my variable to the init state? null is the initial state for those. More generally, .init can be used as to get the initial state for any type. ie. m_string2edge = typeof(m_string2edge).init; Thank you, Minty Fresh, this was the solution!
Is there an alternative to "__FUNCTION__" that gives the actual function symbol and not the name as a string?
For debugging purposes, I have built a mixin that will, when declared inside a function, output code to the console that will reproduce the exact function call. So, as an example, for the following function int reproducible_function(int a, int b){ mixin(OUTPUT_REPRO_CASE!reproducible_function); return a + b; } called in the following way: reproducible_function(5, 9); the mixin OUTPUT_REPRO_CASE will output the exact same call. I can then take that code, paste it into the tests and tad - I have a repro case. The only problem I have with this is that you still need to give the function as a template argument to OUTPUT_REPRO_CASE. In other cases, I have gotten around this by giving __FUNCTION__ as the default argument to that template parameter, but that doesn't work here because the mixin expands to something that calls ParameterIdentifierTuple. ParameterIdentifierTuple does not take a string as an argument, but needs the actual symbol (or an alias) to be passed. So what I am looking for then is the equivalent to __FUNCTION__ that evaluates to the actual symbol of the function instead of its name, so it can be used as a parameter to ParameterIdentifierTuple. With this, I could then change the call to just be: int reproducible_function(int a, int b){ mixin(OUTPUT_REPRO_CASE!()); return a + b; }
Re: Is there an alternative to "__FUNCTION__" that gives the actual function symbol and not the name as a string?
On Saturday, 23 October 2021 at 18:36:27 UTC, Tim wrote: On Saturday, 23 October 2021 at 18:23:47 UTC, Simon wrote: So what I am looking for then is the equivalent to __FUNCTION__ that evaluates to the actual symbol of the function instead of its name, so it can be used as a parameter to ParameterIdentifierTuple. You could use the following: alias F = __traits(parent, {}); The lambda {} is a symbol inside the function. F will be its parent, which is the function itself. So before I had OUTPUT_REPRO_CASE defined like this: enum OUTPUT_REPRO_CASE(alias func) = "build the actual code stuff with"~fullyQualifiedName!func~" and so on"; And I tried to use your suggestion like this: enum OUTPUT_REPRO_CASE(alias func = __traits(parent, {})) = "build the actual code stuff with"~fullyQualifiedName!func~" and so on"; Which doesn't work. In that case func seems to become the parent namespace. If I substitute fullyQualifiedName!func with fullyQualifiedName!(__traits(parent, {}))) I get a circular dependecy error. How do I have to use this exactly?
Re: Is there an alternative to "__FUNCTION__" that gives the actual function symbol and not the name as a string?
On Saturday, 23 October 2021 at 19:03:41 UTC, Tim wrote: On Saturday, 23 October 2021 at 18:56:48 UTC, Simon wrote: And I tried to use your suggestion like this: enum OUTPUT_REPRO_CASE(alias func = __traits(parent, {})) = "build the actual code stuff with"~fullyQualifiedName!func~" and so on"; Which doesn't work. In that case func seems to become the parent namespace. If I substitute fullyQualifiedName!func with fullyQualifiedName!(__traits(parent, {}))) I get a circular dependecy error. How do I have to use this exactly? This seems to work: import std.traits, std.stdio; enum OUTPUT_REPRO_CASE = q{ string name = fullyQualifiedName!(__traits(parent, {})); writeln(name); }; void test() { mixin(OUTPUT_REPRO_CASE); } void main() { test(); } Thanks for putting up with me! I tried a bunch and it seems like I left out too much code, because I can't get it working 100%. I didn't even know about the q{} syntax, so I didn't think the stuff I left out would matter, but when using this approach it apparently does. I am going to show the full code here, just so that there are no more suprises. Sorry for this becoming a "please do my homework" kind of question. I tried for half an hour with no idea how to fix the errors I'm seeing. Something always fails. The full implementation of OUTPUT_REPRO_CASE looked like this: enum MAKE_FUNCTION_REPRODUCIBLE(alias func, s64 execution_count_to_log_reproduction_on = -1) = "static s64 execution_count = 0; if(context.settings.debug_.output_call_counts_for_reproducible_functions) dlog(\""~fullyQualifiedName!func~" call count: %lld\", execution_count); if(execution_count == "~execution_count_to_log_reproduction_on.stringof~"){ dlog(generate_reproduction_for_current_function_call!( "~fullyQualifiedName!func~", ["~get_string_argument_list_of_function!([ParameterIdentifierTuple!func])~"])( context.temp_allocator, "~get_argument_list_of_function!([ParameterIdentifierTuple!func])~")); trigger_breakpoint(); }"; It calls the function String generate_reproduction_for_current_function_call(alias func, string[] parameter_names, Parameter_Types...)(Allocator* allocator, Parameter_Types parameters); that actually creates the code-string. I did a lot of trial and error getting the trait-calls to expand at the right time (become part of the mixin string or outside of it) and falling on my nose about when something is a type vs a string and so on, so this became really ugly. If you have a better suggestion I'm open to it. I was testing this inside the function Dynamic_Array!(Polygon) clip_polygons(Slice!Vector2 p0, Slice!Vector2 p1, bool return_difference_instead_of_union = true, Slice!(Slice!Vector2) p0_holes = Slice!(Slice!Vector2)(), bool debug_print = false); in which the OUTPUT_REPRO_CASE expands to: static s64 execution_count = 0; if(context.settings.debug_.output_call_counts_for_reproducible_functions) dlog("vbl.clip_polygons call count: %lld", execution_count); if(execution_count == -1L){ dlog(generate_reproduction_for_current_function_call!( vbl.clip_polygons, ["p0", "p1", "return_difference_instead_of_union", "p0_holes", "debug_print"])( context.temp_allocator, p0, p1, return_difference_instead_of_union, p0_holes, debug_print)); trigger_breakpoint(); } When I try to change the whole thing to be a token string, execution_count_to_log_reproduction_on was suddenly undefined. I changed this to be an abominational mix of token string and string literal and then it sort of worked, except then the whole business of making the parameter names expand as actual tokens and not strings failed for some reason. I am super confused (this is a reccuring theme with D compile time stuff on my part :/). Can you show me how this can work with your token string solution?
Re: Is there an alternative to "__FUNCTION__" that gives the actual function symbol and not the name as a string?
On Saturday, 23 October 2021 at 20:24:32 UTC, Tim wrote: import std.traits, std.stdio; string generateLogCode(alias func)() { string code = "writeln("; code ~= "\"" ~ fullyQualifiedName!func ~ "(\""; foreach(i, param; ParameterIdentifierTuple!func) { if(i) code ~= ", \", \""; code ~= ", " ~ param; } code ~= ", \");\");"; return code; } enum OUTPUT_REPRO_CASE = q{ pragma(msg, generateLogCode!(__traits(parent, {}))()); mixin(generateLogCode!(__traits(parent, {}))()); }; void test(int a, int[] b) { mixin(OUTPUT_REPRO_CASE); } void main() { test(1, [2, 3]); } That worked! I needed to modify it a bit, since there is the "execution_count_to_log_reproduction_on"-template parameterto the enum. If I try to use that inside the token string, it just says it can't find the identifier, so I had to inject some non-token string in the middle that plays nice with the token string, where I can actually reference the parameter. For completeness, here is how the code looks like now (I am completely horrified by the result): string generate_call_to_log_reproduction(alias func)(){ string code = "dlog(generate_reproduction_for_current_function_call!("~fullyQualifiedName!func~", [get_string_argument_list_of_function!([ParameterIdentifierTuple!(__traits(parent, {}))])])(context.temp_allocator"; foreach(i, param; ParameterIdentifierTuple!func) code ~= "," ~ param; code ~= "));"; return code; } string generate_call_to_log_execution_count(alias func)(){ return "dlog(\""~fullyQualifiedName!(func)~" execution count: %lld\", execution_count);"; } enum OUTPUT_REPRO_CASE(s64 execution_count_to_log_reproduction_on = -1) = q{ static s64 execution_count = 0; if(context.settings.debug_.output_call_counts_for_reproducible_functions){ mixin(generate_call_to_log_execution_count!(__traits(parent, {}))); } } ~ "if(execution_count == "~execution_count_to_log_reproduction_on.stringof~")"~q{ { mixin(generate_call_to_log_reproduction!(__traits(parent, {}))()); trigger_breakpoint(); } execution_count++; }; and the usage just looks like this: void some_function(int a, int b){ mixin(OUTPUT_REPRO_CASE!()); return a + b; } The "mixin(OUTPUT_REPRO_CASE!());" then expands to: static s64 execution_count = 0; if(context.settings.debug_.output_call_counts_for_reproducible_functions){ mixin(generate_call_to_log_execution_count!(__traits(parent, {}))); } if(execution_count == -1L){ mixin(generate_call_to_log_reproduction!(__traits(parent, {}))()); trigger_breakpoint(); } execution_count++; Thanks for your help!!!
What is D's "__debugbreak()" equivalent?
Microsofts C++ compiler provides the __debugbreak function, which on x86 emits interrupt 3, which will cause the debugger to halt. What is the equivalent in D? I tried using raise(SIGINT) from core.stdc.signal, but that just closes the debugger (I thought that was the same, seems like I was wrong).
compiler fails with fatal error LNK1318: Unexpected PDB-error: OK (0) ""
When compiling my project using v2.098.0 or v2.098.1 on Windows 10, this error just appeared: : fatal error LNK1318: Unerwarteter PDB-Fehler: OK (0) "". Error: linker exited with status 1318 "Unerwarteter PDB-Fehler" means "Unexpected PDB-error". The next 3 attempts to compile yielded the following errors: libucrtd.lib(lseek.obj) : fatal error LNK1318: Unerwarteter PDB-Fehler: OK (0) "". Error: linker exited with status 1318 libucrtd.lib(initcon.obj) : fatal error LNK1318: Unerwarteter PDB-Fehler: OK (0) "". Error: linker exited with status 1318 libucrtd.lib(round.obj) : fatal error LNK1318: Unerwarteter PDB-Fehler: OK (0) "". Error: linker exited with status 1318 The obj file reported seems to pretty much be picked at random. In the first error message there simply was no lib/obj mentioned. This issue started appearing today, without installing any updates (to any program at all). The PC wasn't even shut down since yesterday, when everything worked fine. Restarting doesn't help, removing all obj files and doing a full rebuild also doesn't help (I have obj files built by MSVC that also get linked). I was on v2.098.0 before, then upgraded in hopes of fixing the issue. The very first compile after upgrading to v2.098.1 worked, every consecutive compile after that is broken again.
Re: compiler fails with fatal error LNK1318: Unexpected PDB-error: OK (0) ""
Forgot to mention: I'm using DMD, and the Windows 10 is 64 bit.
Re: compiler fails with fatal error LNK1318: Unexpected PDB-error: OK (0) ""
oops this went into the wrong forum! Sorry! I will repost this as a compiler issue, any moderator feel free to delete this post.
Aliasing a mixin (or alternative ways to profile a scope)
Hello, I am currently porting the frontend of my instrumenting profiler to D. It features a C++-makro that profiles the time between entering and leaving a scope (achieved with con-/destructors in C++). Since D has scopeguards, I hoped to achieve this by creating a mixin that generates the following code: measure("func1"); scope(exit) measure("func1"); Since I of course don't want to type a mixin for that everytime I want to use it, I tried to alias it: import std.meta : Alias; alias profile_scope(string name) = Alias!(mixin("measure(\"" ~ name ~ "\"); scope(exit) measure(\"" ~ name ~ "\");")); I expected this to generate the code above by typing profile_scope("func1") in my programm. However the compiler (dmd) gives me the following error: source\main.d(24): Error: template plattform.profile_scope cannot deduce function from argument types !()(string), candidates are: source\plattform.d(262):plattform.profile_scope(string name) This looks like a deduction/overload kind of error, which has me completly confused since there is no other identifier of the name "profile_scope" in my programm and the error message shows only one candidate. Is there any way I can achive generating those two statements using only something that effectively looks like a function call/C-macro with an argument?
Re: Aliasing a mixin (or alternative ways to profile a scope)
On Thursday, 7 March 2019 at 20:34:48 UTC, Johannes Loher wrote: auto profile_scope(string name) { import std.format : format; return q{import std.stdio : writeln; writeln("%1$s"); scope(exit) writeln("%1$s");}.format(name); } void main() { mixin(profile_scope("func1")); } Is there a way to achieve this while compiling with -betterC? I use a custom string struct right now, and your version needs TypeInfo, concatening using ~ needs the garbage collector. I have the feeling D is really not agreeing with the way I want to do things. If this is not possible, I will just roll with the Adam's struct solution.
Re: Aliasing a mixin (or alternative ways to profile a scope)
On Thursday, 7 March 2019 at 21:50:17 UTC, Johannes Loher wrote: ``` enum profile_scope(string name) = "import core.stdc.stdio : printf; printf(\"" ~ name ~ "\n\"); scope(exit) printf(\"" ~ name ~ "\n\");"; extern (C) void main() { mixin(profile_scope!"func1"); } ``` This uses string concatenation only at compile time and not during run time, so it does not require the garbage collector and is compatible with betterC :) Thanks, this works flawlessly. Out of interest: what is the "enum" doing there? I had the exact same behaviour in a function before, that I only called at compile-time, so why did it complain then? Can I somehow tell the compiler that a function should only be available at compile-time?
Re: Aliasing a mixin (or alternative ways to profile a scope)
On Saturday, 9 March 2019 at 09:12:13 UTC, Dennis wrote: On Friday, 8 March 2019 at 11:42:11 UTC, Simon wrote: Thanks, this works flawlessly. Out of interest: what is the "enum" doing there? I had the exact same behaviour in a function before, that I only called at compile-time, so why did it complain then? Can I somehow tell the compiler that a function should only be available at compile-time? The enum (which in D is not only for enumerations, but also for "manifest constants") ensures it is evaluated at compile-time, since things are only evaluated at compile-time if they have to. See also Adam D. Ruppe's post in this thread: https://forum.dlang.org/post/blaawtdhljjantvga...@forum.dlang.org Thanks!
Matching an array-type of a C++ function signature in D, without using a D-array-type, because the compiler crashes otherwise
Hi, I experienced some trouble with DMD today, while trying to declare an external C++ function in D, that gets linked from a C++ compiled object file. The C++ function that I want to link against is declared as follows: bool ColorEdit4(const char* label, float col[4], int flags = 0); Yielding the following signature in the .obj (using a demangler): BOOL __cdecl ImGui::ColorEdit4(char const * __ptr64,float * __ptr64 const,int) The Function is declared in D as follows: extern(C++, ImGui) bool ColorEdit4(const(char)* label, float[4] col, int flags = 0); Which unfortunately crashes the compiler, which tells me I can't use a D array-type here, and should use a pointer. If you want to know the full story, you can look into the bug-report: https://issues.dlang.org/show_bug.cgi?id=19759 So as a workaround until this is fixed, I somehow need to end up with the same mangled function signature, while using a pointer type for the second argument. I tried: extern(C++, ImGui) bool ColorEdit4(const(char)* label, const float* col, int flags = 0); which yields: BOOL __cdecl ImGui::ColorEdit4(char const * __ptr64,float * __ptr64,int) so the 2nd argument is of type "float * __ptr64", when it should be "float * __ptr64 const". Trying "const float* col" or "const(float)* col" doesn't yield the correct result either ("float const * __ptr64 const" and "float const * __ptr64"), and I didn't find any other combinations I could try. Is there any way to end up with the correct mangled function signature, using only pointer types? Another workaround would of course be to switch to C-Linkage, so the names don't get mangled at all, but the C++-object file has function overloads, so I would like to avoid that. If this isn't possible, I might just wait for the dmd update.
Re: Matching an array-type of a C++ function signature in D, without using a D-array-type, because the compiler crashes otherwise
On Saturday, 23 March 2019 at 13:04:10 UTC, kinke wrote: On Saturday, 23 March 2019 at 11:35:45 UTC, Simon wrote: Is there any way to end up with the correct mangled function signature, using only pointer types? The problem is that the C++ compiler uses head-const for the array param (`float * const`), which cannot be represented in D. What you can do is specify the mangle manually, e.g.: pragma(mangle, "?ColorEdit4@ImGui@@YA_NPEBDQEAMH@Z") extern(C++) bool ColorEdit4(const(char)* label, float* col, int flags = 0); I didn't know specifying the mangle was possible, thanks!
Why is this pure function taking a string literal not CTFE-executable?
Hi Guys! In my programm, I have a custom String-type that I want to initialize some variables of at compile time by casting a string literal to said custom String type. I thought I could achieve this straight forwardly, but after trying a bit, I could not find a (simple) working solution. I made this minimal example to show where the easy solution all fall flat: struct My_String{ long size; char* data; } My_String make_my_string(string s){ My_String my_string; my_string.data = cast(char*) s.ptr; my_string.size = s.length; return my_string; } struct Dummy{ My_String s = make_my_string("hello!"); } void main(){ Dummy dummy; } Which produces the compilation error "cannot use non-constant CTFE pointer in an initializer My_String(6L, &"hello!"[0])". I do not understand this error message. What is the non-constant CTFE pointer here. The "data"-member? If so, why does this compile: struct My_String{ long size; char* data; } struct Dummy{ My_String s = My_String("hello!".length, cast(char*) "hello!".ptr); } void main(){ Dummy dummy; } Why does the error message show an opcall to My_String with filled out members ("6L, &"hello!"[0]"), although the code only ever default-constructs a My_string variable? I am confused. And why on earth does this work: struct My_String{ long size; char* data; } My_String make_my_string(string s){ My_String my_string; my_string.data = cast(char*) s.ptr; my_string.size = s.length; return my_string; } void main(){ My_String s = make_my_string("hello!"); } Please help, I have no idea whats going on here.