Re: Dynamic length string array at compile time?
On 6/6/23 4:38 PM, Dany12L wrote: On Tuesday, 6 June 2023 at 14:26:01 UTC, Steven Schveighoffer wrote: On 6/5/23 11:45 AM, Dany12L wrote: Hi, I'd be interested to know if it's possible to have a dynamic length array containing strings generated at compile time. Sure. Just do it. Probably the reason nobody answered the question yet is that it trivially works if you try it :) Unless you did try it and it didn't work? In that case, post your code, I'm sure we can fix it. My concern would be to create an immutable array (or other "equivalent") of strings by adding elements to the array at compile time from different modules. OK, so you want to affect the same static variable from multiple places? No, that can't happen that way. You can do it in a functional programming fashion, but there can only be one initialization, you can't edit the thing afterwards. So for instance, you can do: ```d immutable string[] strs = mod1.makeStrings() ~ mod2.makeStrings() ~ ...; ``` In a centralized place. -Steve
Re: unittest under betterC
On Monday, 5 June 2023 at 18:22:45 UTC, Ernesto Castellotti wrote: [...] It's not so easy to deal automatically in case of multiple modules _multiple modules_ The following code, in a batch (.bat) file, works for me: ``` @echo off :loop if [%1]==[] goto loopexit type .\%1.d > .\__temp_%1.d echo extern(C) void main() { static foreach(u; __traits(getUnitTests, __traits(parent, main))) u();} >> .\__temp_%1.d dmd -betterC -unittest -i -run .\__temp_%1.d del .\__temp_%1.d shift goto loop :loopexit ```
Re: How do I generate `setX` methods for all private mutable variables in a class?
On Monday, 5 June 2023 at 18:54:30 UTC, cc wrote: [...] Is there a way to check for mutability as well? I have both immutable and mutable fields. I would like to generate setters for mutable fields only.
Cannot convert expression of a SubType's sub type to the parent SubType on return
I am trying to use std.sumtype which seems to be just what I need for some functions that can return various types of errors with parameters or a success value with different parameters. In this test I get a compilation error with ldc2: ```d import std.stdio; import std.sumtype; struct Error {string message;} struct Success {int s;} SumType!( Error, Success) foo(string[] args) { if (args.length > 1) { return Success(cast(int)args.length); } else { return Error("not enough arguments"); } } int main(string[] args) { foo(args).match!( (Error e) {writeln("Error ", e.message);}, (Success s) {writeln("Success: ", s.s);}); return 0; } ``` It seems that the compiler does not want to convert an Error or Success value to the SumType!(Error, Success) value automatically. Ok, well if I create an alias for the type and assign to an instance of that it seems to work: ```d import std.stdio; import std.sumtype; struct Error {string message;} struct Success {int s;} alias FooReturnType = SumType!( Error, Success); FooReturnType foo(string[] args) { FooReturnType fr; if (args.length > 1) { return fr = Success(cast(int)args.length); } else { return fr = Error("not enough arguments"); } } int main(string[] args) { foo(args).match!( (Error e) {writeln("Error ", e.message);}, (Success s) {writeln("Success: ", s.s);}); return 0; } ``` Or if I just do a simple cast it also seems to work: ```d import std.stdio; import std.sumtype; import std.traits; struct Error {string message;} struct Success {int s;} SumType!( Error, Success) foo(string[] args) { if (args.length > 1) { return cast(ReturnType!foo)Success(cast(int)args.length); } else { return cast(ReturnType!foo)Error("not enough arguments"); } } int main(string[] args) { foo(args).match!( (Error e) {writeln("Error ", e.message);}, (Success s) {writeln("Success: ", s.s);}); return 0; } ``` But both of those seem a little ugly. Is there a better approach than one of these two workarounds? Or a way to make the compiler ok with converting an Error or Success to a SumType!(Error, Success)? ldc2 version: ``` LDC - the LLVM D compiler (1.28.0): based on DMD v2.098.0 and LLVM 11.1.0 built with LDC - the LLVM D compiler (1.28.0) Default target: x86_64-pc-linux-gnu Host CPU: skylake http://dlang.org - http://wiki.dlang.org/LDC ```
Re: Dynamic length string array at compile time?
On Tuesday, 6 June 2023 at 14:26:01 UTC, Steven Schveighoffer wrote: On 6/5/23 11:45 AM, Dany12L wrote: Hi, I'd be interested to know if it's possible to have a dynamic length array containing strings generated at compile time. Sure. Just do it. Probably the reason nobody answered the question yet is that it trivially works if you try it :) Unless you did try it and it didn't work? In that case, post your code, I'm sure we can fix it. -Steve My concern would be to create an immutable array (or other "equivalent") of strings by adding elements to the array at compile time from different modules. I would like to know if it is possible to do something similar
Re: How do I generate `setX` methods for all private mutable variables in a class?
On 6/6/23 09:13, Basile B. wrote: > yeah I know that opDispatch is disliked because it is tried in a SFINAE > fashion, as citicized by Adam. But on the other side it's the best opover. I like how it helped in my current project: user.someShellCommand("-foo", "-bar"); opDispatch makes a command string, passes it to executeShell, takes care of the return code and dumps its output if there was an error. Ali
Re: What's dxml DOMEntity(R) type ?
On 6/6/23 12:15 PM, Ferhat Kurtulmuş wrote: On Tuesday, 6 June 2023 at 14:16:37 UTC, Steven Schveighoffer wrote: In general, the easiset thing to do is use typeof, though it's not always pretty (and not always obvious how to write it). However, it's required for voldemort types. ```d typeof(parseDom("")) DomEntity; ``` İt is one of the nicest features of d. I believe it should not be used too often because it may cause longer compilation times, worse code reading, less comfort with d code scanners, editors etcetera. It should not affect compile times at all. If you are going to instantiate it that way, it will need to be compiled regardless. `typeof` is very low cost, as it's a direct call on the compiler internals. Something like `ReturnType!Foo` is different. -Steve
Re: What's dxml DOMEntity(R) type ?
On Tuesday, 6 June 2023 at 14:16:37 UTC, Steven Schveighoffer wrote: On 6/5/23 6:43 AM, Ferhat Kurtulmuş wrote: On Monday, 5 June 2023 at 10:01:01 UTC, John Xu wrote: [...] ```d import dxml.dom; import std.stdio; DOMEntity!string xmlRoot; int main() { string xml = ""; auto dom = parseDOM(xml); writeln(typeof(dom.children[0]).stringof); // yields "DOMEntity!string" xmlRoot = dom.children[0]; return 0; } ``` In general, the easiset thing to do is use typeof, though it's not always pretty (and not always obvious how to write it). However, it's required for voldemort types. ```d typeof(parseDom("")) DomEntity; ``` -Steve İt is one of the nicest features of d. I believe it should not be used too often because it may cause longer compilation times, worse code reading, less comfort with d code scanners, editors etcetera.
Re: How do I generate `setX` methods for all private mutable variables in a class?
On Tuesday, 6 June 2023 at 14:23:59 UTC, Steven Schveighoffer wrote: On 6/5/23 11:33 AM, Basile B. wrote: [...] Ugh, don't do it that way. Always give opDispatch a template constraint or it will suck to use. Also, given the problem constraints, you can build the method automatically using the string. ```d auto opDispatch(string member, T)(auto ref T t) if(member.startsWith("set")) { mixin(toLower(m[3]), m[4 .. $], " = t;"); } ``` -Steve yeah I know that opDispatch is disliked because it is tried in a SFINAE fashion, as citicized by Adam. But on the other side it's the best opover.
Re: how to skip empty field in csvReader?
On Tuesday, 6 June 2023 at 14:18:25 UTC, Steven Schveighoffer wrote: On 6/6/23 1:09 AM, mw wrote: Is there a way to tell csvReader to skip such empty fields? What I have done is specify that it's a string, and then handle the conversion myself. The std library need to be enhanced to skip such empty field (very simple change I think), it's a common scenario in real world data, which Python can handle easily. Possibly it can use Nullable, but I'm not sure. Or, is there another CSV reader library with this functionality I can use? I don't know how much of this is supported in tsv-utils but you might give it a look. https://github.com/eBay/tsv-utils Do you know if there is any API doc? Readme only has command line doc.
Re: Dynamic length string array at compile time?
On 6/5/23 11:45 AM, Dany12L wrote: Hi, I'd be interested to know if it's possible to have a dynamic length array containing strings generated at compile time. Sure. Just do it. Probably the reason nobody answered the question yet is that it trivially works if you try it :) Unless you did try it and it didn't work? In that case, post your code, I'm sure we can fix it. -Steve
Re: How do I generate `setX` methods for all private mutable variables in a class?
On 6/5/23 11:33 AM, Basile B. wrote: On Monday, 5 June 2023 at 15:13:43 UTC, Basile B. wrote: On Monday, 5 June 2023 at 13:57:20 UTC, Ki Rill wrote: How do I generate `setX` methods for all private mutable although I did not spent time on the setter body... I suppose the question was more about the metprogramming technic, and that you don't want a pre-mashed solution ;) By the way...an other solution is to use [opDispatch](https://dlang.org/spec/operatoroverloading.html#dispatch): ```d class Color {} class Rectangle { private Color fillColor; private Color strokeColor; private uint strokeWidth; auto opDispatch(string member, T)(auto ref T t) { static if (member == "setStrokeWidth") {} else static if (member == "setStrokeColor") {} else static if (member == "setFillColor") {} else static assert(0, "cannot set " ~ member); return this; } } void main() { (new Rectangle) .setStrokeWidth(0) .setStrokeColor(null) .setFillColor(null); } ``` Ugh, don't do it that way. Always give opDispatch a template constraint or it will suck to use. Also, given the problem constraints, you can build the method automatically using the string. ```d auto opDispatch(string member, T)(auto ref T t) if(member.startsWith("set")) { mixin(toLower(m[3]), m[4 .. $], " = t;"); } ``` -Steve
Re: how to skip empty field in csvReader?
On 6/6/23 1:09 AM, mw wrote: Is there a way to tell csvReader to skip such empty fields? What I have done is specify that it's a string, and then handle the conversion myself. Possibly it can use Nullable, but I'm not sure. Or, is there another CSV reader library with this functionality I can use? I don't know how much of this is supported in tsv-utils but you might give it a look. -Steve
Re: What's dxml DOMEntity(R) type ?
On 6/5/23 6:43 AM, Ferhat Kurtulmuş wrote: On Monday, 5 June 2023 at 10:01:01 UTC, John Xu wrote: The parseDOM returns a DOMEntity(R) type, how do I write a xmlRoot as global variable? I need its detailed type (auto / Variant doesn't work). import dxml.dom; ?? xmlRoot; int main() { string xml = readText("a.xml"); auto dom = parseDOM(xml); xmlRoot = dom.children[0]; } ```d import dxml.dom; import std.stdio; DOMEntity!string xmlRoot; int main() { string xml = ""; auto dom = parseDOM(xml); writeln(typeof(dom.children[0]).stringof); // yields "DOMEntity!string" xmlRoot = dom.children[0]; return 0; } ``` In general, the easiset thing to do is use typeof, though it's not always pretty (and not always obvious how to write it). However, it's required for voldemort types. ```d typeof(parseDom("")) DomEntity; ``` -Steve
Re: How do I generate `setX` methods for all private mutable variables in a class?
On Monday, 5 June 2023 at 15:28:34 UTC, Paul Backus wrote: Is there a reason you can't just make these fields `public`? My bet is that OP actually wants to generate something like ```d void setStrokeWidth(uint value) { if (value = strokeWidth) return; strokeWidth = value; redraw(); // or maybe... // needRedraw = true; } ``` that's a common pattern in 2D graphic libraries.
Re: How do I generate `setX` methods for all private mutable variables in a class?
On Monday, 5 June 2023 at 15:28:34 UTC, Paul Backus wrote: Is there a reason you can't just make these fields `public`? My bet is that OP actually wants to generate something like ```d void setStrokeWidth(uint value) { if (value = strokeWidth) return; strokeWidth = value; redraw(); // or maybe... // needRedraw = true; } ``` that's a common pattern in 2D graphics libraries