Re: Can I output strings using core.stdc.stdio?
On Wednesday, 23 December 2020 at 13:06:37 UTC, drug wrote: On 12/23/20 3:23 PM, Godnyx wrote: Any ideas? Just fix your typos: ```D import std : printf, toStringz; void put(A...)(string prompt, A args) { static foreach (ulong i; 0..args.length) { static if (is(typeof(args[i]) == string)) printf("%s\n", args[i].toStringz); static if (is(typeof(args[i]) == int)) printf("%i\n", args[i]); static if (is(typeof(args[i]) == bool)) { if (args[i]) printf("true"); else printf("false"); } } } void main() { put("Prompt:", "Hello, my age is: ", 19, true); } ``` P.S. replace `arg` by `args[i]` Damn I'm fucking BLIND! Anyway I changed it to foreach (x; args) to get the args itself. Thanks for anything man! The community is the best thing about D!!! ;)
Re: Can I output strings using core.stdc.stdio?
On Wednesday, 23 December 2020 at 13:55:20 UTC, Adam D. Ruppe wrote: On Wednesday, 23 December 2020 at 13:06:37 UTC, drug wrote: static foreach (ulong i; 0..args.length) { static if (is(typeof(args[i]) == string)) printf("%s\n", args[i].toStringz); static if (is(typeof(args[i]) == int)) Putting some `else` in there would help too to ensure your thing only ever matches one branch. doesn't matter here but will later if you add generic array support. and then there's const etc but that's yet another thing so wait on that till you have the basics down I will probably but I probably won't add array, class support. Probably I'll use this only for debug purposes with std.write. Thanks a lot for everything!!!
Re: Can I output strings using core.stdc.stdio?
On Wednesday, 23 December 2020 at 09:42:42 UTC, Ferhat Kurtulmuş wrote: On Wednesday, 23 December 2020 at 09:40:27 UTC, Ferhat Kurtulmuş wrote: On Wednesday, 23 December 2020 at 09:06:02 UTC, Godnyx wrote: [...] I didn't dive into your use case, but you should use static foreach in this case: void put(A...)(string prompt, A args) { static foreach (ulong i; 0..args.length) { if (typeof(args[i]).stringof == "string") printf("%s\n", args[i].toStringz); } } and better: void put(A...)(string prompt, A args) { static foreach (ulong i; 0..args.length) { if (is(typeof(args[i]) == string)) printf("%s\n", args[i].toStringz); } } This method works but I won't let me use arguments other than string. Ali gave a solution that will solve this limitation! See me reply below!
Re: Can I output strings using core.stdc.stdio?
On Wednesday, 23 December 2020 at 09:50:03 UTC, Ali Çehreli wrote: On 12/23/20 1:06 AM, Godnyx wrote: > for (ulong i = 0; i < args.length; i++) { > if (typeof(args[i]).stringof == "string") > printf("%s\n", args[i].toStringz); > } I replaced for with foreach and it worked (and I passed "prompt"). static foreach would work as well. import core.stdc.stdio : printf; import std.string : toStringz; void put(A...)(string prompt, A args) { foreach (arg; args) { if (typeof(arg).stringof == "string") printf("%s\n", arg.toStringz); } } void main() { string h = "World!"; string w = "World!"; put("prompt", h, w); } But it can get better: you don't want to compare typeof with "string" at run time. Instead, there shouldn't be any code generated for the non-string cases. Enter 'static if' and the 'is' expression to feel better. :) import core.stdc.stdio : printf; import std.string : toStringz; void put(A...)(string prompt, A args) { static foreach (arg; args) { static if (is (typeof(arg) == string)) { printf("%s\n", arg.toStringz); } } } void main() { string h = "World!"; string w = "World!"; put("prompt", h, w); } Ali Probably the best solution! It also let's me use types other than string! Example: put("Prompt:", "Hello, my age is: ", 19, true); tho still I can't print anything. This is the code: void put(A...)(string prompt, A args) { static foreach (ulong i; 0..args.length) { static if (is(typeof(arg) == string)) printf("%s\n", args[i].toStringz); static if (is(typeof(arg) == int)) printf("%i\n", args[i]); static if (is(typeof(arg) == bool)) { if (arg) printf("true"); else printf("false"); } } } void main() { put("Prompt:", "Hello, my age is: ", 19, true); } Any ideas?
Re: Can I output strings using core.stdc.stdio?
On Wednesday, 23 December 2020 at 08:50:50 UTC, Mike Parker wrote: On Wednesday, 23 December 2020 at 08:45:15 UTC, Godnyx wrote: Yep and I find it out! It won't work with templates and/or variadic function parameters. It says that the variable can't be read at compile time (so I can't cast it) or it will work but it will give me a segmentation fault (lol hello C). Any idea why this is happening in those cases? Please show the code that's causing the error. Without it, all anyone can do is keep making suggestions that *might* be the problem. With the code, someone can point to it exactly. Yep that's the best thing I can do! Code: import core.stdc.stdio : printf; import std.string : toStringz; void put(A...)(string prompt, A args) { for (ulong i = 0; i < args.length; i++) { if (typeof(args[i]).stringof == "string") printf("%s\n", args[i].toStringz); } } void main() { string h = "World!"; string w = "World!"; put(h, w); } I'm getting two errors. First that i can't be read at compile time and second that I don't initialize the function right. So I know I'm doing something wrong but I don't know why... Any ideas?
Re: Can I output strings using core.stdc.stdio?
On Wednesday, 23 December 2020 at 04:02:54 UTC, Paul Backus wrote: On Tuesday, 22 December 2020 at 21:26:37 UTC, Godnyx wrote: On Tuesday, 22 December 2020 at 21:10:59 UTC, Godnyx wrote: Is there a way? If not then how std.stdio does it? I should mention that I want to use it in a variable that can't be read at compile time so .toStringz is not working for me. toStringz works just fine on variables that can't be read at compile time. You must be doing something else to trigger that error. Yep and I find it out! It won't work with templates and/or variadic function parameters. It says that the variable can't be read at compile time (so I can't cast it) or it will work but it will give me a segmentation fault (lol hello C). Any idea why this is happening in those cases?
Re: Can I output strings using core.stdc.stdio?
On Tuesday, 22 December 2020 at 21:40:15 UTC, Dave P. wrote: On Tuesday, 22 December 2020 at 21:37:23 UTC, Godnyx wrote: On Tuesday, 22 December 2020 at 21:28:10 UTC, Dave P. wrote: On Tuesday, 22 December 2020 at 21:10:59 UTC, Godnyx wrote: [...] Lol. Actually I just don't want to use Phobos and trying to stay on core. Unfortunately, my variable can't be read at compile time so I doesn't work. Any other ideas? What I wrote should still work in non-betterC as long as you are linking to libc. You can also just write to stdout directly if all you need is to write the string without any extra formatting: fwrite(somestring.ptr, 1, somestring.length, stdout); They is another problem. See the reply I did to Paul Backus
Re: Can I output strings using core.stdc.stdio?
On Tuesday, 22 December 2020 at 21:28:10 UTC, Dave P. wrote: On Tuesday, 22 December 2020 at 21:10:59 UTC, Godnyx wrote: Is there a way? If not then how std.stdio does it? I assume you’re asking this because you don’t have access to std.stdio (such as using betterC). The way to do it is to use the %.*s specifier in printf. For example: void print_string(string text){ printf(“%.*s\n”, cast(int)text.length, text.ptr); } The ‘.N' in front of the ’s’ says to not print more than N characters from the char*. using a ‘*’ says that the actual number of characters will be passed as an argument to printf instead of a hardcoded number. This is specified to be an int, so we have to cast the length of the string to int when calling printf. Finally, we need to pass the pointer to the actual character data, thus the text.ptr. Lol. Actually I just don't want to use Phobos and trying to stay on core. Unfortunately, my variable can't be read at compile time so I doesn't work. Any other ideas?
Re: Can I output strings using core.stdc.stdio?
On Tuesday, 22 December 2020 at 21:10:59 UTC, Godnyx wrote: Is there a way? If not then how std.stdio does it? I should mention that I want to use it in a variable that can't be read at compile time so .toStringz is not working for me.
Can I output strings using core.stdc.stdio?
Is there a way? If not then how std.stdio does it?
Re: C++ or D?
On Thursday, 12 November 2020 at 09:35:10 UTC, hgriffin wrote: C++ is a really overloaded with features language. The burden of backward compatibility and source compatibility with C doesn't make it any better. But right now it's the only right choice for development. There are plenty of libraries for many common tasks, a big community and the most important thing - C++ is evolving. Evolution is not as fast as we would want, but it's here. On the other side, D is really pleasant to work with, it has many good features and it can be a really convenient tool for small projects. But at the same time it's just not mature and not suitable for serious development. If you choose it, you'll face with lots of issues without a solution. Nobody from the D community would help you other than "just don't use const", "we haven't developed a concensus yet", "we can't convince Walter", etc. You can look at the D's evolution history and approximate it into the future. Years go by, nothing's changing. It's stagnating. The biggest D's problem is poor management and it's not going to change in any foreseeable time. Hi! Can you be more specific about the problems someone is gonna face with D that can't be fixed? This is very important for me because I'm planning to use D for development in the near (I wish near) future and I want to know what's going on. So yeah some examples will be appreciated!
Re: Can I convert string to expression somehow?
On Saturday, 12 December 2020 at 11:03:06 UTC, Tobias Pankrath wrote: On Saturday, 12 December 2020 at 09:05:19 UTC, Godnyx wrote: I'm trying to create a cool function that will let us do formatting sorter and faster. The function will work like that: outln("My name is {name} and my age is {age}"); this will be equivalent to: writeln("My name is ", name, " and my age is ", age); or: writefln("My name is %s and my age is %d", name, age); There was a DIP to bring something akin to this into the language, but there were also some decent counter proposals. See for example here: http://dpldocs.info/this-week-in-d/Blog.Posted_2019_12_16.html Thanks for your time man. I'm gonna check it out! Have a great day!
Can I convert string to expression somehow?
I'm trying to create a cool function that will let us do formatting sorter and faster. The function will work like that: outln("My name is {name} and my age is {age}"); this will be equivalent to: writeln("My name is ", name, " and my age is ", age); or: writefln("My name is %s and my age is %d", name, age); You can see how much sorter and faster this is and how better it looks. This is actually the way Rust does it for anyone that happen to know. So I'm trying to find a way to convert the strings inside the curly braces so I can print them. In our example the function must do write("My name is "); write(name); write("and my age is "); write(age); So yeah I want a way to convert the strings into expressions if that's possible. I also tried mixins but the variable cannot read at compile time so I'm out of luck...