digitalmars.D - Automatic Inference for Both Key and Value Types of an Associative
- Meta (36/36) Aug 07 2014 Something H.S. Teoh in a recent pull request in Github got me
- Meta (2/3) Aug 07 2014 Something H.S. Teoh *said* in a recent pull request
- Daniel Murphy (3/7) Aug 07 2014 How often do you actually want to do this? At best it's a bunch of weir...
- Meta (5/13) Aug 07 2014 Isn't that also the case for the auto*, const[], immutable[$],
- Daniel Murphy (2/6) Aug 08 2014 Yes, I have the same problem with that enhancement.
- bearophile (7/11) Aug 14 2014 One difference between Kenji's enhancement and the AA type
- Meta (6/17) Aug 14 2014 Kenji also implemented functionality to allow all of the
- Kenji Hara via Digitalmars-d (10/44) Aug 15 2014 I implemented partial type deduction in AA keys.
- bearophile (5/13) Aug 15 2014 Looks nice. Thank you.
- Meta (5/13) Aug 15 2014 Neat. I see as well that you thought of a case that I didn't:
- Philippe Sigaud via Digitalmars-d (4/11) Aug 15 2014 It's wonderful! I like it a lot.
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
On Thursday, 7 August 2014 at 16:07:38 UTC, Meta wrote:Something H.S. Teoh in a recent pull requestSomething H.S. Teoh *said* in a recent pull request
Aug 07 2014
"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
On Thursday, 7 August 2014 at 17:37:54 UTC, Daniel Murphy wrote:"Meta" wrote in message news:ohzzgcslkthrozimfiwd 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.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
"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
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
On Thursday, 14 August 2014 at 18:07:57 UTC, bearophile wrote:Meta:Kenji also implemented functionality to allow all of the following: auto[] ai = new int[](3); const* ci = new int(1); immutable[$] = [1, 2, 3];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
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
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 HaraLooks nice. Thank you. D type system will slowly get better and better. Bye, bearophile
Aug 15 2014
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 HaraNeat. 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
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