Is it possible to obtain textual representation of an arbitary code?
Hi, I wonder if it's possible to convert D language code into a string at compile time? C/C++ preprocessor has this feature built-in: `#` preprocessing operator allows converting a macro argument into a string constant. See the following code snippet for example: ```cplusplus #define ASSERT(EXPR) some_function((EXPR), #EXPR) // Elsewhere in the code: void some_function(bool const condition, char const* const expr) { if (!condition) { printf("Assertion failed for: %s\n", expr); } } // Usage: ASSERT(a == b); // Will print "Assertion failed for: a == b" ``` I could imagine a mixin-based solution in D: ```d // Usage: ASSERT!"a == b"; ``` But it seems a bit alien to me. First of all, it kind of stringly-typed one. Secondly, neither IDEs nor advanced text editors are able to figure out that the string contains actual D code, and so neither syntax highlighting nor code assistance work with this approach. BR, -- Oleksii
Re: Is it possible to obtain textual representation of an arbitary code?
On Friday, January 26, 2018 11:18:21 Oleksii Skidan via Digitalmars-d-learn wrote: > Hi, > > I wonder if it's possible to convert D language code into a > string at compile time? C/C++ preprocessor has this feature > built-in: `#` preprocessing operator allows converting a macro > argument into a string constant. See the following code snippet > for example: > > ```cplusplus > #define ASSERT(EXPR) some_function((EXPR), #EXPR) > > // Elsewhere in the code: > void some_function(bool const condition, char const* const expr) { >if (!condition) { > printf("Assertion failed for: %s\n", expr); >} > } > > // Usage: > ASSERT(a == b); // Will print "Assertion failed for: a == b" > ``` > > I could imagine a mixin-based solution in D: > ```d > // Usage: > ASSERT!"a == b"; > ``` > But it seems a bit alien to me. First of all, it kind of > stringly-typed one. Secondly, neither IDEs nor advanced text > editors are able to figure out that the string contains actual D > code, and so neither syntax highlighting nor code assistance work > with this approach. You can use stringof on symbols to get their string representation, but you can't get sections of code that way. D makes it very easy to turn strings into code using mixin statements, but it doesn't provide ways to take arbitrary code and turn it into strings. And in many cases, the compiler wouldn't necessarily have access to the code anyway, just like you wouldn't in C/C++. C/C++ can do it with macros but not arbitrary code, and D doesn't have the equivalent of C/C++ macros. Arguably the closest thing that D has to C/C++ macros is the ability to mixin strings, and if you're mixing in a string, you already have the code as a string. And yes, if you use a lot of metaprogramming stuff like string mixins, then you're not going to have the code to see in your text editor, but that's what happens when you generate code at compile time rather than having the code sit in a file. However, if you want syntax highlighting for a string literal that contains code, you can use q{} to contain the string literal rather that "" or `` - e.g. q{auto i = 42;} instead of "auto i = 42;", then most text editors will highlight the string as if it were just code. - Jonathan M Davis
Re: Is it possible to obtain textual representation of an arbitary code?
On Friday, 26 January 2018 at 11:18:21 UTC, Oleksii Skidan wrote: I could imagine a mixin-based solution in D: ```d // Usage: ASSERT!"a == b"; ``` But it seems a bit alien to me. First of all, it kind of stringly-typed one. Secondly, neither IDEs nor advanced text editors are able to figure out that the string contains actual D code, and so neither syntax highlighting nor code assistance work with this approach. Token strings are intended for this and editors *should* highlight them (don't know if any currently do): https://dlang.org/spec/lex.html#token_strings
Re: Is it possible to obtain textual representation of an arbitary code?
On Friday, January 26, 2018 11:32:42 Mike Parker via Digitalmars-d-learn wrote: > On Friday, 26 January 2018 at 11:18:21 UTC, Oleksii Skidan wrote: > > I could imagine a mixin-based solution in D: > > ```d > > // Usage: > > ASSERT!"a == b"; > > ``` > > But it seems a bit alien to me. First of all, it kind of > > stringly-typed one. Secondly, neither IDEs nor advanced text > > editors are able to figure out that the string contains actual > > D code, and so neither syntax highlighting nor code assistance > > work with this approach. > > Token strings are intended for this and editors *should* > highlight them (don't know if any currently do): > > https://dlang.org/spec/lex.html#token_strings So that's what they're called. I can never remember (though I rarely use them either, since I actually prefer that strings be highlighted as strings and not code even if they contain code). vim definitely highlights them as code, and I would expect most editors that don't understand them to highlight them as if they were code, since q{} looks like code. If anything, it would take a text editor that understood D quite well to highlight it as a string (though really, no editor should be doing that, since the main point of using token strings is for them to be highlighted as code). - Jonathan M Davis
MapViewOfFileEx: Not enough storage is available to process this command.
Hi, in my application I create a zip archive with a size of ~ 400 MB and after that I read the archive. While trying to read the archive, there is an error: std.windows.syserror.WindowsException@std\mmfile.d(267): MapViewOfFileEx: Not enough storage is available to process this command. (error 8) 0x0045B2AE in @safe void* std.windows.syserror.wenforce!(void*, immutable(char)[]).wenforce(void*, lazy immutable(char)[], immutable(char)[], uint) 0x0044F4D8 in std.mmfile.MmFile std.mmfile.MmFile.__ctor(immutable(char)[]) From the source code I cannot see any issue. Do you have an idea what the issue is causing? void zipFolder(string archiveFilePath, string folderPath) { import std.zip, std.file; import std.exception: enforce; import std.path: baseName; enforce(exists(folderPath) && isDir(folderPath)); ZipArchive zip = new ZipArchive(); string folderName = folderPath.baseName; foreach(entry; dirEntries(folderPath, SpanMode.depth)) { if (!entry.isFile) continue; ArchiveMember am = new ArchiveMember(); am.name = entry.name[folderPath.length + 1..$]; am.expandedData(cast(ubyte[]) read(entry.name)); zip.addMember(am); } void[] compressed_data = zip.build(); write(archiveFilePath, compressed_data); } string[] listZipContent(string archiveFilePath) { import std.zip, std.file, std.mmfile; auto mmfile = new MmFile(archiveFilePath); // <-- causing the issue auto zip = new ZipArchive(mmfile[]); string[] results; foreach (name, am; zip.directory) results ~= name; return results; } Kind regards André
Re: MapViewOfFileEx: Not enough storage is available to process this command.
On 26/01/2018 12:16 PM, Andre Pany wrote: Hi, in my application I create a zip archive with a size of ~ 400 MB and after that I read the archive. While trying to read the archive, there is an error: std.windows.syserror.WindowsException@std\mmfile.d(267): MapViewOfFileEx: Not enough storage is available to process this command. (error 8) 0x0045B2AE in @safe void* std.windows.syserror.wenforce!(void*, immutable(char)[]).wenforce(void*, lazy immutable(char)[], immutable(char)[], uint) 0x0044F4D8 in std.mmfile.MmFile std.mmfile.MmFile.__ctor(immutable(char)[]) From the source code I cannot see any issue. Do you have an idea what the issue is causing? void zipFolder(string archiveFilePath, string folderPath) { import std.zip, std.file; import std.exception: enforce; import std.path: baseName; enforce(exists(folderPath) && isDir(folderPath)); ZipArchive zip = new ZipArchive(); string folderName = folderPath.baseName; foreach(entry; dirEntries(folderPath, SpanMode.depth)) { if (!entry.isFile) continue; ArchiveMember am = new ArchiveMember(); am.name = entry.name[folderPath.length + 1..$]; am.expandedData(cast(ubyte[]) read(entry.name)); zip.addMember(am); } void[] compressed_data = zip.build(); write(archiveFilePath, compressed_data); } string[] listZipContent(string archiveFilePath) { import std.zip, std.file, std.mmfile; auto mmfile = new MmFile(archiveFilePath); // <-- causing the issue auto zip = new ZipArchive(mmfile[]); string[] results; foreach (name, am; zip.directory) results ~= name; return results; } Kind regards André Could be heap fragmentation to who knows what else assuming of course this is 32bit right? If so, 64bit is the answer.
Re: MapViewOfFileEx: Not enough storage is available to process this command.
On Friday, 26 January 2018 at 12:19:10 UTC, rikki cattermole wrote: Could be heap fragmentation to who knows what else assuming of course this is 32bit right? If so, 64bit is the answer. Thanks for the hint. 64bit solves the issue. Should I anyway create an issue? Kind regards André
Re: Is it possible to obtain textual representation of an arbitary code?
On Friday, 26 January 2018 at 11:32:42 UTC, Mike Parker wrote: On Friday, 26 January 2018 at 11:18:21 UTC, Oleksii Skidan wrote: I could imagine a mixin-based solution in D: ```d // Usage: ASSERT!"a == b"; ``` But it seems a bit alien to me. First of all, it kind of stringly-typed one. Secondly, neither IDEs nor advanced text editors are able to figure out that the string contains actual D code, and so neither syntax highlighting nor code assistance work with this approach. Token strings are intended for this and editors *should* highlight them (don't know if any currently do): https://dlang.org/spec/lex.html#token_strings Seems like I have to add some context into this conversation: I'm writing a poor man's testing framework, since it's the best and easiest way to learn D ;-) I'm trying to achieve something similar to `Catch2``REQUIRE`macro. To be honest, I did not know about toking strings until today, and I don't know D much. Here's what I came up with so far: ```d string require(string expr)(string file = __FILE__, int line = __LINE__) { import std.array, std.conv; return q{ if (!($expr)) { import std.stdio; writeln("Test failed @", `$file`, ":", $line, "\n", " Expected: `", `$expr`, "` to be `true`.\n"); } }.replace("$expr", expr) .replace("$file", file) .replace("$line", to!string(line)); } ``` That code snippet uses token strings to compose an if statement that basically checks whether the given condition holds. That looks okay-ish to me, the usage of that function is not pretty though: ```d unittest { mixin(require!q{false}); // This test will fail. } ``` It would be awesome if I could write something like the this instead: ```d unittest { require!q{false}; } ``` At first glance it seems like I could have moved the `mixin` statement into the `require` function itself, but that would not really work. Consider the following snippet: ```d unittest { float value = 3f; require!q{value == 3f}; // This line won't compile. } ``` That code won't even compile, since `value` exists in `unittest` scope, which is not visible to the `require` function. -- Oleksii
Re: MapViewOfFileEx: Not enough storage is available to process this command.
On 26/01/2018 12:32 PM, Andre Pany wrote: On Friday, 26 January 2018 at 12:19:10 UTC, rikki cattermole wrote: Could be heap fragmentation to who knows what else assuming of course this is 32bit right? If so, 64bit is the answer. Thanks for the hint. 64bit solves the issue. Should I anyway create an issue? Kind regards André No, everything is working as designed.
Re: Is it possible to obtain textual representation of an arbitary code?
On Friday, 26 January 2018 at 11:32:42 UTC, Mike Parker wrote: On Friday, 26 January 2018 at 11:18:21 UTC, Oleksii Skidan wrote: I could imagine a mixin-based solution in D: ```d // Usage: ASSERT!"a == b"; ``` But it seems a bit alien to me. First of all, it kind of stringly-typed one. Secondly, neither IDEs nor advanced text editors are able to figure out that the string contains actual D code, and so neither syntax highlighting nor code assistance work with this approach. Token strings are intended for this and editors *should* highlight them (don't know if any currently do): https://dlang.org/spec/lex.html#token_strings LOL, all do that: https://imgur.com/a/UoHpz.
Re: Is it possible to obtain textual representation of an arbitary code?
On Friday, January 26, 2018 12:30:03 Oleksii Skidan via Digitalmars-d-learn wrote: > On Friday, 26 January 2018 at 11:32:42 UTC, Mike Parker wrote: > > On Friday, 26 January 2018 at 11:18:21 UTC, Oleksii Skidan > > > > wrote: > >> I could imagine a mixin-based solution in D: > >> ```d > >> // Usage: > >> ASSERT!"a == b"; > >> ``` > >> But it seems a bit alien to me. First of all, it kind of > >> stringly-typed one. Secondly, neither IDEs nor advanced text > >> editors are able to figure out that the string contains actual > >> D code, and so neither syntax highlighting nor code assistance > >> work with this approach. > > > > Token strings are intended for this and editors *should* > > highlight them (don't know if any currently do): > > > > https://dlang.org/spec/lex.html#token_strings > > Seems like I have to add some context into this conversation: I'm > writing a poor man's testing framework, since it's the best and > easiest way to learn D ;-) > > I'm trying to achieve something similar to > `Catch2``REQUIRE`macro. To be honest, I did not know about toking > strings until today, and I don't know D much. Here's what I came > up with so far: > > ```d > string require(string expr)(string file = __FILE__, int line = > __LINE__) > { > import std.array, std.conv; > return q{ > if (!($expr)) { > import std.stdio; > writeln("Test failed @", `$file`, ":", $line, "\n", > " Expected: `", `$expr`, "` to be > `true`.\n"); > } > }.replace("$expr", expr) > .replace("$file", file) > .replace("$line", to!string(line)); > } > > ``` > > That code snippet uses token strings to compose an if statement > that basically checks whether the given condition holds. That > looks okay-ish to me, the usage of that function is not pretty > though: > > ```d > unittest > { > mixin(require!q{false}); // This test will fail. > } > ``` > > It would be awesome if I could write something like the this > instead: > > ```d > unittest > { > require!q{false}; > } > ``` > > At first glance it seems like I could have moved the `mixin` > statement into the `require` function itself, but that would not > really work. Consider the following snippet: > > ```d > unittest > { > float value = 3f; > require!q{value == 3f}; // This line won't compile. > } > ``` > > That code won't even compile, since `value` exists in `unittest` > scope, which is not visible to the `require` function. Why are you using strings for any of this? Printing out the expression is kind of pointless. If you have the file and line number (which an AssertError will give you), then you know where the failure is, and you can see the expression. All of this extra machinery is just going to increase your compile times for no benefit. So, what you're doing here is objectively worse than just using assertions. There might be some value if you had something like assertEqual(lhs, rhs); and then on failure, you printed the values that were being compared, since that's not necessarily information that's in the code, but the expressions themselves _are_ already in the code, so printing them out doesn't help any. But even if you have helper functions that take the values separately so that they can be printed, in my experience, the extra template instantiations required to use helper functions like that everywhere in unit tests increases the compilation times (and memory required) enough that it's not worth it, especially when you consider that once the tests are passing, all of that extra machinery does you no good whatsoever. Ultimately, it just costs less to temporarily make an adjustment to the test and rerun it if you need more information. If you don't think that simply using assertions for unit tests is good enough, then I'd suggest that you look at https://code.dlang.org/packages/unit-threaded - Jonathan M Davis
Re: Is it possible to obtain textual representation of an arbitary code?
On Friday, 26 January 2018 at 13:05:26 UTC, Jonathan M Davis wrote: If you don't think that simply using assertions for unit tests is good enough, then I'd suggest that you look at https://code.dlang.org/packages/unit-threaded There's also https://code.dlang.org/packages/fluent-asserts which shows detailed error messages.
Rewriting a c++ template to D (replacing iterator with ranges "properly")
It basically steps through in a stride and sets the checkpoints to false. C++: template void mark(It begin, It end, N step) { assert(begin != end) *begin = false; while (end - begin > step) { begin = begin + step; *begin = false; } } For D this is what I figured would be the way? void mark(R, N)(auto ref R range, N step) if ( /* 1 */ isIntegral!N /* 2 */ && isRandomAccessRange!R /* 3 */ && is(ElementType!R == bool) /* 4 */ && hasAssignableElements!R ) { range.front = false; while (!range.empty) { range.popFrontN(N); range.front = false; } } I have a more specific question too: 1) I've seen some phobos code checking for assignability like this: is(typeof(range.front = false)) ... is that an advantage of that over hasAssignableElements? Or is that just basically combining constraints 3 and 4 which I have above? 2) Say I wanted to restrict to only lvalue ranges passed in as inputs. Does that mean I use hasLvalueElements as a constraint or is remove the "auto" and just have a ref parameter sufficient? Cheers
Re: Rewriting a c++ template to D (replacing iterator with ranges "properly")
On Friday, 26 January 2018 at 14:16:04 UTC, aliak wrote: range.front = false; while (!range.empty) { range.popFrontN(N); range.front = false; } } Oops, this should be: while (!range.empty) { range.front = false; range.popFrontN(N); }
Re: Is it possible to obtain textual representation of an arbitary code?
On Friday, 26 January 2018 at 13:05:26 UTC, Jonathan M Davis wrote: Why are you using strings for any of this? Printing out the expression is kind of pointless. If you have the file and line number (which an AssertError will give you), then you know where the failure is, and you can see the expression. All of this extra machinery is just going to increase your compile times for no benefit. So, what you're doing here is objectively worse than just using assertions. There might be some value if you had something like assertEqual(lhs, rhs); and then on failure, you printed the values that were being compared, since that's not necessarily information that's in the code, but the expressions themselves _are_ already in the code, so printing them out doesn't help any. That's actually what I have: ``` // Usage: // mixin(requireEq!q{expected, actual}); // Checks whether the `actual` value is equal to `reauired`. string requireEq(string expr)(string file = __FILE__, int line = __LINE__) { import std.array, std.conv, std.string; auto parts = expr.split(","); return q{ { bool equal = false; static if (__traits(isFloating, $expected) || __traits(isFloating, $actual)) { import std.math; equal = approxEqual($expected, $actual); } else { equal = $expected == $actual; } if (!equal) { import std.stdio, std.conv; writeln("Test failed @", `$file`, ":", $line, "\n", " Expected `", `$actual`, "` to be `", to!string($expected), "`, but got `", to!string($actual), "`\n"); } } }.replace("$expected", parts[0].strip()) .replace("$actual", parts[1].strip()) .replace("$file", file) .replace("$line", to!string(line)); } ``` The sample code I posted before, was way much simpler than this. But even if you have helper functions that take the values separately so that they can be printed, in my experience, the extra template instantiations required to use helper functions like that everywhere in unit tests increases the compilation times (and memory required) enough that it's not worth it, especially when you consider that once the tests are passing, all of that extra machinery does you no good whatsoever. Ultimately, it just costs less to temporarily make an adjustment to the test and rerun it if you need more information. I won't argue against asserts. I agree with your point: they are fine for simple cases, and they are definitely faster to compile than any other framework. My ultimate goal is to implement a BDD framework, so that I could write tests in the most productive (for myself) way. I'm not aiming for a production-ready quality, a toy framework that suits my needs would be just fine. If you don't think that simply using assertions for unit tests is good enough, then I'd suggest that you look at https://code.dlang.org/packages/unit-threaded Thanks, I'll look at it. Also, I have a vague idea that aliases may be the key to the desired functionality. BR -- Oleksii
Re: value of 'this' is not know at CT for typeof(this)
On Friday, 26 January 2018 at 20:08:19 UTC, Dechcaudron wrote: So I'm trying to get this to compile: ``` static foreach (alias member; getSymbolsByUDA!(typeof(this), Serialize)) serializeMember!member(bundle); ``` And I'm getting the following error: value of 'this' is not known at compile time for the line on top. typeof(this) to get the type of the calling object seems to work everywhere else (this happens inside a method definition inside a mixin template). Can anyone tell me why? I can't reproduce the problem. Could you give us some more code - preferably a compilable segment that gives the same problem? -- Simen
Re: Rewriting a c++ template to D (replacing iterator with ranges "properly")
On Friday, 26 January 2018 at 14:35:25 UTC, Meta wrote: On Friday, 26 January 2018 at 14:16:04 UTC, aliak wrote: 1) I've seen some phobos code checking for assignability like this: is(typeof(range.front = false)) ... is that an advantage of that over hasAssignableElements? Or is that just basically combining constraints 3 and 4 which I have above? Where did you see this? That's got to be some very old code; I can't think of any instance where you would not want to use `hasAssignableElements` instead. Seems to occur in https://github.com/dlang/phobos/blob/v2.078.1/std/algorithm/mutation.d eg: https://github.com/dlang/phobos/blob/v2.078.1/std/algorithm/mutation.d#L555
Re: Is it possible to obtain textual representation of an arbitary code?
On Friday, 26 January 2018 at 13:05:26 UTC, Jonathan M Davis wrote: On Friday, January 26, 2018 12:30:03 Oleksii Skidan via Digitalmars-d-learn wrote: On Friday, 26 January 2018 at 11:32:42 UTC, Mike Parker wrote: > On Friday, 26 January 2018 at 11:18:21 UTC, Oleksii Skidan > > wrote: >> [...] > > Token strings are intended for this and editors *should* > highlight them (don't know if any currently do): > > https://dlang.org/spec/lex.html#token_strings Seems like I have to add some context into this conversation: I'm writing a poor man's testing framework, since it's the best and easiest way to learn D ;-) I'm trying to achieve something similar to `Catch2``REQUIRE`macro. To be honest, I did not know about toking strings until today, and I don't know D much. Here's what I came up with so far: ```d string require(string expr)(string file = __FILE__, int line = __LINE__) { import std.array, std.conv; return q{ if (!($expr)) { import std.stdio; writeln("Test failed @", `$file`, ":", $line, "\n", " Expected: `", `$expr`, "` to be `true`.\n"); } }.replace("$expr", expr) .replace("$file", file) .replace("$line", to!string(line)); } ``` That code snippet uses token strings to compose an if statement that basically checks whether the given condition holds. That looks okay-ish to me, the usage of that function is not pretty though: ```d unittest { mixin(require!q{false}); // This test will fail. } ``` It would be awesome if I could write something like the this instead: ```d unittest { require!q{false}; } ``` At first glance it seems like I could have moved the `mixin` statement into the `require` function itself, but that would not really work. Consider the following snippet: ```d unittest { float value = 3f; require!q{value == 3f}; // This line won't compile. } ``` That code won't even compile, since `value` exists in `unittest` scope, which is not visible to the `require` function. Why are you using strings for any of this? Printing out the expression is kind of pointless. If you have the file and line number (which an AssertError will give you), then you know where the failure is, and you can see the expression. All of this extra machinery is just going to increase your compile times for no benefit. So, what you're doing here is objectively worse than just using assertions. There might be some value if you had something like assertEqual(lhs, rhs); and then on failure, you printed the values that were being compared, since that's not necessarily information that's in the code, but the expressions themselves _are_ already in the code, so printing them out doesn't help any. But even if you have helper functions that take the values separately so that they can be printed, in my experience, the extra template instantiations required to use helper functions like that everywhere in unit tests increases the compilation times (and memory required) enough that it's not worth it, especially when you consider that once the tests are passing, all of that extra machinery does you no good whatsoever. Ultimately, it just costs less to temporarily make an adjustment to the test and rerun it if you need more information. If you don't think that simply using assertions for unit tests is good enough, then I'd suggest that you look at https://code.dlang.org/packages/unit-threaded - Jonathan M Davis I've just realized that I can actually make the test code more pleasant if I use string concatenation. For example, this test: ``` unittest { import testing; { Particle a = Particle(0.9, 0); Particle b = Particle(2.1, 0); Constraint c = Constraint(a, b); c.distance = 1f; c.solve(); mixin(requireEq!q{1f, a.x}); mixin(requireEq!q{0f, a.y}); mixin(requireEq!q{2f, b.x}); mixin(requireEq!q{0f, b.y}); } } ``` could be written as: ``` unittest { import testing; { Particle a = Particle(0.9, 0); Particle b = Particle(2.1, 0); Constraint c = Constraint(a, b); c.distance = 1f; c.solve(); mixin( requireEq!q{1f, a.x} ~ requireEq!q{0f, a.y} ~ requireEq!q{2f, b.x} ~ requireEq!q{0f, b.y} ); } } ``` That code looks a little bit unusual, but I guess I can get used to it. Seems like I can write a test scenario per mixin.
Re: Rewriting a c++ template to D (replacing iterator with ranges "properly")
On Friday, 26 January 2018 at 14:16:04 UTC, aliak wrote: 1) I've seen some phobos code checking for assignability like this: is(typeof(range.front = false)) ... is that an advantage of that over hasAssignableElements? Or is that just basically combining constraints 3 and 4 which I have above? Where did you see this? That's got to be some very old code; I can't think of any instance where you would not want to use `hasAssignableElements` instead.
Help me understand how to contribute to bugs report / fixing
Hi, I have been lurking in the bug tracker for some time, checking and trying to reproduce bugs and fixes. I finally want to submit something and contribute but I am having an hard time understanding the workflow. Pull request are done via git and bugs reported by the tracker. The problem is when I want to understand if the bug of the tracker is referenced in the repo of the organization and has an open PR. --- Real world case: this bug has been reported recently: https://issues.dlang.org/show_bug.cgi?id=18288#add_comment but it should be closed given how in the recent changes to the phobos master branch the bug is fixed. You can double check that by running the code snippet using dmd-nightly in run.dlang.io. From a quick glance at the phobos repo, I found no mention of this bug in any closed or open PR, just a PR (#6056, bug 18280) on the same file (comparison.d) that probably fixed the issue for bug 18288. What should I do now? I am undecided between: - commenting on the bug tracker and close the bug - link the pr 6056 on the bug tracker - leaving it be I feel that the section "Get involved" on the D wiki should aid newcomers with a more detailed description of the workflow of maintainers and collaborators.
value of 'this' is not know at CT for typeof(this)
So I'm trying to get this to compile: ``` static foreach (alias member; getSymbolsByUDA!(typeof(this), Serialize)) serializeMember!member(bundle); ``` And I'm getting the following error: value of 'this' is not known at compile time for the line on top. typeof(this) to get the type of the calling object seems to work everywhere else (this happens inside a method definition inside a mixin template). Can anyone tell me why?
Re: Rewriting a c++ template to D (replacing iterator with ranges "properly")
On Friday, 26 January 2018 at 14:16:04 UTC, aliak wrote: It basically steps through in a stride and sets the checkpoints to false. C++: template N> void mark(It begin, It end, N step) { assert(begin != end) *begin = false; while (end - begin > step) { begin = begin + step; *begin = false; } } what is N here? You're declaring it to be an int value in the template<> definition, and then use it as a type in the function definition. For D this is what I figured would be the way? void mark(R, N)(auto ref R range, N step) if ( /* 1 */ isIntegral!N /* 2 */ && isRandomAccessRange!R /* 3 */ && is(ElementType!R == bool) /* 4 */ && hasAssignableElements!R ) { range.front = false; while (!range.empty) { range.popFrontN(N); range.front = false; } } Not exactly. range.front will assert after the last popFrontN (since the range is empty). You'll need something like this: void main(R, N)(R range, N step) if (isIntegral!N && isRandomAccessRange!R && is(ElementType!R == bool) && hasAssignableElements!R) { if (range.empty) return; do { range.front = false; range.popFrontN(N); } while (!range.empty); } 1) I've seen some phobos code checking for assignability like this: is(typeof(range.front = false)) ... is that an advantage of that over hasAssignableElements? Or is that just basically combining constraints 3 and 4 which I have above? It's trying to combine 3 and 4 I think, but it fails because this is allowed in D: int a; a = false; 2) Say I wanted to restrict to only lvalue ranges passed in as inputs. Does that mean I use hasLvalueElements as a constraint or is remove the "auto" and just have a ref parameter sufficient? You'll want to pass the range as ref. hasLvalueElements checks if the elements have lvalue semantics, not if the range itself does. Here's a range that has lvalue elements, for which passing it as ref or non-ref makes a huge difference: import std.range; struct R { int[3] elements; int index; ref auto front() { return elements[index]; } void popFront() { ++index; } bool empty() { return index >= elements.length; } } unittest { assert(hasLvalueElements!(R)); auto a = R([1,2,3], 0); auto b = a; b.front = 4; assert(a.elements == [1,2,3]); assert(b.elements == [4,2,3]); } As we can see, b is a complete copy of a, and changing b does not change a. The exact same behavior would occur if a was passed by value to a function. -- Simen
Re: Is it possible to obtain textual representation of an arbitary code?
On Friday, 26 January 2018 at 11:18:21 UTC, Oleksii Skidan wrote: Hi, I wonder if it's possible to convert D language code into a string at compile time? C/C++ preprocessor has this feature built-in: `#` preprocessing operator allows converting a macro argument into a string constant. See the following code snippet for example: See feature request I suggested here: https://forum.dlang.org/thread/mailman.2377.1516230845.9493.digitalmar...@puremagic.com __ARGS__ : allow access to (stringified) arguments, as C's `#arg` macro
Re: Class instance memory overhead lower than 3 words?
On 1/24/18 4:48 PM, Nordlöw wrote: Why is the memory overhead for a class instance as high as 3 words (24 bytes on 64-bit systems? I find that annoyingly much for my knowledge database application. I'm aware of extern(C++), having one word overhead, but such extern(C++)-classes cannot use all of D; I get compilation errors such as node.d(99,25): Error: Internal Compiler Error: type `inout(Edge)[]` can not be mapped to C++ Should be 2 words, the monitor and the typeinfo pointer. What is the 3rd one? On 64-bit macos: class C { } void main() { import std.stdio; writeln(typeid(C).initializer.length); // 16 } -Steve
Strange compiler error. Whose bug is that?
Hi, I get a strange error: ``` λ dub build Performing "debug" build using D:\d\dmd2\windows\bin\dmd.exe for x86. strange ~master: building configuration "application"... source\app.d(24,18): Error: non-constant expression &[Particle(1.0F, 1.0F, 1.0F), Particle(2.0F, 2.0F, 1.0F), Particle(3.0F, 3.0F, 1.0F)][0] source\app.d(25,18): Error: non-constant expression &[Particle(1.0F, 1.0F, 1.0F), Particle(2.0F, 2.0F, 1.0F), Particle(3.0F, 3.0F, 1.0F)][1] source\app.d(24,18): Error: non-constant expression &[Particle(1.0F, 1.0F, 1.0F), Particle(2.0F, 2.0F, 1.0F), Particle(3.0F, 3.0F, 1.0F)][1] source\app.d(25,18): Error: non-constant expression &[Particle(1.0F, 1.0F, 1.0F), Particle(2.0F, 2.0F, 1.0F), Particle(3.0F, 3.0F, 1.0F)][2] source\app.d(24,18): Error: non-constant expression &[Particle(1.0F, 1.0F, 1.0F), Particle(2.0F, 2.0F, 1.0F), Particle(3.0F, 3.0F, 1.0F)][2] source\app.d(25,18): Error: non-constant expression &[Particle(1.0F, 1.0F, 1.0F), Particle(2.0F, 2.0F, 1.0F), Particle(3.0F, 3.0F, 1.0F)][0] D:\d\dmd2\windows\bin\dmd.exe failed with exit code 1. ``` when compiling the following code: ``` import std.stdio; struct Particle { float x; float y; float invMass; this(float x, float y) { this.x = x; this.y = y; this.invMass = 1f; } } struct Constraint { Particle* x; Particle* y; float distance; this(ref Particle x, ref Particle y) { this.x = &x; this.y = &y; auto dx = x.x - y.x; auto dy = x.y - y.y; import std.math; this.distance = sqrt(dx*dx + dy*dy); } } interface Body { void tick(); } class Triangle : Body { Particle[3] particles = [ Particle(1, 1), Particle(2, 2), Particle(3, 3) ]; Constraint[3] constraints; this() { constraints[0] = Constraint(particles[0], particles[1]); constraints[1] = Constraint(particles[1], particles[2]); constraints[2] = Constraint(particles[2], particles[0]); } void tick() { particles[0].x += 1; } } struct Game { Triangle player = new Triangle; void tick() { player.tick(); } } void main() { Game game; game.tick(); } ``` Is that my bug (highest probability here) or a compiler bug? I'm confused, since the following `main` function compiles successfully: ``` void main() { Triangle player = new Triangle; player.tick(); } ``` as well as this code: ``` struct Game { Triangle player; this(Triangle player) { this.player = player; } void tick() { player.tick(); } } Game makeGame() { return Game(new Triangle); } void main() { auto game = makeGame(); game.tick(); } ``` What's wrong with the first code snippet? Thanks in advance, -- Oleksii
Re: Rewriting a c++ template to D (replacing iterator with ranges "properly")
On Friday, 26 January 2018 at 14:59:09 UTC, Simen Kjærås wrote: what is N here? You're declaring it to be an int value in the template<> definition, and then use it as a type in the function definition. Oops again :) Should've been typename N (where N is some integral type). Not exactly. range.front will assert after the last popFrontN (since the range is empty). Ya, sorry, realized this a bit after I posted. It's trying to combine 3 and 4 I think, but it fails because this is allowed in D: int a; a = false; Ah true, so it's more of a is(ElementType!R : bool) check? You'll want to pass the range as ref. hasLvalueElements checks if the elements have lvalue semantics Doh, of course. It's in the name even! import std.range; struct R { int[3] elements; int index; ref auto front() { return elements[index]; } void popFront() { ++index; } bool empty() { return index >= elements.length; } } unittest { assert(hasLvalueElements!(R)); auto a = R([1,2,3], 0); auto b = a; b.front = 4; assert(a.elements == [1,2,3]); assert(b.elements == [4,2,3]); } As we can see, b is a complete copy of a, and changing b does not change a. The exact same behavior would occur if a was passed by value to a function. -- Simen Thanks for the input!
Re: Help me understand how to contribute to bugs report / fixing
On Friday, 26 January 2018 at 20:43:05 UTC, Fra Mecca wrote: What should I do now? I am undecided between: - commenting on the bug tracker and close the bug - link the pr 6056 on the bug tracker - leaving it be Leaving a comment on the bug with a link to the PR, and marking the bug resolved fixed is exactly what you should do, and thank you. :) -- Simen
Re: Is it possible to obtain textual representation of an arbitary code?
On Friday, 26 January 2018 at 13:25:12 UTC, Seb wrote: On Friday, 26 January 2018 at 13:05:26 UTC, Jonathan M Davis wrote: If you don't think that simply using assertions for unit tests is good enough, then I'd suggest that you look at https://code.dlang.org/packages/unit-threaded There's also https://code.dlang.org/packages/fluent-asserts which shows detailed error messages. Thanks, That one look great! I like how it prints the source code of the test, so that you have the context within which the test failed.
Re: Help me understand how to contribute to bugs report / fixing
On 01/26/2018 09:43 PM, Fra Mecca wrote: Real world case: this bug has been reported recently: https://issues.dlang.org/show_bug.cgi?id=18288#add_comment [...] From a quick glance at the phobos repo, I found no mention of this bug in any closed or open PR, just a PR (#6056, bug 18280) on the same file (comparison.d) that probably fixed the issue for bug 18288. The issue is actually referenced on the page of PR #6056, in the comment by dlang-bot. The check marks in the "auto-close" column indicate that the issues should have been closed when the PR was merged. But apparently only the first one was actually closed. This might be a bug in dlang-bot. I've filed an issue on the bot: https://github.com/dlang-bots/dlang-bot/issues/175 What should I do now? I am undecided between: - commenting on the bug tracker and close the bug - link the pr 6056 on the bug tracker - leaving it be When you can point to the PR that fixed an issue, close the issue as RESOLVED FIXED and leave a comment pointing to the PR. Something along the lines of "Fixed by https://github.com/dlang/phobos/pull/6056";. When you don't know what fixed an issue, you can close as RESOLVED WORKSFORME. But make sure that you've taken all the necessary steps to reproduce the issue: operating system, compiler flags, etc. In both cases, it's great when you can make a PR that adds a test to the suite, ensuring that the issue doesn't come back. PR #6056 already adds the tests (as far as I see), so that's not needed here.
Re: Possible dmd 2.078 regression ?
On Friday, 26 January 2018 at 21:52:30 UTC, timotheecour wrote: On Friday, 12 January 2018 at 18:51:31 UTC, Basile B. wrote: On Friday, 12 January 2018 at 18:50:10 UTC, Basile B. wrote: On Friday, 12 January 2018 at 17:58:30 UTC, Stefan Koch wrote: On Friday, 12 January 2018 at 14:13:22 UTC, Basile B. wrote: I have a simple program that only compiles if the dependency is not pre-compiled as a static library. It worked fine before. I guess a mangle problem ? Yes and quite old...apparently it's more a 2.074.x regression. i'm digging right now. digger: Commit 1e7b526b40852e9b85df3684430e371034cdf7ec (1/1) is untestable. digger: There are only untestable commits left to bisect. digger: The first bad commit could be any of: digger: 1e7b526b40852e9b85df3684430e371034cdf7ec digger: 6fecaa8232a427fb3ca29c5a5245e08fc43b71b1 digger: f0410bea1ad2b130884964d603b34e729b3e4f69 object.Exception@bisect.d(186): We cannot bisect more! please file a bug on d.puremagic.com/issues/ Yeah i did. https://issues.dlang.org/show_bug.cgi?id=18234 I saw a related (but not similar) thing yesterday. the guy has to pass a dummy.d file for linking to succeed.
Is https://tour.dlang.org under maintenance?
I'm trying to learn D, but many of my site searches take me to https://tour.dlang.org, which cannot be displayed. Of course it may be a local problem (or it might not.) In case this is affecting anyone else, the error I'm getting from firefox is --- Your connection is not secure The website tried to negotiate an inadequate level of security. tour.dlang.org uses security technology that is outdated and vulnerable to attack. An attacker could easily reveal information which you thought to be safe. The website administrator will need to fix the server first before you can visit the site. Error code: NS_ERROR_NET_INADEQUATE_SECURITY --- Have tried going to the http version but your server (or my browser) seems to be setup to auto-redrect to the https version. Please excuse this post if this is a known issue. Now if I could just find a diagram of the exception tree in phobos. I'm trying to throw an exception when all possible environment variables my program could use to find the current account's home directory are not present but can't figure out which one would be appropriate. Thanks,
rdmd main.d leads to Segmentation fault
Hey, simple hello world crashes with segfault: import std.stdio; void main() { writeln("hi"); } $ rdmd main.d Segmentation fault Same problem with a vibe.d project. Just set up this VirtualBox $ dmd --version DMD32 D Compiler v2.078.1 $ rdmd --version rdmd build 20180121 ... $ uname -a Linux timoses-home-debian 4.9.0-4-686 #1 SMP Debian 4.9.65-3+deb9u1 (2017-12-23) i686 GNU/Linux $ dmd main.d $ gdb ./main (gdb) r Starting program: /home/timoses/test3/main [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1". Program received signal SIGSEGV, Segmentation fault. 0x00432e04 in _d_dso_registry () (gdb) bt #0 0x00432e04 in _d_dso_registry () #1 0x00431c63 in ?? () #2 0x0045c08b in __libc_csu_init () #3 0xb7d8e206 in __libc_start_main (main=0x431c24 , argc=1, argv=0xb784, init=0x45c040 <__libc_csu_init>, fini=0x45c0a0 <__libc_csu_fini>, rtld_fini=0xb7feb080 <_dl_fini>, stack_end=0xb77c) at ../csu/libc-start.c:247 #4 0x004315c1 in _start () Any ideas?
Re: Possible dmd 2.078 regression ?
On Friday, 12 January 2018 at 18:51:31 UTC, Basile B. wrote: On Friday, 12 January 2018 at 18:50:10 UTC, Basile B. wrote: On Friday, 12 January 2018 at 17:58:30 UTC, Stefan Koch wrote: On Friday, 12 January 2018 at 14:13:22 UTC, Basile B. wrote: I have a simple program that only compiles if the dependency is not pre-compiled as a static library. It worked fine before. I guess a mangle problem ? Yes and quite old...apparently it's more a 2.074.x regression. i'm digging right now. digger: Commit 1e7b526b40852e9b85df3684430e371034cdf7ec (1/1) is untestable. digger: There are only untestable commits left to bisect. digger: The first bad commit could be any of: digger: 1e7b526b40852e9b85df3684430e371034cdf7ec digger: 6fecaa8232a427fb3ca29c5a5245e08fc43b71b1 digger: f0410bea1ad2b130884964d603b34e729b3e4f69 object.Exception@bisect.d(186): We cannot bisect more! please file a bug on d.puremagic.com/issues/
Re: Is https://tour.dlang.org under maintenance?
On Friday, 26 January 2018 at 22:56:00 UTC, ChrisPiker wrote: I'm trying to learn D, but many of my site searches take me to https://tour.dlang.org, which cannot be displayed. Of course it may be a local problem (or it might not.) In case this is affecting anyone else, the error I'm getting from firefox is No it's not on maintenance. I am not getting this error on neither Chrome or Firefox. See: https://imgur.com/a/wRhrw What Firefox version are you using? Anything specific about your setup? --- Your connection is not secure The website tried to negotiate an inadequate level of security. tour.dlang.org uses security technology that is outdated and vulnerable to attack. An attacker could easily reveal information which you thought to be safe. The website administrator will need to fix the server first before you can visit the site. Error code: NS_ERROR_NET_INADEQUATE_SECURITY --- Have tried going to the http version but your server (or my browser) seems to be setup to auto-redrect to the https version. Yes, Google downranks websites which don't redirect by default to HTTPS. Please excuse this post if this is a known issue. FYI for the future: you can report issues here: https://github.com/dlang-tour/core Now if I could just find a diagram of the exception tree in phobos. I'm trying to throw an exception when all possible environment variables my program could use to find the current account's home directory are not present but can't figure out which one would be appropriate. There's no complex exception hierarchy. You can easily get a list of all exceptions in Phobos, e.g. grep -r ": Exception" * Why don't you create your own exception? ``` import std.exception; class InputException : Exception { /// mixin basicExceptionCtors; } ... throw new InputException("Environment variable " ~ s ~ " isn't set."); ```
Re: Rewriting a c++ template to D (replacing iterator with ranges "properly")
On Friday, 26 January 2018 at 15:33:03 UTC, aliak wrote: On Friday, 26 January 2018 at 14:35:25 UTC, Meta wrote: On Friday, 26 January 2018 at 14:16:04 UTC, aliak wrote: 1) I've seen some phobos code checking for assignability like this: is(typeof(range.front = false)) ... is that an advantage of that over hasAssignableElements? Or is that just basically combining constraints 3 and 4 which I have above? Where did you see this? That's got to be some very old code; I can't think of any instance where you would not want to use `hasAssignableElements` instead. Seems to occur in https://github.com/dlang/phobos/blob/v2.078.1/std/algorithm/mutation.d eg: https://github.com/dlang/phobos/blob/v2.078.1/std/algorithm/mutation.d#L555 The function is called fill, and assigns a value to every element in the range. If a[0] = false compiles, we also want a.fill(false) to compile. It's simply testing that, rather than caring about the exact type of the elements. -- Simen
Re: Command Line Parsing
On Wednesday, 12 April 2017 at 10:58:07 UTC, Nicholas Wilson wrote: On Wednesday, 12 April 2017 at 09:51:34 UTC, Russel Winder wrote: Are Argon https://github.com/markuslaker/Argon or darg https://github. com/jasonwhite/darg getting traction as the default command line handling system for D or are they just peripheral and everyone just uses std.getopt https://dlang.org/phobos/std_getopt.html ? there is also Vladimir Panteleev's ae.funopt https://blog.thecybershadow.net/2014/08/05/ae-utils-funopt/ https://github.com/CyberShadow/ae/blob/master/utils/funopt.d Thanks for mentioning this, looks to be just what the doctor ordered. Coming in from python I've been disappointed by getopt and frustrated by Argon. This looks like it might fit the bill. Meta-names for option arguments ( ex: --index=FILE ) are an efficient way to communicate intent to users, nice to see them included. Thanks,
Re: Possible dmd 2.078 regression ?
On Friday, 26 January 2018 at 22:15:15 UTC, Basile B. wrote: On Friday, 26 January 2018 at 21:52:30 UTC, timotheecour wrote: On Friday, 12 January 2018 at 18:51:31 UTC, Basile B. wrote: On Friday, 12 January 2018 at 18:50:10 UTC, Basile B. wrote: On Friday, 12 January 2018 at 17:58:30 UTC, Stefan Koch wrote: On Friday, 12 January 2018 at 14:13:22 UTC, Basile B. wrote: I have a simple program that only compiles if the dependency is not pre-compiled as a static library. It worked fine before. I guess a mangle problem ? Yes and quite old...apparently it's more a 2.074.x regression. i'm digging right now. digger: Commit 1e7b526b40852e9b85df3684430e371034cdf7ec (1/1) is untestable. digger: There are only untestable commits left to bisect. digger: The first bad commit could be any of: digger: 1e7b526b40852e9b85df3684430e371034cdf7ec digger: 6fecaa8232a427fb3ca29c5a5245e08fc43b71b1 digger: f0410bea1ad2b130884964d603b34e729b3e4f69 object.Exception@bisect.d(186): We cannot bisect more! please file a bug on d.puremagic.com/issues/ Yeah i did. https://issues.dlang.org/show_bug.cgi?id=18234 I saw a related (but not similar) thing yesterday. the guy has to pass a dummy.d file for linking to succeed. Ah it was you actually: https://forum.dlang.org/thread/mailman.2616.1516919399.9493.digitalmars-d-b...@puremagic.com
Re: rdmd main.d leads to Segmentation fault
On Friday, 26 January 2018 at 22:40:29 UTC, Timoses wrote: Hey, simple hello world crashes with segfault: [...] Where did you get the D toolchain? Does the same segmentation fault happen with dmd or gdc or ldc? (dmd should be the more probable) Can you compile a dub project?
Re: Is https://tour.dlang.org under maintenance?
On Saturday, 27 January 2018 at 01:15:03 UTC, ChrisPiker wrote: On Friday, 26 January 2018 at 23:55:06 UTC, Seb wrote: No it's not on maintenance. I am not getting this error on neither Chrome or Firefox. See: https://imgur.com/a/wRhrw Oh good. Was a bit worried that it might be affecting everyone. What Firefox version are you using? Anything specific about your setup? I'm running version 52.5.1 on RedHat 6, turned off all add-ons to make sure none were affecting page load but still received the same result. I'll try again from a computer running a different OS and/or browser and see what happens. It's still strange. Mind to open an issue and share screenshots here? https://github.com/dlang-tour/core/issues/new tour.dlang.{org,io} uses LetsEncrypt which is used by a lot of websites. BTW you can run the tour offline too: ``` git clone https://github.com/dlang-tour/core cd core git submodule foreach git pull origin master dub ``` Why don't you create your own exception? I'm new to D and just trying to use the tools already provide in the standard library before making my own or looking for alternatives. Coming from Java and Python it seems to be re-inventing the wheel a bit to generate purpose build exceptions to report environment problems. These must occur all the time in may different programs. If the common D way is make your own exceptions I could do that, no problem. In D typically you just use `enforce`: https://dlang.org/phobos/std_exception.html#enforce (except for bigger projects / libraries, of course) Example: ``` enforce("The environment variable FOOBAR isn't defined", "FOOBAR" !in environment); ``` The mantra here is that in the majority of all cases you don't care as long as the uncaught exception kills the program and prints a nice message to the user or your log. And more complex projects want to define and use their own Exception hierarchies anyways. The standard library only defines Exceptions it can/will throw itself.
Re: Is https://tour.dlang.org under maintenance?
On Saturday, 27 January 2018 at 01:53:12 UTC, Seb wrote: It's still strange. Mind to open an issue and share screenshots here? https://github.com/dlang-tour/core/issues/new I just tried going there on a Mint 18 system running firefox 57.0.1 and everything loaded just fine. I'm hesitant to create more issue noise when the problem is only present (so far) on a very specific old Linux distribution. I appreciate that D is mostly maintained by volunteers and don't want to create useless work for them. BTW you can run the tour offline too: ``` git clone https://github.com/dlang-tour/core cd core git submodule foreach git pull origin master dub ``` Not that it's worth debugging since wget should get the job done, but there seems to be a dependency library missing on our systems. The error message when running dub was: ``` ... Compiling Diet HTML template tour.dt... Compiling Diet HTML template editor.dt... Linking... /usr/bin/ld: cannot find -levent collect2: ld returned 1 exit status Error: linker exited with status 1 dmd failed with exit code 1. ``` In D typically you just use `enforce`: https://dlang.org/phobos/std_exception.html#enforce (except for bigger projects / libraries, of course) Example: ``` enforce("The environment variable FOOBAR isn't defined", "FOOBAR" !in environment); ``` Well that looks handy. Thanks!
Re: Is https://tour.dlang.org under maintenance?
On Friday, 26 January 2018 at 23:55:06 UTC, Seb wrote: No it's not on maintenance. I am not getting this error on neither Chrome or Firefox. See: https://imgur.com/a/wRhrw Oh good. Was a bit worried that it might be affecting everyone. What Firefox version are you using? Anything specific about your setup? I'm running version 52.5.1 on RedHat 6, turned off all add-ons to make sure none were affecting page load but still received the same result. I'll try again from a computer running a different OS and/or browser and see what happens. Why don't you create your own exception? I'm new to D and just trying to use the tools already provide in the standard library before making my own or looking for alternatives. Coming from Java and Python it seems to be re-inventing the wheel a bit to generate purpose build exceptions to report environment problems. These must occur all the time in may different programs. If the common D way is make your own exceptions I could do that, no problem.
Re: Is it possible to obtain textual representation of an arbitary code?
On Friday, January 26, 2018 14:23:20 Oleksii Skidan via Digitalmars-d-learn wrote: > On Friday, 26 January 2018 at 13:05:26 UTC, Jonathan M Davis > > wrote: > > Why are you using strings for any of this? Printing out the > > expression is kind of pointless. If you have the file and line > > number (which an AssertError will give you), then you know > > where the failure is, and you can see the expression. All of > > this extra machinery is just going to increase your compile > > times for no benefit. So, what you're doing here is objectively > > worse than just using assertions. > > > > There might be some value if you had something like > > > > assertEqual(lhs, rhs); > > > > and then on failure, you printed the values that were being > > compared, since that's not necessarily information that's in > > the code, but the expressions themselves _are_ already in the > > code, so printing them out doesn't help any. > > That's actually what I have: > > ``` > // Usage: > // mixin(requireEq!q{expected, actual}); > // Checks whether the `actual` value is equal to `reauired`. > string requireEq(string expr)(string file = __FILE__, int line = > __LINE__) > { > import std.array, std.conv, std.string; > > auto parts = expr.split(","); > > return q{ > { > bool equal = false; > > static if (__traits(isFloating, $expected) || > __traits(isFloating, $actual)) { > import std.math; > equal = approxEqual($expected, $actual); > } else { > equal = $expected == $actual; > } > > if (!equal) { > import std.stdio, std.conv; > writeln("Test failed @", `$file`, ":", $line, > "\n", > " Expected `", `$actual`, "` to be `", > to!string($expected), "`, but got `", > > to!string($actual), "`\n"); > } > } > }.replace("$expected", parts[0].strip()) > .replace("$actual", parts[1].strip()) > .replace("$file", file) > .replace("$line", to!string(line)); > } > > ``` > > The sample code I posted before, was way much simpler than this. If that's what you want, there's still no reason to use strings. Just take the two arguments separately as normal function arguments and print their values when there's a failure. Why are you using strings and mixins for any of this? You could do something like void assertEqual(T, U)(T lhs, U rhs, string file = __FILE__, size_t line = __LINE__) { enforce!AssertError(lhs == rhs, format("assertEqual failed. lhs: %s, rhs: %s", lhs, rhs), file, line); } - Jonathan M Davis
Re: Is https://tour.dlang.org under maintenance?
On Saturday, 27 January 2018 at 02:59:24 UTC, ChrisPiker wrote: BTW you can run the tour offline too: ``` git clone https://github.com/dlang-tour/core cd core git submodule foreach git pull origin master dub ``` Not that it's worth debugging since wget should get the job done The (offline) tour allows you to run the code directly in your browser - though of course that's really an absolute necessity. but there seems to be a dependency library missing on our systems. The error message when running dub was: ``` ... Compiling Diet HTML template tour.dt... Compiling Diet HTML template editor.dt... Linking... /usr/bin/ld: cannot find -levent collect2: ld returned 1 exit status Error: linker exited with status 1 dmd failed with exit code 1. ``` The Dlang-Tour uses the web framework vibe.d (https://github.com/vibe-d/vibe.d). vibe.d currently uses libevent as underlying event library by default. Though with the upcoming 0.8.3 release that's about to change soon - it will then use the native D vibe-core scheduler by default. You can already use vibe-core today if you want to: dub --override-config="vibe-d:core/vibe-core" (BTW libevent is typically available as libevent-dev or libevent-devel.) In any case you can also always read the "pure" Markdown content directly on GitHub: https://github.com/dlang-tour/english
Re: Help me understand how to contribute to bugs report / fixing
On Fri, Jan 26, 2018 at 08:43:05PM +, Fra Mecca via Digitalmars-d-learn wrote: [...] > Pull request are done via git and bugs reported by the tracker. The > problem is when I want to understand if the bug of the tracker is > referenced in the repo of the organization and has an open PR. The way it's done is that in your git commit, include a commit message like this: Fix issue 12345: This will cause the bot on github to automatically link the PR to the bugzilla issue, so when the PR gets merged, the bug will be updated. On the bugzilla side, the convention is that once there's a PR for that bug, you post the github link in a bugnote and add "pull" to the list of keywords. This way people can find the PR from the bug or vice versa. [...] > I feel that the section "Get involved" on the D wiki should aid > newcomers with a more detailed description of the workflow of > maintainers and collaborators. Now that you know how, perhaps you should update that page so that it's more helpful to newcomers. That's what a wiki is for. ;-) We could do it too, but it might be better if you did it since you still have the fresh outlook of a newcomer, whereas old-timers like us would probably be unconsciously making assumptions that newcomers have no idea about, and wind up writing something that isn't as helpful as it could be. T -- Always remember that you are unique. Just like everybody else. -- despair.com
Further questions on interfacing to Postgres libpq
An example test program that I'm using to learn D to C interfacing (specifically calling the libpq library) has a call to a C function declared as follows: void PQprint(FILE *fout, /* output stream */ const PGresult *res, const PQprintOpt *po); PQprintOpt is a struct whose first six members are declared as 'pqbool' which is in turn declared as "typedef char pqbool;" in the distributed Postgres header file. I've defined an "alias pqbool = char;" in the D file, which is pretty straightforward. The second of the six members has the name "align", which is a D keyword. So I renamed it "align_" and I presume that won't cause any problems. To deal with the first argument to PQprint, I added "import core.stdc.stdio : FILE;". The question is how to pass the D "stdout" as that argument. The D compiler tells me I can't pass it as is (as was done in C), because in D it's of type "File".
Re: Further questions on interfacing to Postgres libpq
On 27/01/2018 5:11 AM, Joe wrote: An example test program that I'm using to learn D to C interfacing (specifically calling the libpq library) has a call to a C function declared as follows: void PQprint(FILE *fout, /* output stream */ const PGresult *res, const PQprintOpt *po); PQprintOpt is a struct whose first six members are declared as 'pqbool' which is in turn declared as "typedef char pqbool;" in the distributed Postgres header file. I've defined an "alias pqbool = char;" in the D file, which is pretty straightforward. Use ubyte, not char. char has a bunch of logic related to Unicode surrounding it which is clearly not the intent. The second of the six members has the name "align", which is a D keyword. So I renamed it "align_" and I presume that won't cause any problems. You're good, layout+size just has to match not names of fields. To deal with the first argument to PQprint, I added "import core.stdc.stdio : FILE;". The question is how to pass the D "stdout" as that argument. The D compiler tells me I can't pass it as is (as was done in C), because in D it's of type "File". https://dlang.org/phobos/std_stdio.html#.File.getFP
Re: Is https://tour.dlang.org under maintenance?
On Saturday, 27 January 2018 at 03:08:29 UTC, Seb wrote: The Dlang-Tour uses the web framework vibe.d (https://github.com/vibe-d/vibe.d). vibe.d currently uses libevent as underlying event library by default. Though with Well now libevent_pthreads is missing. Anyway, don't worry about it we're (finally) switching over to RedHat 7 soon anyway, deployment is underway, that should bring a browser that's not an old extended support release. the upcoming 0.8.3 release that's about to change soon - it will then use the native D vibe-core scheduler by default. You can already use vibe-core today if you want to: dub --override-config="vibe-d:core/vibe-core" (BTW libevent is typically available as libevent-dev or libevent-devel.) I'll build against the native scheduler in the future if I get a chance to write any vibe.d code. Might as well try it out. In any case you can also always read the "pure" Markdown content directly on GitHub: https://github.com/dlang-tour/english Cool, I'll just use that if needed. Anyway, you've been so helpful that I've setup a small monthly recurring D Foundation donation. Have a good evening,