Sign in to follow this  

D language array syntax question

This topic is 826 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
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

This topic is 826 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this