www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Automatic Inference for Both Key and Value Types of an Associative

reply "Meta" <jared771 gmail.com> writes:
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.
Aug 07 2014
next sibling parent "Meta" <jared771 gmail.com> writes:
On Thursday, 7 August 2014 at 16:07:38 UTC, Meta wrote:
 Something H.S. Teoh in a recent pull request
Something H.S. Teoh *said* in a recent pull request
Aug 07 2014
prev sibling next sibling parent reply "Daniel Murphy" <yebbliesnospam gmail.com> writes:
"Meta"  wrote in message news:ohzzgcslkthrozimfiwd forum.dlang.org...

 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:
How often do you actually want to do this? At best it's a bunch of weird partial type special cases in the compiler.
Aug 07 2014
parent reply "Meta" <jared771 gmail.com> writes:
On Thursday, 7 August 2014 at 17:37:54 UTC, Daniel Murphy wrote:
 "Meta"  wrote in message 
 news:ohzzgcslkthrozimfiwd forum.dlang.org...

 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:
How often do you actually want to do this? At best it's a bunch of weird partial type special cases in the compiler.
Isn't that also the case for the auto*, const[], immutable[$], etc. syntax? Conceptually it feels the same to me as Kenji's enhancement; I don't know enough about the compiler to talk about it technically.
Aug 07 2014
next sibling parent "Daniel Murphy" <yebbliesnospam gmail.com> writes:
"Meta"  wrote in message news:uvtdfndzaiamyvtkbjrq forum.dlang.org... 

 Isn't that also the case for the auto*, const[], immutable[$], 
 etc. syntax? Conceptually it feels the same to me as Kenji's 
 enhancement; I don't know enough about the compiler to talk about 
 it technically.
Yes, I have the same problem with that enhancement.
Aug 08 2014
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Meta:

 Isn't that also the case for the auto*, const[], immutable[$], 
 etc. syntax? Conceptually it feels the same to me as Kenji's 
 enhancement; I don't know enough about the compiler to talk 
 about it technically.
One difference between Kenji's enhancement and the AA type inference is that arr[$] length inference for arrays is useful in many situations and it helps avoid some bugs and make the code more DRY. So it's a different degree of usefulness. Bye, bearophile
Aug 14 2014
parent "Meta" <jared771 gmail.com> writes:
On Thursday, 14 August 2014 at 18:07:57 UTC, bearophile wrote:
 Meta:

 Isn't that also the case for the auto*, const[], immutable[$], 
 etc. syntax? Conceptually it feels the same to me as Kenji's 
 enhancement; I don't know enough about the compiler to talk 
 about it technically.
One difference between Kenji's enhancement and the AA type inference is that arr[$] length inference for arrays is useful in many situations and it helps avoid some bugs and make the code more DRY. So it's a different degree of usefulness. Bye, bearophile
Kenji also implemented functionality to allow all of the following: auto[] ai = new int[](3); const* ci = new int(1); immutable[$] = [1, 2, 3];
Aug 14 2014
prev sibling next sibling parent reply Kenji Hara via Digitalmars-d <digitalmars-d puremagic.com> writes:
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 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.
Aug 15 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Kenji Hara:

 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
Looks nice. Thank you. D type system will slowly get better and better. Bye, bearophile
Aug 15 2014
prev sibling parent "Meta" <jared771 gmail.com> writes:
On Friday, 15 August 2014 at 08:34:22 UTC, Kenji Hara via 
Digitalmars-d wrote:
 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
Neat. I see as well that you thought of a case that I didn't: auto[shared const] aa4 = [1:1, 2:2] I think this partial type inference is a very interesting feature.
Aug 15 2014
prev sibling parent Philippe Sigaud via Digitalmars-d <digitalmars-d puremagic.com> writes:
Kenji Hara:
 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]]));
It's wonderful! I like it a lot. As for Walter's question about the syntax, I quite like the [$] thingie. '$' is already associated with 'length' in my mind.
Aug 15 2014