January 15, 2023

On Saturday, 14 January 2023 at 23:07:13 UTC, TheZipCreator wrote:

>

This is not the purpose mixin templates are meant to serve. They're for copying declarations into scopes (and as such only support declarations in them). Instead, I think what you want is

I'm trying to understand you. But I regret to say that I do not agree with you. You are a programmer like me, so you don't like this usage?

template MyContainer(string varS = "")
{
  struct Var
  {
    import std.variant;

    private
    Variant[string] values; alias
                    values this;
    @property {
      Variant opDispatch(string name)() const
      {
        return values[name];
      }

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

  static if(varS.length > 0)
  {
    import std.format;
    mixin(varS.format!"Var %s;");
  } else
    Var data;
}

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

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

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

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

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

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

  1/2023 in Turkish format
  Date: 2023/1

//*/
import std.stdio;
void otherTest(string str)
{
  mixin MyContainer;
  data.test = str;
  data.test.writeln;
}

I'm 44 and maybe an old-fashioned person who likes macros. But mixins are all kinds of beautiful yaw 😀

Because they can be used as easily as importing. Moreover, they can be personalized.

SDB@79

January 16, 2023
On 1/13/23 18:51, bauss wrote:

> That's a good one!

It looks like you liked it four years ago as well. :) I found where I remembered it from:

  https://forum.dlang.org/post/pvdoq2$1e7t$3@digitalmars.com

Ali

January 16, 2023
On Monday, 16 January 2023 at 08:17:24 UTC, Ali Çehreli wrote:
> On 1/13/23 18:51, bauss wrote:
>
>> That's a good one!
>
> It looks like you liked it four years ago as well. :) I found where I remembered it from:
>
>   https://forum.dlang.org/post/pvdoq2$1e7t$3@digitalmars.com
>
> Ali

Looks like my memory isn't as good, as I had totally forgotten about it, but perhaps because I haven't gotten to use it :)
1 2
Next ›   Last »