how to skip empty field in csvReader?
Hi, https://run.dlang.io/is/9afmT1 ``` void main() { import std.csv; import std.stdio: write, writeln, writef, writefln; import std.algorithm.comparison : equal; string text = "Hello;65;;\nWorld;123;7.5"; struct Layout { string name; int value; double other; } auto records = text.csvReader!Layout(';'); assert(records.equal([ Layout("Hello", 65, 2.5), Layout("World", 123, 7.5), ])); } ``` There is an empty field in the 1st line: "Hello;65;;", then std.csv.CSVException@/dlang/dmd/linux/bin64/../../src/phobos/std/csv.d(1232): Floating point conversion error for input "". Is there a way to tell csvReader to skip such empty fields? Or, is there another CSV reader library with this functionality I can use? Thanks.
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: On Monday, 5 June 2023 at 13:57:20 UTC, Ki Rill wrote: How do I generate `setX` methods for all private mutable variables in my class? Do I need to use `__traits`? ```d mixin template GenerateSetters() { static foreach (idx, field; typeof(this).tupleof) static if (__traits(getVisibility,field) == "private") { mixin(q{ void %SETTER(typeof(this.tupleof[idx]) _) { %NAME = _; } } .replace("%NAME", field.stringof) .replace("%SETTER", "set"~toUpper(field.stringof[0..1]) ~ field.stringof[1..$]) ); } } class Rectangle { private Color fillColor; private Color strokeColor; private uint strokeWidth; this(int x, int y) {} mixin GenerateSetters; } void main() { auto rect = new Rectangle(0, 0); rect.setStrokeWidth(4); assert(rect.strokeWidth == 4); } ``` Thank you! That is exactly what I needed. Although another solution provided by Basile B. using attributes opens a door for other posibilities... I don't usually use metaprogramming or code generation much, this is an eye-opening experience.
Re: What's dxml DOMEntity(R) type ?
On Monday, 5 June 2023 at 10:43:27 UTC, 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; } ``` Thanks, that's very helpful. D sometimes drives me crazy, screws up my brain, :-)
Re: How do I generate `setX` methods for all private mutable variables in a class?
On Monday, 5 June 2023 at 13:57:20 UTC, Ki Rill wrote: How do I generate `setX` methods for all private mutable variables in my class? Do I need to use `__traits`? ```d mixin template GenerateSetters() { static foreach (idx, field; typeof(this).tupleof) static if (__traits(getVisibility,field) == "private") { mixin(q{ void %SETTER(typeof(this.tupleof[idx]) _) { %NAME = _; } } .replace("%NAME", field.stringof) .replace("%SETTER", "set"~toUpper(field.stringof[0..1]) ~ field.stringof[1..$]) ); } } class Rectangle { private Color fillColor; private Color strokeColor; private uint strokeWidth; this(int x, int y) {} mixin GenerateSetters; } void main() { auto rect = new Rectangle(0, 0); rect.setStrokeWidth(4); assert(rect.strokeWidth == 4); } ```
Re: unittest under betterC
On Monday, 5 June 2023 at 18:14:31 UTC, DLearner wrote: On Monday, 5 June 2023 at 14:25:33 UTC, Mike Parker wrote: [...] Thank you for the link, can confirm that: ``` int foo() { [...] It's not so easy to deal automatically in case of multiple modules
Re: unittest under betterC
On Monday, 5 June 2023 at 14:25:33 UTC, Mike Parker wrote: [...] The docs say it should work: https://dlang.org/spec/betterc.html#unittests [...] Thank you for the link, can confirm that: ``` int foo() { return 4; } unittest { assert(foo() != 4, "!= Assert triggered."); assert(foo() == 4, "== Assert triggered."); } extern(C) void main() { static foreach(u; __traits(getUnitTests, __traits(parent, main))) u(); } ``` run via: ``` dmd -betterC -unittest -i -run foo2 ``` works as expected. However, as a suggestion to create a consistent experience with 'Full D', should not the combination of '-main' and '-betterC' cause the generation and attachment of the boilerplate code ``` extern(C) void main() { static foreach(u; __traits(getUnitTests, __traits(parent, main))) u(); } ``` to a source file containing just the original function and it's unittests?
Re: How do I generate `setX` methods for all private mutable variables in a class?
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); } ``` Sure the first solution takes advantage of D features but to generate code that finally looks like C# or Delphi (`Set`, `Get`; `property`, etc.) On top of that I tend to prefer the later solution because self-introspection based on `__traits` has corner issues, especially when typecons-like structures are used for the members (e.g sumtype, nullable, etc.).
Re: How do I generate `setX` methods for all private mutable variables in a class?
On Monday, 5 June 2023 at 13:57:20 UTC, Ki Rill wrote: How do I generate `setX` methods for all private mutable variables in my class? Do I need to use `__traits`? I need this for my [tiny-svg](https://github.com/rillki/tiny-svg) project to generate `setX` methods for all Shapes. Example: ```D class Rectangle { private immutable ...; private Color fillColor; private Color strokeColor; private uint strokeWidth; this(int x, int y) {...} mixin(???); // I need this: Rectangle setFillColor(Color color) {...} Rectangle setStrokeColor(Color color) {...} Rectangle setStrokeWidth(uint color) {...} } ``` Is there a reason you can't just make these fields `public`? D supports property accessors, so you can always go back and make them `private` later on if it turns out you need to. For example: ```d // Before class MyClass1 { public int data; } void example1(MyClass1 c) { c.data = 123; // set int n = c.data; // get } // After class MyClass2 { private int data_; int data() { return data; } void data(int value) { data = value; } } void example2(MyClass2 c) { // Usage is exactly the same c.data = 123; // set int n = c.data; // get } ```
Re: unittest under betterC
On Monday, 5 June 2023 at 14:29:35 UTC, Richard (Rikki) Andrew Cattermole wrote: On 06/06/2023 2:25 AM, Mike Parker wrote: On Monday, 5 June 2023 at 14:16:39 UTC, ryuukk_ wrote: In my book this is broken and needs to be fixed, as a user i don't care about under the hood things, it's a you problem, user should be able to unit test The docs say it should work: https://dlang.org/spec/betterc.html#unittests So either the docs are wrong or the implementation is bugged. If there's no issue yet, could you please file one? Yes that is what I recommended earlier in the thread. But automatic running requires druntime. Ah, I see now. It's working as expected then.
Re: How do I generate `setX` methods for all private mutable variables in a class?
On Monday, 5 June 2023 at 13:57:20 UTC, Ki Rill wrote: How do I generate `setX` methods for all private mutable variables in my class? Do I need to use `__traits`? I need this for my [tiny-svg](https://github.com/rillki/tiny-svg) project to generate `setX` methods for all Shapes. Example: ```D class Rectangle { private immutable ...; private Color fillColor; private Color strokeColor; private uint strokeWidth; this(int x, int y) {...} mixin(???); // I need this: Rectangle setFillColor(Color color) {...} Rectangle setStrokeColor(Color color) {...} Rectangle setStrokeWidth(uint color) {...} } ``` Usage: ```D new Rectangle(10, 10) .setFillColor(Colors.white) .setStrokeColor(Colors.black) .setStrokeWidth(3); ``` You need to put an user attribute on the fieldd then to use static introspection to generate the setters. Very basically that works like that ```d enum Set; class Color {} auto generateSetters(T)() { string result; import std.traits; static foreach (m; __traits(allMembers, T)) {{ alias member = __traits(getMember, T, m); static if (hasUDA!(member, Set)) result ~= "void set" ~ m ~ "(" ~ typeof(member).stringof ~ "){}\n"; }} return result; } class Rectangle { @Set private Color fillColor; @Set private Color strokeColor; @Set private uint strokeWidth; mixin(generateSetters!(Rectangle)()); } void main() { with (new Rectangle) { setstrokeWidth(0); setstrokeColor(null); setfillColor(null); } } ``` 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 ;)
Re: unittest under betterC
On Monday, 5 June 2023 at 14:16:39 UTC, ryuukk_ wrote: In my book this is broken and needs to be fixed, as a user i don't care about under the hood things, it's a you problem, user should be able to unit test The docs say it should work: https://dlang.org/spec/betterc.html#unittests So either the docs are wrong or the implementation is bugged. If there's no issue yet, could you please file one?
Re: unittest under betterC
On 06/06/2023 2:25 AM, Mike Parker wrote: On Monday, 5 June 2023 at 14:16:39 UTC, ryuukk_ wrote: In my book this is broken and needs to be fixed, as a user i don't care about under the hood things, it's a you problem, user should be able to unit test The docs say it should work: https://dlang.org/spec/betterc.html#unittests So either the docs are wrong or the implementation is bugged. If there's no issue yet, could you please file one? Yes that is what I recommended earlier in the thread. But automatic running requires druntime.
Re: unittest under betterC
On 06/06/2023 2:16 AM, ryuukk_ wrote: In my book this is broken and needs to be fixed, as a user i don't care about under the hood things, it's a you problem, user should be able to unit test If you as the user disable key components required for the language to fully work, its a you problem, not a compiler developer problem. This is true in C just as much as it is in D. If you want to go at it alone, you have to accept the consequences of your actions.
Re: unittest under betterC
On Monday, 5 June 2023 at 11:41:08 UTC, Richard (Rikki) Andrew Cattermole wrote: On 05/06/2023 3:42 PM, ryuukk_ wrote: I don't know how all this works, but the runtime shouldn't know about tests, imagine if you ship a game, and the player can run all the unittests, that doesn't make sense Currently that is not possible. When you turn on unittests to be compiled in, that runtime will automatically run them and then end the program. The compiler should run the unittest, the compiler should run the unittest, no matter what flag the programmer is using, in that case: -betterC Run what? Until its in a binary on the target platform (which may not be the host), there is nothing to run. Running using CTFE will not allow for testing for things that are platform dependent. Which we do plenty of. You guys should read what you suggest to people sometimes, i repeat, D is not a new language, and D is not the only language, provide greatness to people, not dumb and broken stuff It's not broken. It's working exactly how it needs to work. You can't expect a runtime dependent feature that has no way of working without a runtime, to work without a runtime. In my book this is broken and needs to be fixed, as a user i don't care about under the hood things, it's a you problem, user should be able to unit test
How do I generate `setX` methods for all private mutable variables in a class?
How do I generate `setX` methods for all private mutable variables in my class? Do I need to use `__traits`? I need this for my [tiny-svg](https://github.com/rillki/tiny-svg) project to generate `setX` methods for all Shapes. Example: ```D class Rectangle { private immutable ...; private Color fillColor; private Color strokeColor; private uint strokeWidth; this(int x, int y) {...} mixin(???); // I need this: Rectangle setFillColor(Color color) {...} Rectangle setStrokeColor(Color color) {...} Rectangle setStrokeWidth(uint color) {...} } ``` Usage: ```D new Rectangle(10, 10) .setFillColor(Colors.white) .setStrokeColor(Colors.black) .setStrokeWidth(3); ```
Re: unittest under betterC
On 05/06/2023 3:42 PM, ryuukk_ wrote: I don't know how all this works, but the runtime shouldn't know about tests, imagine if you ship a game, and the player can run all the unittests, that doesn't make sense Currently that is not possible. When you turn on unittests to be compiled in, that runtime will automatically run them and then end the program. The compiler should run the unittest, the compiler should run the unittest, no matter what flag the programmer is using, in that case: -betterC Run what? Until its in a binary on the target platform (which may not be the host), there is nothing to run. Running using CTFE will not allow for testing for things that are platform dependent. Which we do plenty of. You guys should read what you suggest to people sometimes, i repeat, D is not a new language, and D is not the only language, provide greatness to people, not dumb and broken stuff It's not broken. It's working exactly how it needs to work. You can't expect a runtime dependent feature that has no way of working without a runtime, to work without a runtime.
Re: What's dxml DOMEntity(R) type ?
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; } ```
What's dxml DOMEntity(R) type ?
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]; }
Re: unittest under betterC
On Monday, 5 June 2023 at 03:42:20 UTC, ryuukk_ wrote: [...] I don't know how all this works, ... For what it is worth, running _both_ the above code fragments with: ``` dmd -main -unittest -i -run foo ``` (ie removing the -betterC flag) produces: ``` foo.d(8): [unittest] != Assert triggered. 1/1 modules FAILED unittests ``` which is what was expected.