Jump to page: 1 2
Thread overview
Implicit type conversion depending on assignment
Mar 23
user1234
Mar 23
user1234
Mar 23
apz28
Mar 24
zjh
Mar 24
bachmeier
Mar 24
bachmeier
March 23

Is it possible to convert such records inside the structure to the assigned type?

struct MyVal
{
    string value;
    // Here it would be possible to use an alias to this, but it can only be used 1 time
}

auto a = MyVal("100");
auto b = MyVal("11.2");

int MyInt = a;        // Implicitly convert to target type
float myFloat = b;    // Implicitly convert to target type
March 23

On Thursday, 23 March 2023 at 13:38:51 UTC, Alexander Zhirov wrote:

>

Is it possible to convert such records inside the structure to the assigned type?

struct MyVal
{
    string value;
    // Here it would be possible to use an alias to this, but it can only be used 1 time
}

auto a = MyVal("100");
auto b = MyVal("11.2");

int MyInt = a;        // Implicitly convert to target type
float myFloat = b;    // Implicitly convert to target type

Here is an example from the documentation, but for "several" types, for example, with a cast check and a return of the default value.

March 23

On Thursday, 23 March 2023 at 14:05:07 UTC, Alexander Zhirov wrote:

>

On Thursday, 23 March 2023 at 13:38:51 UTC, Alexander Zhirov wrote:

>

Is it possible to convert such records inside the structure to the assigned type?

struct MyVal
{
    string value;
    // Here it would be possible to use an alias to this, but it can only be used 1 time
}

auto a = MyVal("100");
auto b = MyVal("11.2");

int MyInt = a;        // Implicitly convert to target type
float myFloat = b;    // Implicitly convert to target type

Here is an example from the documentation, but for "several" types, for example, with a cast check and a return of the default value.

The best I can think of ATM

struct MyVal
{
    string value;
    auto opCast(T)()
    {
        import std.conv : to;
        return to!T(value);
    }
}

void main()
{
    auto a = MyVal("100");
    auto b = MyVal("11.2");

    int MyInt       = cast(int)a;
    float myFloat   = cast(float)b;
}

not exactly thing goal yet. The doc example you have put a link for is different, the struct with alias this a redefinition of the "alias this"'ed thing, that just cant work in what you ask in the first post.

March 23

On Thursday, 23 March 2023 at 14:17:25 UTC, user1234 wrote:

>

not exactly thing goal yet. The doc example you have put a link for is different, the struct with alias this a redefinition of the "alias this"'ed thing, that just cant work in what you ask in the first post.

omg, let's rewrite this...

not exactly the same thing as the goal yet. The doc example you have put a link
for is different, the struct with "alias this" is a redefinition of
the "alias this"'ed thing, that just cant work in what you ask in the first post because the type is different.

March 23

On Thursday, 23 March 2023 at 14:19:31 UTC, user1234 wrote:

>

omg, let's rewrite this...

I meant something like that. But you can't do that. I wanted WITHOUT explicit casting.

struct MyVal
{
    private string value;

    @property auto toString(T)()
    {
        return value.to!T;
    }

    alias toString this;
}

auto a = MyVal("100");
auto b = MyVal("11.2");

int myInt       = a;
float myFloat   = b;
March 23

On Thursday, 23 March 2023 at 14:36:11 UTC, Alexander Zhirov wrote:

>

I wanted WITHOUT explicit casting.

I also have thoughts about using templates, but I don't have enough experience yet how to implement it.

March 23
On 3/23/23 06:38, Alexander Zhirov wrote:
> Is it possible to convert such records inside the structure to the
> assigned type?

D does not embrace implicit conversions. There is some support like 'alias this' as you mentioned but what you are looking for is not possible.

Ali

March 23

On Thursday, 23 March 2023 at 14:36:11 UTC, Alexander Zhirov wrote:

>

On Thursday, 23 March 2023 at 14:19:31 UTC, user1234 wrote:

>

omg, let's rewrite this...

Or abuse opBinary

struct MyVal
{
    string value;
    T opBinary(string op, T)(T rhs)
    if (op == "+")
    {
        import std.conv : convto = to;
        static if (is(T==int))
            return convto!T(value) + rhs;
        else static if (is(T==float))
            return convto!T(value) + rhs;
        else
            static assert(0);
    }

    alias opBinary this;
}

void main()
{

auto a = MyVal("100");
auto b = MyVal("11.2");

int MyInt = a + 0;
float myFloat = b + 0.0f;
}

March 24

On Thursday, 23 March 2023 at 13:38:51 UTC, Alexander Zhirov wrote:

>

Is it possible to convert such records inside the structure to the assigned type?

struct MyVal
{
    string value;
    // Here it would be possible to use an alias to this, but it can only be used 1 time
}

auto a = MyVal("100");
auto b = MyVal("11.2");

int MyInt = a;        // Implicitly convert to target type
float myFloat = b;    // Implicitly convert to target type

However, rust andcpp are all can!
Here.

March 23
On 3/23/23 07:36, Alexander Zhirov wrote:

>      @property auto toString(T)()

The name is misleading because you want types other than string as well.

>      alias toString this;

That should have been a compilation error because 'toString' does not have a known type (because it depends on a template parameter). How can the compiler accept that 'alias this'?

> int myInt       = a;
> float myFloat   = b;

Since you need to spell out 'int' and 'float' in some form anyway, the following would be my choice, which I did use in my code before:

struct MyVal
{
    private string value;

    auto to(T)()
    {
        import std.conv : to;
        return value.to!T;
    }
}

void main() {
    auto a = MyVal("100");
    auto b = MyVal("11.2");

    auto myInt = a.to!int;
    auto myFloat = b.to!float;
}

Should it be an error to convert 'a' to float? If so, the following modules may be helpful:

  https://dlang.org/phobos/std_sumtype.html

  https://dlang.org/phobos/std_variant.html

Ali

« First   ‹ Prev
1 2