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

Here is another working variation of Ali's code, try it yourself:

import std.stdio;
import std.conv;

struct MyVal
{
    string value;
    T opCast(T)() { 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;

    writeln(MyInt, ", ", myFloat);
}

In general, all type casting and parsing stuff usually exist under std.conv in the standard library, so you can take advantage of it.

March 24

On Friday, 24 March 2023 at 09:39:00 UTC, Jacob Shtokolov 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?

BTW, you can also alias this your struct value and then use std.conv : to for casting, if you don't need specific casting rules.

March 24

On Friday, 24 March 2023 at 09:46:26 UTC, Jacob Shtokolov wrote:

>

BTW, you can also alias this your struct value and then use std.conv : to for casting, if you don't need specific casting rules.

I don't quite understand what you mean? Could you show me an example?

March 24

On Friday, 24 March 2023 at 09:59:47 UTC, Alexander Zhirov wrote:

>

On Friday, 24 March 2023 at 09:46:26 UTC, Jacob Shtokolov wrote:

>

BTW, you can also alias this your struct value and then use std.conv : to for casting, if you don't need specific casting rules.

I don't quite understand what you mean? Could you show me an example?

I mean, it would be the same code except that you don't define any opCast or other operators for a struct, just alias the value to this and use std.conv : to directly as follows:

struct MyVal
{
    string value;
    alias value this;
}

void main()
{
    import std.stdio;
    import std.conv;

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

    auto MyInt = a.to!int;
    auto myFloat = b.to!float;

    writeln(MyInt, ", ", myFloat);
}
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

You're limited by the use of int and float. It works just fine for structs:

struct MyInt {
    int x;
    alias x this;

    this(MyString s) {
        x = s.to!int;
    }

    void opAssign(MyString s) {
        x = s.to!int;
    }
}

struct MyFloat {
    float x;
    alias x this;

    this(MyString s) {
        x = s.to!float;
    }

    void opAssign(MyString s) {
        x = s.to!float;
    }
}

struct MyString {
    string s;
    alias s this;
}

void main() {
    auto ms = MyString("100");
    auto ms2 = MyString("11.2");
    MyInt mi = ms;
    MyFloat mf = ms2;
}
March 24

On Friday, 24 March 2023 at 13:53:02 UTC, bachmeier 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

You're limited by the use of int and float. It works just fine for structs:

struct MyInt {
    int x;
    alias x this;

    this(MyString s) {
        x = s.to!int;
    }

    void opAssign(MyString s) {
        x = s.to!int;
    }
}

struct MyFloat {
    float x;
    alias x this;

    this(MyString s) {
        x = s.to!float;
    }

    void opAssign(MyString s) {
        x = s.to!float;
    }
}

struct MyString {
    string s;
    alias s this;
}

void main() {
    auto ms = MyString("100");
    auto ms2 = MyString("11.2");
    MyInt mi = ms;
    MyFloat mf = ms2;
}

opAssign is not needed for this code to compile, but it would be if you had

MyInt mi;
mi = ms;
March 24

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

>
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

Have you tried using an associative array?

I feel that you will come up with a solution for your purpose from the examples below:

template MyContainer(string data = "")
{       // Container name ---^
  struct Var
  {
    import std.variant;

    private Variant[string] values;
    alias values this;

    @property
    {
      Variant opDispatch(string key)() const
      {
        return values[key];
      }

      void opDispatch(string key, T)(T val)
      {
        values[key] = val;
      }
    }
  }

  static if(data.length > 0)
  {
    import std.format;

    mixin(data.format!"Var %s;");

  } else {

    Var data; // no conflicts

  }
}

import std.stdio;

void main()
{
  enum Tarih
  {
    AY  = 1,
    YIL = 2023
  }
  mixin MyContainer!"date";

  date.month = cast(ubyte)Tarih.AY;
  date.month.write("/");

  assert(date["month"] != Tarih.AY);
  assert(date["month"].type == typeid(ubyte));

  date.year = cast(short)Tarih.YIL;
  date.year.writeln(" in Turkish format");

  assert(date["year"] != Tarih.YIL);
  assert(date["year"].type == typeid(short));

  writefln("Date: %s/%s", date.year, date.month);
}

SDB@79

1 2
Next ›   Last »