Automatic Inference for Both Key and Value Types of an Associative Array

Kenji Hara via Digitalmars-d digitalmars-d at puremagic.com
Fri Aug 15 01:34:12 PDT 2014


I implemented partial type deduction in AA keys.
https://github.com/D-Programming-Language/dmd/pull/3615

For example:
    auto[auto[$]] aa5 = [[1,2]:1, [3,4]:2];
    static assert(is(typeof(aa5) == int[int[2]]));

    int[int[][$]] aa15 = [[[1],[2]]:1, [[3],[4]]:2];
    static assert(is(typeof(aa15) == int[int[][2]]));

Kenji Hara



2014-08-08 1:07 GMT+09:00 Meta via Digitalmars-d <
digitalmars-d at puremagic.com>:

> 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:
>
> //typeof(aa) -> string[int]
> string[auto] aa = [1:"first", 2:"second"];
>
> //typeof(aa) -> char[int[]]
> auto[int[]] aa = [[1]:'a', [2]:'b'];
>
>
> 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:
>
>     // dynamic array type
>     immutable[] a4 = [1,2];    // immutable(int)[]
>     shared[]    a5 = [3,4,5];  // shared(int)[]
>     // partially specified part is unqualified.
>
>     // pointer type
>     auto*  p1 = new int(3);  // int*
>     const* p2 = new int(3);  // const(int)*
>
>
> 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:
>
> //typeof(aa) -> string[immutable(int)]
> string[immutable] aa = [1:"first", 2:"second"];
>
> //typeof(aa) -> immutable(char)[int[]]
> immutable[int[]] aa = [[1]:'a', [2]:'b'];
>
>
> A downside is that combining the two *could* lead to some type expressions
> that are very weird looking:
>
> //typeof(aa) -> const(int)[][immutable(char)]
> const[][immutable] aa = ['a':[1], 'b':[2]];
>
> //typeof(aa) -> const(const(int)[const(int)])
> const(const[const]) aa = [1:1, 2:2];
>
> //WTF???
> inout immutable[$][][const[$]][] aa;
>
>
> 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.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20140815/ce98df1e/attachment-0001.html>


More information about the Digitalmars-d mailing list