[Issue 3546] Aliasing an element of a static array should be legal if the index is a compile time constant

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jun 14 19:22:13 UTC 2020


https://issues.dlang.org/show_bug.cgi?id=3546

Basile-z <b2.temp at gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |b2.temp at gmx.com

--- Comment #4 from Basile-z <b2.temp at gmx.com> ---
The feature cannot be made using language tricks. What is requested here is
really a function, i.e an expression to read or write from an offset. This is
simply not supported by AliasDeclaration abd would require to add stuff in
AliasDeclaration.

(while simply aliasing a ref getter will give good code gen with LDC2).

Technically the AST node for alias would require to store an additional
IntegerExp, do some type check to see if building the IndexExp would work and
then replace each use of the aliased symbol by an IndexExp.

When you alias an element of a compile time sequence, the element is solved at
compile time and gives a symbol. For the ER, the element is not a symbol, it's
an offset + a symbol.

Actually, I think this is feasible but not worth, just use a function. Also it
is specified that alias cant give exp. supporting one special case could open
the Pandora box...

more funny attempts to make it works with typecons/meta-like stuff:

1. try to turn elements as symbol and to index the new symbol...
---
int[2] sta = [0, 1];   // works for static immutable / enum  STC
auto staSeq = aliasSeqOf!sta;
alias sta1 = staSeq[1];
---

2. try to make explicit symbols as member of a union, in parallel of the static
array.
---
struct AliasableStaticArray(T)
if (isStaticArray!T)
{
    alias E = ElementType!T;
    private static string getCodeForElements()
    {
        string result = "struct {";
        static foreach (i; 0 .. T.length)
        {
            result ~= E.stringof ~ " element" ~ i.to!string ~ "; ";
        }
        result ~= "} ";
        return result;
    }
    union
    {
        T array;
        mixin(getCodeForElements());
    }
    alias array this;
}

void main(string[] args)
{
    AliasableStaticArray!(int[2]) asa;
    alias e00 = asa.element0;
    alias e10 = asa.element1;
    alias e01 = __traits(getMember, asa, "element0");
    alias e11 = __traits(getMember, asa, "element1");
    e00 = 42;             // error, need this...
    e10 = 1337;           // error, need this... 
    e01 = 42;             // error, need this... 
    e11 = 1337;           // error, need this...
    assert(asa == [42, 1337]);
}
---

verdict: impossible.

--


More information about the Digitalmars-d-bugs mailing list