Thread overview
static dictionary initialization
Nov 29, 2004
Tyro
Nov 29, 2004
Stewart Gordon
November 29, 2004
What is the correct way to statically initialize a dictionary?
I thought something like this would work but as you can see, it does not.

static uint[char[]] matrix =
[
"A2000", "A2001", "A2002", "A2003", "A2004", "A2005", "A2006", "A2007", "A2008",
"A2009",
"A2010", "A2011", "A2012", "A2013", "A2014", "A2015", "A2016", "A2017", "A2018",
"A2019",
"A2020", "A2021", "A2022", "A2023", "A2024", "A2025", "A2026", "A2027", "A2028",
"A2029",
"A2030", "A2031", "A2032", "A2033", "A2034", "A2035", "A2036", "A2037", "A2038",
"A2039",
"A2040", "A2041", "A2042", "A2043", "A2044", "A2045", "A2046", "A2047", "A2048",
"A2049",
"A2050", "A2051", "A2052", "A2053", "A2054", "A2055", "A2056", "A2057", "A2058",
"A2059",
"A2060", "A2061", "A2062", "A2063", "A2064", "A2065", "A2066", "A2067", "A2068",
"A2069",
"A2070", "A2071", "A2072", "A2073", "A2074", "A2075", "A2076", "A2077", "A2078",
"A2079",
"A2080", "A2081", "A2082", "A2083", "A2084", "A2085", "A2086", "A2087", "A2088",
"A2089",
"A2090", "A2091", "A2092", "A2093", "A2094", "A2095", "A2096", "A2097", "A2098",
"A2099"
];


Thanks,
Andrew


November 29, 2004
Tyro wrote:
> What is the correct way to statically initialize a dictionary?

There isn't one.  It's been brought up a few times:

http://www.digitalmars.com/drn-bin/wwwnews?D/26695

Meanwhile, you're stuck with populating it manually.  You can use a static this() function to do this.

> I thought something like this would work but as you can see, it does not.
> 
> static uint[char[]] matrix =
> [
> "A2000", "A2001", "A2002", "A2003", "A2004", "A2005", "A2006", "A2007", "A2008",
> "A2009",
<snip>

Of course it doesn't.  An AA needs two items of data for each entry: a key and a value.  By the looks of it, you've only given it the keys.

Stewart.

-- 
My e-mail is valid but not my primary mailbox.  Please keep replies on the 'group where everyone may benefit.
November 29, 2004
Stewart Gordon wrote:

>> I thought something like this would work but as you can see, it does not.
>>
>> static uint[char[]] matrix =
>> [
>> "A2000", "A2001", "A2002", "A2003", "A2004", "A2005", "A2006", "A2007", "A2008",
>> "A2009",
> 
> <snip>
> 
> Of course it doesn't.  An AA needs two items of data for each entry: a key and a value.  By the looks of it, you've only given it the keys.

If there *was* a way to initialize it, what would it look like ?

Something like this, perhaps ?

> static uint[char[]] matrix =
> [
>   "A2000" : 2000, "A2001" : 2001, "A2002" : 2002
> ];

That would make it consistent with dynamic arrays and structs...


Not sure what the hash representation looks like "behind the scenes",
maybe the D compiler even has to generate code to do the above init ?

--anders