December 23, 2020
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);
    }
}
December 23, 2020
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);
    }
}
December 23, 2020
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

December 23, 2020
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?
December 23, 2020
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!
December 23, 2020
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]`

December 23, 2020
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
December 23, 2020
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!!! ;)
December 23, 2020
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!!!
1 2
Next ›   Last »