Hi the following code have problem. in the Message struct, if you comment on the field "hello", it works fine. also if you comment the line "alias h this", also it works fine. if the size of the array is smaller than 3, also it works fine. absolutely there is a problem. the same problem exist on dmd and ldc2 hope you find it and and improve the compilers. here is the code. if you have any question, please do not hesitate. thanks a lot for the D programming language, it is great "my favorite". the code: ///////////////////////////// module test; import std.concurrency; import std.stdio: write, writeln, writef, writefln; struct LimitEntry { int price; int quantity; } struct Header { int a; } struct Message(ITEM_TYPE) { Header h; int hello; int element; ITEM_TYPE[3] array; alias h this; } alias MessageType = Message!(LimitEntry); void spawnedFunc() { receive( (MessageType m) { writeln(m); } ); } void main() { auto id = spawn(&spawnedFunc); auto message = MessageType(); message.h.a = 2; message.hello = 3; message.element = 5; message.array[0] = LimitEntry(0,0); message.array[1] = LimitEntry(1,1); message.array[2] = LimitEntry(2,2); writeln(message); send(id, message); } ////////////////////////////////////////////
additional comments. I see the problem on FreeBSD also. didn't try other OS only (MacOS and FreeBSD). Thanks a lot, George
Reduced: import std.variant; struct Message { int h; int[5] array; alias h this; } unittest { Message msg; msg.array[] = 3; Variant a = msg; auto other = a.get!Message; assert(msg.array[0] == 3); assert(other.array[0] == 3); } Change Variant to VariantN!40, or reduce the size of Message to maxDataSize or below, and it works.
@Biotronic created dlang/phobos pull request #7552 "Fix issue 21021 - std.variant confused by alias this when struct larg…" fixing this issue: - Fix issue 21021 - std.variant confused by alias this when struct larger than maxDataSize https://github.com/dlang/phobos/pull/7552
The root cause here is this, which essentially is what happens inside std.variant.VariantN.opAssign[0]: struct S { int i; alias i this; string s; } unittest { S s = S(3, "Foo"); S s2 = S(s); // calls S(s.i), the auto-generated constructor assert(s.s == ""); } If we rearrange S such that the string comes first, it simply does not compile, but when the alias this is (the same type as) the first element of the struct, S(s) compiles and silently does the wrong thing. [0]: https://github.com/dlang/phobos/blob/22ebc705c14bcc2e18e0323c6d53e3bb7274a4fa/std/variant.d#L689
dlang/phobos pull request #7552 "Fix issue 21021 - std.variant confused by alias this when struct larg…" was merged into master: - f4fc3e5a34c7ac6580b228180fde582fb16f6629 by Simen Kjærås: Fix issue 21021 - std.variant confused by alias this when struct larger than maxDataSize https://github.com/dlang/phobos/pull/7552