Re: DMD: how to restore old unittest+main
On Thursday, 13 August 2020 at 07:52:07 UTC, novice3 wrote: Hello. I don't use dub. I use Windows and *.d file association to compile small apps by dmd with "-i -unittest -g" switches. Now i update dmd, and found, that apps compiled with "-unittest" not runs main(). How i can restore old behaviour (run all unittests then main()) *without use "--DRT-testmode=run-main" switch every time then i start compiled app.exe*? I want just press Enter on app.d file, then press Enter on app.exe. Any advises? Thanks. Is there a reason you need to run all unittests every time you want to run the program? I personally compile with -unittest to make sure all my unittests pass, then recompile without the -unittest flag if I actually want to run the program. This way, time isn't wasted running unittests every time the program is run.
Use std.traits.getSymbolsByUDA to access members of instance.
I can use `std.traits.getSymbolsByUDA` to get all the members of a class that have a particular UDA `getSymbolsByUDA(ValueType, UDA)`. But how do I get the values with it? Is there a more convenient way than `__traits(getMember, value, getSymbolsByUDA(ValueType, UDA)[0].stringof)`?
`free` for struct with C bindings.
I am using a C bindings library (https://code.dlang.org/packages/xcb-d). I am following through a tutorial that was written for the C library directly and just making the minor changes to make it work with D. I ran into a problem. The library ends up giving me a struct pointer. ``` xcb_generic_event_t*event; event = xcb_wait_for_event (connection); free (event); ``` The problem is the `free` function. It is not provided by the library but is part of the C standard library (in stdlib.h). Do I need to call this function with my D code? I tried using the `core.memory.GC.free` function from the D standard library and it compiled and ran but that does not necessarily mean there are not memory leaks (it also ran with the line entirely removed). Do I need to call the `free` function with my D code because I need to free memory that was allocated in C code?
Arguments of function as an array.
Is there a way in D to take past arguments as an array? A like a normal Variadic function. All the arguments should be of the same type just as an array. Basically I want to allow a function like this to be called without square brackets. void fun(int[] intArray) { //... } void main() { fun([5,6,4]); } Like this: void fun(int... intArray) { //typeof(intArray) is `int[]` } void main() { fun(5,6,4); } Is this doable in D?
Re: Strange Thread Causing Duplicating `writeln`
On Tuesday, 10 April 2018 at 23:59:08 UTC, Steven Schveighoffer wrote: On 4/9/18 6:56 PM, Jonathan wrote: On Monday, 9 April 2018 at 22:53:31 UTC, Jonathan wrote: On Monday, 9 April 2018 at 22:49:07 UTC, Cym13 wrote: I don't know, but I can't reproduce either with dmd or ldc. What was your compilation line? dmd -run file.d I am on Window 10 btw. It's a windows 32-bit issue (specifically, DMC's FILE *, upon which std.stdio.File is based, is thread unsafe). Try -m64. https://issues.dlang.org/show_bug.cgi?id=18483 http://bugzilla.digitalmars.com/issues/show_bug.cgi?id=327 -Steve Hum, thank you.
Re: Strange Thread Causing Duplicating `writeln`
On Monday, 9 April 2018 at 22:56:33 UTC, Jonathan wrote: On Monday, 9 April 2018 at 22:53:31 UTC, Jonathan wrote: On Monday, 9 April 2018 at 22:49:07 UTC, Cym13 wrote: I don't know, but I can't reproduce either with dmd or ldc. What was your compilation line? dmd -run file.d I am on Window 10 btw. Hum, LDC does not do it for me?
Re: Strange Thread Causing Duplicating `writeln`
On Monday, 9 April 2018 at 22:53:31 UTC, Jonathan wrote: On Monday, 9 April 2018 at 22:49:07 UTC, Cym13 wrote: I don't know, but I can't reproduce either with dmd or ldc. What was your compilation line? dmd -run file.d I am on Window 10 btw.
Re: Strange Thread Causing Duplicating `writeln`
On Monday, 9 April 2018 at 22:49:07 UTC, Cym13 wrote: I don't know, but I can't reproduce either with dmd or ldc. What was your compilation line? dmd -run file.d
Strange Thread Causing Duplicating `writeln`
I am totally lost on why this is happening. I stripped the code down to what appears to be the most minimal code that still causes the problem. --- import core.sync.mutex; import core.thread; import std.stdio; __gshared Mutex m;//__gshared just for testing (; void thread1() { foreach (i;0..8) { synchronized(m) { writeln("a1-",i); } writeln("a2-",i); } } void thread2() { foreach (i;0..8) { synchronized(m) { writeln("b1-",i); } writeln("b2-",i); } } void main() { m = new Mutex(); new Thread().start; new Thread().start; } --- The beginning of the output for this code is: a1-0 a2-0 a2-0 b1-0 b2-0 b2-0 a1-1 a2-1 a2-1 Why is the "a2" and "b2" writeln being repeated?!
Atomic vs Mutex
Everywhere I look the advice is to avoid atomic and just mutex things. Why is this `a.atomicStore(b)`(memory order is seq) less safe than `synchronized{a=b}`? I get that when more operations or shared values are used it is appropriate to mutex the entire set of operations but why would I for a single set? If the first is in fact less safe that the second then I am eager to learn more, could you recommend a book or paper on the subject? Thanks!
Re: Is socket.send thread safe?
On Monday, 26 March 2018 at 17:55:10 UTC, bauss wrote: On Monday, 26 March 2018 at 16:14:31 UTC, Jonathan wrote: Can I send data over an std.socket on multiple threads without manual mutexing? If not, can I send data on a separate thread than receive? The docs for std.socket say nothing of it (which I guess means I should assume it is not thread safe but...). Thanks! Define thread safe. It's safe in the way that the buffers each call to send will have will be what you expect. Ex. thread1 sends [1,2,3] and thread2 sends [4,5,6] then you're guaranteed that what you receive would be [1,2,3] and [4,5,6]. What it isn't safe from would be race conditions. So you don't know if you get it like [1,2,3,4,5,6] or [4,5,6,1,2,3]. So if the order of the buffer matters then you should use a mutex, but if the order doesn't matter then you don't need to. You answered what I needed. The order of receiving the messages is not a problem, merely that a message its self is not broken, ie: [4,1,2,5,3,6](This would not work!) Thanks!
Is socket.send thread safe?
Can I send data over an std.socket on multiple threads without manual mutexing? If not, can I send data on a separate thread than receive? The docs for std.socket say nothing of it (which I guess means I should assume it is not thread safe but...). Thanks!
Re: Can't add ubytes together to make a ubyte... bug or feature?
On Tuesday, 19 January 2016 at 23:36:14 UTC, Adam D. Ruppe wrote: On Tuesday, 19 January 2016 at 22:12:06 UTC, Soviet Friend wrote: I don't care if my computer needs to do math on a 4 byte basis, I'm not writing assembly. x86 actually doesn't need to do math that way, if you were writing assembly, it would just work. This is just an annoying rule brought over by C. Can I prevent the initial implicit casts? Nope, though you can help tell the compiler that you want it to fit there by doing stuff like ubyte a = 200; ubyte b = 100; ubyte c = (a+b)&0xff; or something like that, so the expression is specifically proven to fit in the byte with compile time facts. `(a+b)&0xff` What is this syntax?! Could you give a link to this in the D documentation? I am not even sure how to look it up...
Re: Cast a 2d static array to a 1d static array. T[s][r] -> T[s*r]
On Tuesday, 27 February 2018 at 22:13:05 UTC, Jonathan wrote: Is it possible to cast a 2d static length array to a 1d static length array? E.g. int[2][2] a = [[1,2],[3,4]]; int[4]b = cast(int[4])a; Is not the byte data in memory exactly the same? *( [pos,size].ptr .cst!(void*) .cst!(int[4]*) ) (using dub `cst` library) or *( cast(int[4]*)(cast(void*)([pos,size].ptr)) ) Okay, this works but is this the best way?!
Cast a 2d static array to a 1d static array. T[s][r] -> T[s*r]
Is it possible to cast a 2d static length array to a 1d static length array? E.g. int[2][2] a = [[1,2],[3,4]]; int[4]b = cast(int[4])a; Is not the byte data in memory exactly the same?
Re: Equivalent to Python with Statement
On Tuesday, 27 February 2018 at 16:18:43 UTC, Stefan Koch wrote: On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote: I know Python's `with` statement can be used to have an automatic close action: ``` with open("x.txt") as file: #do something with file #`file.close()` called automatically ``` I know D's `with` statement does something different but is there some sort of equivalent? In this case with(File("bla")) will do the same. Oh really, cool. Is this just because the scope of the file variable will end and thus its `~this`?
Equivalent to Python with Statement
I know Python's `with` statement can be used to have an automatic close action: ``` with open("x.txt") as file: #do something with file #`file.close()` called automatically ``` I know D's `with` statement does something different but is there some sort of equivalent?
Re: Template Constraints
On Saturday, 24 February 2018 at 03:04:07 UTC, Adam D. Ruppe wrote: On Saturday, 24 February 2018 at 02:54:13 UTC, Jonathan wrote: I am having trouble finding many useful explanations of using template constraints beyond basic usage. The constraint is just like static if as to what it allows inside, so you can check almost anything in there. Like for the cast, you might do void name(T)(T t) if(__traits(compiles, cast(int) t) {} just seeing it the cast compiles. You might also do if(is(T : int)) which asks if T is implicitly convertible to int. But since you want explicit cast, the compiles is prolly the way to go. is: https://dlang.org/spec/expression.html#IsExpression compiles: https://dlang.org/spec/traits.html#compiles Thanks, this was just what I needed to know.
Template Constraints
I am having trouble finding many useful explanations of using template constraints beyond basic usage. I would like to have a template constrant to enforce that a type can be explicitly cast to another type: void (T)(T t) if (cast(int) T)//force `cast(int) T` to be possible { // Yay I know `t` can be cast to an `int`! } Is this possible?
Implicit Casting
I am trying to make a `Pos` type. But I need it to implicitly cast from an `int[2]`. I am using the `alias this` to get most of what I want but it still doesn't do all an implicit cast can do. What I have now is this: struct Pos { int[2] pos; alias pos this; this (int[2] pos) { this.pos = pos; } } This allows me to implicitly cast from type `Pos` to type `int[2]` but not the other way. I can do a sort of cast when I define a `Pos` (`Pos pos = [2,3]` works). But what I really want it to do is to implicitly cast an `int[2]` to a `Pos`. Is this possible in D?
Re: Static if to compare two types are the exact same
static if (is(T == V)) Are static ifs always checked outside of runtime? Is it possible for a static if condition to be undeterminable outside of runtime, or would such a condition throw a compiler error?
Static if to compare two types are the exact same
What's the best way to do this? I'm assuming this should be best practice: http://dlang.org/traits.html#isSame struct S { } writeln(__traits(isSame, S, S));
switch statement exiting a void function
Here's the setup, I have a function void main { ... } The main method parses input (via std.getopt) and calls one of three void-return-type functions. The program's three options correspond to significantly different initialization options. In the code we then have: enum RunOpt {opt1, opt2, opt3}; And the body of the function wants to do: RunOpt option; //parsing that results in, among other things option being initialized switch(option){ case RunOpt.opt1: fun1(...); case RunOpt.opt2: fun2(...); default: fun3(...); } When compiling, the error I get is Error: switch case fallthrough - use 'goto case;' if intended This is not intended. Note that calling return; after funi(...) makes everything work. However, it feels like I'm doing something wrong here?
Re: switch statement exiting a void function
Try: enum RunOpt { opt1, opt2, opt3 } // No semicolon here final switch (option) with (RunOpt) { case opt1: fun1(...); break; case opt2: fun2(...); break; case opt3: fun3(...); break; } Bye, bearophile My hero.