Jump to page: 1 2 3
Thread overview
CT-String as a Symbol
Apr 15, 2015
Nordlöw
Apr 15, 2015
Ali Çehreli
Apr 15, 2015
Nordlöw
Apr 15, 2015
Nordlöw
Apr 15, 2015
Nordlöw
Apr 15, 2015
Ali Çehreli
Apr 15, 2015
Nordlöw
Apr 15, 2015
Ali Çehreli
Apr 16, 2015
Per Nordlöw
Apr 16, 2015
Per Nordlöw
Apr 16, 2015
Per Nordlöw
Apr 16, 2015
John Colvin
Apr 16, 2015
Nordlöw
Apr 20, 2015
Nordlöw
Apr 20, 2015
ketmar
Apr 20, 2015
John Colvin
Apr 21, 2015
Per Nordlöw
Apr 21, 2015
Per Nordlöw
Apr 21, 2015
Vlad Levenfeld
Apr 21, 2015
Per Nordlöw
Apr 21, 2015
Vlad Levenfeld
Apr 21, 2015
Vlad Levenfeld
Apr 21, 2015
Nordlöw
Apr 21, 2015
Vlad Levenfeld
Apr 21, 2015
Vlad Levenfeld
Apr 21, 2015
Nordlöw
April 15, 2015
At

https://github.com/nordlow/justd/blob/master/typecons_ex.d#L143

I can't figure out how to make the call to

    mixin genOps!I;

expand to, for instance,

    mixin genOps!Index

in the case when I = "Index".

Help please.
April 15, 2015
On 04/15/2015 09:21 AM, "Nordlöw" wrote:
> At
>
> https://github.com/nordlow/justd/blob/master/typecons_ex.d#L143
>
> I can't figure out how to make the call to
>
>      mixin genOps!I;
>
> expand to, for instance,
>
>      mixin genOps!Index

This seems to work:

    mixin genOps!(mixin(I));

>
> in the case when I = "Index".
>
> Help please.

Ali

April 15, 2015
On Wednesday, 15 April 2015 at 17:03:36 UTC, Ali Çehreli wrote:
> This seems to work:
>
>     mixin genOps!(mixin(I));

I'm afraid not:

    mixin genOps!(mixin(I));

errors

typecons_ex.d(135,5): Error: mixin typecons_ex.IndexedBy!(int[3], "I").IndexedBy.genOps!"I" does not match template declaration genOps(T)
typecons_ex.d(152,12): Error: template instance typecons_ex.IndexedBy!(int[3], "I") error instantiating
typecons_ex.d(185,20):        instantiated from here: indexedBy!("I", int[3])
typecons_ex.d(202,9): Error: static assert  (!true) is false

April 15, 2015
On Wednesday, 15 April 2015 at 19:15:36 UTC, Nordlöw wrote:
> errors
>
> typecons_ex.d(135,5): Error: mixin typecons_ex.IndexedBy!(int[3], "I").IndexedBy.genOps!"I" does not match template declaration genOps(T)
> typecons_ex.d(152,12): Error: template instance typecons_ex.IndexedBy!(int[3], "I") error instantiating
> typecons_ex.d(185,20):        instantiated from here: indexedBy!("I", int[3])
> typecons_ex.d(202,9): Error: static assert  (!true) is false

Correction. This error is triggered if I put at the top of the defintion of IndexedBy(R, string I = "Index")

    mixin genOps!(mixin(I));

If I place after the definition of index struct I get another error

typecons_ex.d(143,19): Error: argument to mixin must be a string type, not I
typecons_ex.d(152,12): Error: template instance typecons_ex.IndexedBy!(int[3], "I") error instantiating
typecons_ex.d(185,20):        instantiated from here: indexedBy!("I", int[3])
typecons_ex.d(202,9): Error: static assert  (!true) is false

How is this possible?
April 15, 2015
I'm using DMD 2.067.
April 15, 2015
On 04/15/2015 12:19 PM, "Nordlöw" wrote:
> I'm using DMD 2.067.

Mee too. The following is the minimum amount of code I used, which compiles:

import std.traits;
import std.range;

struct Index(T = size_t) if (isUnsigned!T)
{
    this(T ix) { this._ix = ix; }
    T opCast(U : T)() const { return _ix; }
    private T _ix = 0;
}

mixin template genOps(T)
{
    auto ref opIndex(T i) inout { return _r[cast(size_t)i]; }
    auto ref opIndexAssign(V)(V value, T i) { return _r[cast(size_t)i] = value; }
    static if (hasSlicing!R)
    {
        auto ref opSlice(T i, T j) inout { return _r[cast(size_t)i ..
                                                     cast(size_t)j]; }
        auto ref opSliceAssign(V)(V value, T i, T j) { return _r[cast(size_t)i ..

cast(size_t)j] = value; }
    }
}

