Re: Mixin helper help
On Saturday, 14 January 2023 at 23:07:13 UTC, TheZipCreator wrote: This is not the purpose mixin templates are meant to serve. They're for copying declarations into scopes (and as such only support declarations in them). Instead, I think what you want is I'm trying to understand you. But I regret to say that I do not agree with you. You are a programmer like me, so you don't like this usage? ```d template MyContainer(string varS = "") { struct Var { import std.variant; private Variant[string] values; alias values this; @property { Variant opDispatch(string name)() const { return values[name]; } void opDispatch(string name, T)(T val) { values[name] = val; } } } static if(varS.length > 0) { import std.format; mixin(varS.format!"Var %s;"); } else Var data; } void main() { //otherTest("test ok");/* mixin MyContainer!"date"; enum Tarih { AY = 1, YIL = 2023 } date.month = cast(ubyte)Tarih.AY; date.month.write("/"); assert(date["month"] != Tarih.AY); // because : assert(date["month"].type == typeid(ubyte)); date.year = cast(short)Tarih.YIL; date.year.writeln(" in Turkish format"); assert(date["year"] != Tarih.YIL); // because : assert(date["year"].type == typeid(short)); writefln("Date: %s/%s", date.year, date.month);//*/ } /* Prints: 1/2023 in Turkish format Date: 2023/1 //*/ import std.stdio; void otherTest(string str) { mixin MyContainer; data.test = str; data.test.writeln; } ``` I'm 44 and maybe an old-fashioned person who likes macros. But mixins are all kinds of beautiful yaw Because they can be used as easily as importing. Moreover, they can be personalized. SDB@79
Re: Mixin helper help
On Saturday, 14 January 2023 at 18:57:21 UTC, John Chapman wrote: I wanted to remove the double braces in my static foreach (needed as I declared some aliases inside but since it creates a scope those new variables can't be referred to outside of it). Inside a function, you can often just use plain foreach instead of static foreach and simplify things. But it depends on the whole context. Where the helper thing helps a lot is outside functions, where normal foreach and double brace are both prohibited.
Re: Coding Challenges - Dlang or Generic
On Tuesday, 10 January 2023 at 00:17:18 UTC, Paul wrote: Greetings Dlang-ers I was wondering if anyone knew of any coding challenges available where the input and output are specified and its left to the programmer to find a solution? Free would be nice but even paid services would be worth considering. I'm taking a D class right now and it has little challenges in the lessons where much of the work is done for you, but I'm thinking of a site/service that is dedicated to these types of challenges (without doing any work for you). [...] while not specifically D-related, the code golf stack exchange is good place to find challenges (even if you're not golfing).
Re: Mixin helper help
On Thursday, 12 January 2023 at 08:03:34 UTC, John Chapman wrote: I'm obviously doing something wrong, but don't quite understand. ```d mixin template helper() { mixin("writeln(12);"); } struct Foo { void opDispatch(string name)() { import std.stdio; mixin helper!(); //mixin("writeln(12);"); } } void main() { Foo.init.opDispatch!"bar"(); } ``` The compiler emits these errors about the mixin ("writeln(12);"): unexpected `(` in declarator basic type expected, not `12` found `12` when expecting `)` no identifier for declarator `writeln(_error_)` semicolon expected following function declaration declaration expected, not `)` Why does the commented code work but the mixin not? Thanks for any pointers. This is not the purpose mixin templates are meant to serve. They're for copying declarations into scopes (and as such only support declarations in them). Instead, I think what you want is ```d template helper() { const char[] helper = `writeln(12);`; } struct Foo { void opDispatch(string name)() { import std.stdio; mixin(helper!()); } } void main() { Foo.init.opDispatch!"bar"(); } ```
Re: Mixin helper help
On Saturday, 14 January 2023 at 02:51:56 UTC, bauss wrote: Ali That's a good one! also very very good, it's clever! SDB@79
Re: Mixin helper help
On Friday, 13 January 2023 at 14:32:44 UTC, Salih Dincer wrote: Why not directly use the mixin template for opDispatch()? My opDispatch generates code based on the arguments passed, interpolating variable names and functions based on the type. I wanted to remove the double braces in my static foreach (needed as I declared some aliases inside but since it creates a scope those new variables can't be referred to outside of it). I saw Adam's post here http://dpldocs.info/this-week-in-d/Blog.Posted_2022_12_26.html showing use of a "helper" template, and I was trying to adapt it. I've since just dropped the double braces and removed the aliases. It's not as clean but works.
Re: Why does the importC example not compile?
On Friday, 13 January 2023 at 12:46:33 UTC, Dennis wrote: On Friday, 13 January 2023 at 12:33:28 UTC, kdevel wrote: What must be added or changed in order to test every example which is intended to produce an executable? Support for separate compilation / ImportC would need to be added to dspec_tester: https://github.com/dlang/dlang.org/blob/master/tools/dspec_tester.d I ran into this issue too, what I discovered is that when you import `square`, it is importing the file `square.c` (if one exists). Because you have a function called `square` in a file called `square`, confusingly, you want `square.square`. Hence the error message about trying to invoke parens `()` on a module. If you rename the file to `my_c_funcs.c` and then you do: ```d import std.stdio; import my_c_funcs; void main() { int i = 7; writefln("The square of %s is %s", i, my_c_funcs.square(i)); } ``` ```sh (dmd-nightly)[user@MSI source]$ dmd app.d my_c_funcs.c (dmd-nightly)[user@MSI source]$ ./app The square of 7 is 49 ```