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 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
Re: Can I output strings using core.stdc.stdio?
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]`
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 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
Re: Can I output strings using core.stdc.stdio?
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); } }
Re: Can I output strings using core.stdc.stdio?
On Wednesday, 23 December 2020 at 09:06:02 UTC, Godnyx wrote: 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? 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); } }
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 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.
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: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.
Re: Can I output strings using core.stdc.stdio?
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);
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 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.
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?