October 10, 2023

On Monday, 9 October 2023 at 22:49:11 UTC, Salih Dincer wrote:

>

On Monday, 9 October 2023 at 16:33:32 UTC, rempas wrote:

>

I'm trying to create a series of function. There will be ten of them, and they will be called function_0, function_1, etc. However, in my example, "stringof" returns the character "i" itself and turns that into a string instead of getting its actual value (number).

Any ideas how I can achieve what I'm trying to achieve?

Great masters generally warn to stay away from stringof. Please do not use it as much as possible. The following code snippet will be useful to you:

alias CN = __traits(allMembers, CardinalNumbers);

static foreach(i; CN)
{
  mixin(create_fn!(i[1]));
}

enum create_fn(char num) = `
  auto function_`~ num ~`()
    => "Hello from function `~ num ~`!";
`;

enum CardinalNumbers
{
  n0, n1, n2, n3, n4, n5, n6, n7, n8, n9
}

void main()
{
  assert(function_9() == "Hello from function 9!");
}

SDB@79

If count < 10 then why not just

import std;

    static foreach(c; "0123456789")
    {
      mixin(create_fn!(c));
    }

    enum create_fn(char num) = `
      auto function_`~ num ~`()
        => "Hello from function `~ num ~`!";
    `;

    void main()
    {
      assert(function_9() == "Hello from function 9!");
    }
October 10, 2023

On Monday, 9 October 2023 at 16:33:32 UTC, rempas wrote:

>

However, in my example, "stringof" returns the character "i" itself and turns that into a string instead of getting its actual value (number).

The result of .stringof is implementation defined, it can be used for debugging but don't make your program's semantics depend on the output of it.

...

...

...That being said, this trick can be used to convert an integer to string at compile time:

enum itoa(int i) = i.stringof;

static foreach(i; 0 .. 10) {
  mixin(create_fn!(itoa!i));
}

Technically not reliable, but I don't expect integers to ever get printed differently than a string of base 10 digits.

October 10, 2023

On Monday, 9 October 2023 at 18:25:15 UTC, rempas wrote:

>

On Monday, 9 October 2023 at 17:42:48 UTC, Imperatorn wrote:

>

You could just add your own int to string I guess?

That will be a good idea! I'll do it in the future if that is the case, as it's not important, and I want to finish my job. Thank you and have a great day!

My engine has its own implementation of toString(long), which does not have dependency with the C runtime:

https://github.com/MrcSnm/HipremeEngine/blob/master/modules/util/source/hip/util/conv.d#L180C1-L208C2

I have reimplemented the entire conv module since it is one of mostly used module and it pulled down a lot of things, so, with my util module I was able to make my program much smaller too.

October 10, 2023

On Monday, 9 October 2023 at 22:49:11 UTC, Salih Dincer wrote:

>

Great masters generally warn to stay away from stringof. Please do not use it as much as possible. The following code snippet will be useful to you:

alias CN = __traits(allMembers, CardinalNumbers);

static foreach(i; CN)
{
  mixin(create_fn!(i[1]));
}

enum create_fn(char num) = `
  auto function_`~ num ~`()
    => "Hello from function `~ num ~`!";
`;

enum CardinalNumbers
{
  n0, n1, n2, n3, n4, n5, n6, n7, n8, n9
}

void main()
{
  assert(function_9() == "Hello from function 9!");
}

SDB@79

Thank you so much! This will do the trick. Have a beautiful day!

October 10, 2023

On Tuesday, 10 October 2023 at 05:32:52 UTC, Imperatorn wrote:

>

If count < 10 then why not just

import std;

    static foreach(c; "0123456789")
    {
      mixin(create_fn!(c));
    }

    enum create_fn(char num) = `
      auto function_`~ num ~`()
        => "Hello from function `~ num ~`!";
    `;

    void main()
    {
      assert(function_9() == "Hello from function 9!");
    }

Thank you! Yeah, the most clean code wins for me, so I'll use yours (if it's open source, lol)! Thank you and have an amazing day!

October 10, 2023

On Tuesday, 10 October 2023 at 11:45:25 UTC, Dennis wrote:

>

The result of .stringof is implementation defined, it can be used for debugging but don't make your program's semantics depend on the output of it.

...

...

...That being said, this trick can be used to convert an integer to string at compile time:

enum itoa(int i) = i.stringof;

static foreach(i; 0 .. 10) {
  mixin(create_fn!(itoa!i));
}

Technically not reliable, but I don't expect integers to ever get printed differently than a string of base 10 digits.

Thank you! It is great and works great. I will however use the example from Imperatorn as it does not use ".stringof". Have an amazing day!

October 10, 2023

On Tuesday, 10 October 2023 at 11:46:38 UTC, Hipreme wrote:

>

My engine has its own implementation of toString(long), which does not have dependency with the C runtime:

https://github.com/MrcSnm/HipremeEngine/blob/master/modules/util/source/hip/util/conv.d#L180C1-L208C2

I have reimplemented the entire conv module since it is one of mostly used module and it pulled down a lot of things, so, with my util module I was able to make my program much smaller too.

Thank you for the idea! However, your code used Phobos which I don't want to use so it will not do.

October 10, 2023

On Tuesday, 10 October 2023 at 13:55:44 UTC, rempas wrote:

>

On Tuesday, 10 October 2023 at 11:46:38 UTC, Hipreme wrote:

>

My engine has its own implementation of toString(long), which does not have dependency with the C runtime:

https://github.com/MrcSnm/HipremeEngine/blob/master/modules/util/source/hip/util/conv.d#L180C1-L208C2

I have reimplemented the entire conv module since it is one of mostly used module and it pulled down a lot of things, so, with my util module I was able to make my program much smaller too.

Thank you for the idea! However, your code used Phobos which I don't want to use so it will not do.

Which part uses Phobos? The linked function compiles without importing anything.

October 13, 2023

On Tuesday, 10 October 2023 at 11:45:25 UTC, Dennis wrote:

>
enum itoa(int i) = i.stringof;

static foreach(i; 0 .. 10) {
  mixin(create_fn!(itoa!i));
}

You can also do it using a string mixin:

mixin(create_fn!(mixin("`", i, "`")));

I think that's equivalent to i.stringof anyway.

October 14, 2023

On Tuesday, 10 October 2023 at 16:11:57 UTC, bachmeier wrote:

>

Which part uses Phobos? The linked function compiles without importing anything.

Actually, you are right. I didn't give a lot of thought to it, as there is the line char[] ret = new char[](length); but I can replace it with an external buffer. Anyway, I'm not trying to do that anymore, and I will also create my own int_to_string method in the future but thanks for pointing it out!