Jump to content
  • Advertisement
Sign in to follow this  
noatom

D language array syntax question

This topic is 1042 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ulong[][uint] _IpIdCache;
_IpIdCache[date] = [record.IpId];

//record.IpId is just an unsigned long
//date is an unsigned int

So, _IpIdCache is an associative array that contains arrays of type ulong, indexed by uints.

 

I'm not entirely sure what is happening here:

_IpIdCache[date] = [record.IpId];

 

is record.IpId, turned into an array with a single element, and then added to _IpIdCache, with the index of date? Or did I get that entirely wrong?

 

Share this post


Link to post
Share on other sites
Advertisement
Yes, that's exactly right. As this program demonstrates.
 
void main() {
    import std.stdio : writeln;

    ulong[][uint] cache;
    cache[10] = [10UL];
    cache[100] = [30UL, 40UL, 50UL];

    foreach(key, val; cache) {
        foreach(ul; val) {
            writeln(ul);
        }
    }
}
Output:
30
40
50
10


The [items] syntax is an array literal. It creates a dynamic array on the heap, the elements of which are whatever inside the brackets.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!