Sooo, when are we implementing this? 😁
Thread overview | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 10, 2021 Associative arrays | ||||
---|---|---|---|---|
| ||||
May 10, 2021 Re: Associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Imperatorn | On Mon, May 10, 2021 at 05:31:21PM +0000, Imperatorn via Digitalmars-d wrote: > Sooo, when are we implementing this? 😁 > > https://dlang.org/spec/hash-map.html#static_initialization This has been on the list for many years, and I haven't seen any signs of it manifesting any time soon. In the meantime, you could simply do this: static immutable K[V] myAA; static this() { myAA = [ "abc": 123, ... // etc. ]; } T -- If you think you are too small to make a difference, try sleeping in a closed room with a mosquito. -- Jan van Steenbergen |
May 18, 2021 Re: Associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Monday, 10 May 2021 at 18:31:20 UTC, H. S. Teoh wrote:
> This has been on the list for many years, and I haven't seen any signs of it manifesting any time soon.
Hi H. S.
I assume it's not been done because it's hard. Do you happen to know what makes it a difficult problem for compiler implementers? I don't need to know, but am curious.
Thanks,
|
May 18, 2021 Re: Associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Piker | On Tuesday, 18 May 2021 at 07:40:53 UTC, Chris Piker wrote:
> On Monday, 10 May 2021 at 18:31:20 UTC, H. S. Teoh wrote:
>> This has been on the list for many years, and I haven't seen any signs of it manifesting any time soon.
>
> Hi H. S.
>
> I assume it's not been done because it's hard. Do you happen to know what makes it a difficult problem for compiler implementers? I don't need to know, but am curious.
I hope it stays unimplemented. AAs should be replaced by a library solution and AA literals should work with custom AAs. The special casing is not good for metaprogramming.
|
May 18, 2021 Re: Associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ola Fosheim Grostad | On Tuesday, 18 May 2021 at 07:50:06 UTC, Ola Fosheim Grostad wrote:
> I hope it stays unimplemented. AAs should be replaced by a library solution and AA literals should work with custom AAs. The special casing is not good for metaprogramming.
I'm new to D and don't come from a hard-core C++ background. On naive first take, the fact that D had a construct resembling python dictionaries made D more attractive. In fact, it was AAs that finally tipped me in favor of trying D.
Can you tell me more about why AAs are an undesirable language feature?
|
May 18, 2021 Re: Associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Piker | On Tuesday, 18 May 2021 at 08:08:10 UTC, Chris Piker wrote:
> On Tuesday, 18 May 2021 at 07:50:06 UTC, Ola Fosheim Grostad wrote:
>> I hope it stays unimplemented. AAs should be replaced by a library solution and AA literals should work with custom AAs. The special casing is not good for metaprogramming.
>
> I'm new to D and don't come from a hard-core C++ background. On naive first take, the fact that D had a construct resembling python dictionaries made D more attractive. In fact, it was AAs that finally tipped me in favor of trying D.
>
> Can you tell me more about why AAs are an undesirable language feature?
Hashing strategies needs contextual knowledge. But that is not the main reason. The main reason is that D needs better metaprogramming. A hashmap literal should bind to any hashtable. There is no reason to not make it a 100% library construct. Stuff it into your runtime and you would hardly notice any difference.
Golden design rule: never add language features that can be done as library constructs, ever.
|
May 18, 2021 Re: Associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Piker | On Tuesday, 18 May 2021 at 08:08:10 UTC, Chris Piker wrote: >On Tuesday, 18 May 2021 at 07:50:06 UTC, Ola Fosheim Grostad wrote: >I hope it stays unimplemented. AAs should be replaced by a library solution and AA literals should work with custom AAs. The special casing is not good for metaprogramming. I'm new to D and don't come from a hard-core C++ background. On naive first take, the fact that D had a construct resembling python dictionaries made D more attractive. In fact, it was AAs that finally tipped me in favor of trying D. Can you tell me more about why AAs are an undesirable language feature? AA are definitely a great D feature IMO. But unlike other D features, they are not as composable as we'd like. Take slices for example. They compose fairly well. You can get an Associative arrays have none of that. They are tied to the way they've been implemented in the runtime, and predate a lot of features. They only work with qualifiers because of the C interfacing. You can't control the underlying memory, which means you can't reuse it or bind it to a buffer if you know the AA size in advance. They don't work well with qualifiers, because the compiler will error on The following doesn't even compile:
Because: >onlineapp.d(4): Error: Now as to why AA don't work at CT: They still use the "old" druntime approach, namely hiding all runtime magic behind There's been a few discussions about this over the year. For example, this PR from a few years ago wanted to at least allow builtin types. @IgorStepanov did some work, e.g. here and here (and more). Martin Nowak IIRC also did quite some work on it. |
May 18, 2021 Re: Associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ola Fosheim Grostad | On Tuesday, 18 May 2021 at 08:22:56 UTC, Ola Fosheim Grostad wrote: >The main reason is that D needs better metaprogramming. That's an interesting take. So far, with only a month's usage of D I'm seeing so much meta-programming capability that it actually worries me a bit. With string mixins and so many other meta programming features around I'm starting to think that reading other people's D code is going to be quite difficult, similar to perl. >Golden design rule: never add language features that can be done as library constructs, ever. This sounds like a reasonable position. Imagine for a second that AAs were implemented as a library. What would the library equivalent of this:
look like? |
May 18, 2021 Re: Associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Piker | On Tuesday, 18 May 2021 at 09:14:46 UTC, Chris Piker wrote: >This sounds like a reasonable position. Imagine for a second that AAs were implemented as a library. What would the library equivalent of this:
look like? The playground will show the call to |
May 18, 2021 Re: Associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mathias LANG | On Tuesday, 18 May 2021 at 08:54:00 UTC, Mathias LANG wrote: Thanks for the description, that was informative :) >The following doesn't even compile:
So basically I can use AAs, so long as I don't put them in critical software since they are always going to throw. No trouble, I can live with that, it's good to know that up front. |