struct IndexedBy(R, string I = "Index") if (isArray!R)
{
    mixin(q{ struct } ~ I ~
          q{ {
                  alias T = size_t;
                  this(T ix) { this._ix = ix; }
                  T opCast(U : T)() const { return _ix; }
                  private T _ix = 0;
              }
          });
    mixin genOps!(mixin(I));
    R _r;
    alias _r this; // TODO Use opDispatch instead; to override only opSlice and opIndex
}

void main()
{
    auto i = IndexedBy!(int[])();
}

Ali
April 15, 2015
On Wednesday, 15 April 2015 at 20:12:27 UTC, Ali Çehreli wrote:
> Ali

I cracked it:

Reason: I hade a failing unittest using it as

    auto xs = x.indexedBy!"I";

which I changed to

    auto xs = x.indexedBy!"Ix";

For some reason the mixin magic becomes confused when I equals "I".

Do you have any clues as to why?

Should we restrict IndexedBy to not allow I to equal "I"?

:)

Thx anyway.
April 15, 2015
On 4/15/15 6:42 PM, "Nordlöw" wrote:
> On Wednesday, 15 April 2015 at 20:12:27 UTC, Ali Çehreli wrote:
>> Ali
>
> I cracked it:
>
> Reason: I hade a failing unittest using it as
>
>      auto xs = x.indexedBy!"I";
>
> which I changed to
>
>      auto xs = x.indexedBy!"Ix";
>
> For some reason the mixin magic becomes confused when I equals "I".
>
> Do you have any clues as to why?
>
> Should we restrict IndexedBy to not allow I to equal "I"?
>
> :)
>
> Thx anyway.


Hm.. from that link you sent, look up a bit, you see this interesting tidbit:

struct IndexedBy(R, string I_ = "Index") if (isArray!R && I_ != "I_") // prevent strange bug from occurring

That comment seems to be similar to what you are talking about...

Later, there's this:

auto indexedBy(string I, R)(R range) if (isArray!R && I != "I_") // prevent strange bug from occurring

Should that be change to "string I_" also?

-Steve
April 15, 2015
On 04/15/2015 03:42 PM, "Nordlöw" wrote:

> On Wednesday, 15 April 2015 at 20:12:27 UTC, Ali Çehreli wrote:
>> Ali
>
> I cracked it:
>
> Reason: I hade a failing unittest using it as
>
>      auto xs = x.indexedBy!"I";
>
> which I changed to
>
>      auto xs = x.indexedBy!"Ix";
>
> For some reason the mixin magic becomes confused when I equals "I".
>
> Do you have any clues as to why?

That is because you have a nested struct named I inside IndexedBy. :) I print it with pragma(msg) below. So, mixin(I) is not happy because it thinks you mean 'struct I' :)

struct IndexedBy(R, string I = "Index") if (isArray!R)
{
    enum toMixin = q{ struct } ~ I ~
          q{ {
                  alias T = size_t;
                  this(T ix) { this._ix = ix; }
                  T opCast(U : T)() const { return _ix; }
                  private T _ix = 0;
              }
          };
    pragma(msg, toMixin);
    mixin(toMixin);

    mixin genOps!(mixin(I));
    R _r;
    alias _r this; // TODO Use opDispatch instead; to override only opSlice and opIndex
}

void main()
{
    auto i = IndexedBy!(int[], "I")();
}

The output:

 struct I {
                  alias T = size_t;
                  this(T ix) { this._ix = ix; }
                  T opCast(U : T)() const { return _ix; }
                  private T _ix = 0;
              }
> Should we restrict IndexedBy to not allow I to equal "I"?

I don't think there is a general solution but you can always disallow as a template constraint or as a static assert:

struct IndexedBy(R, string I = "Index")
    if (isArray!R &&
        I != "I")    // <-- ADDED
{
    // ...
}

>
> :)
>
> Thx anyway.

Ali

April 16, 2015
On Wednesday, 15 April 2015 at 23:02:32 UTC, Ali Çehreli wrote:
>  struct I {
>                   alias T = size_t;
>                   this(T ix) { this._ix = ix; }
>                   T opCast(U : T)() const { return _ix; }
>                   private T _ix = 0;
>               }

How is this possible? Shouldn't it CT-evaluate to

    struct Index { ... }

!?
« First   ‹ Prev
1 2 3