<div dir="ltr">I implemented partial type deduction in AA keys.<div><a href="https://github.com/D-Programming-Language/dmd/pull/3615">https://github.com/D-Programming-Language/dmd/pull/3615</a><br></div><div><br></div><div>
For example:</div><div><div>    auto[auto[$]] aa5 = [[1,2]:1, [3,4]:2];</div><div><div>    static assert(is(typeof(aa5) == int[int[2]]));</div></div><div><br></div><div>    int[int[][$]] aa15 = [[[1],[2]]:1, [[3],[4]]:2];</div>
<div>    static assert(is(typeof(aa15) == int[int[][2]]));</div></div><div><br></div><div>Kenji Hara</div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2014-08-08 1:07 GMT+09:00 Meta via Digitalmars-d <span dir="ltr"><<a href="mailto:digitalmars-d@puremagic.com" target="_blank">digitalmars-d@puremagic.com</a>></span>:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Something H.S. Teoh in a recent pull request in Github got me thinking that it would be useful in some cases to tell the compiler that you want to automatically infer the either the key type or value type of an AA. Something like the following:<br>

<br>
//typeof(aa) -> string[int]<br>
string[auto] aa = [1:"first", 2:"second"];<br>
<br>
//typeof(aa) -> char[int[]]<br>
auto[int[]] aa = [[1]:'a', [2]:'b'];<br>
<br>
<br>
Of course, if you want to have both inferred, just use plain 'auto'. This is similar to Kenji's recent compiler changes to accept things like:<br>
<br>
    // dynamic array type<br>
    immutable[] a4 = [1,2];    // immutable(int)[]<br>
    shared[]    a5 = [3,4,5];  // shared(int)[]<br>
    // partially specified part is unqualified.<br>
<br>
    // pointer type<br>
    auto*  p1 = new int(3);  // int*<br>
    const* p2 = new int(3);  // const(int)*<br>
<br>
<br>
I think the 'immutable[]'/'const[]' type expressions could work similarly for AAs, since they are storage classes just like 'auto'. As per my first example:<br>
<br>
//typeof(aa) -> string[immutable(int)]<br>
string[immutable] aa = [1:"first", 2:"second"];<br>
<br>
//typeof(aa) -> immutable(char)[int[]]<br>
immutable[int[]] aa = [[1]:'a', [2]:'b'];<br>
<br>
<br>
A downside is that combining the two *could* lead to some type expressions that are very weird looking:<br>
<br>
//typeof(aa) -> const(int)[][immutable(char)]<br>
const[][immutable] aa = ['a':[1], 'b':[2]];<br>
<br>
//typeof(aa) -> const(const(int)[const(int)])<br>
const(const[const]) aa = [1:1, 2:2];<br>
<br>
//WTF???<br>
inout immutable[$][][const[$]][] aa;<br>
<br>
<br>
Is this a good idea? I don't have the skill to implement it currently, but I wanted to throw it out there for comments and see what people think.<br>
</blockquote></div><br></div>