Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6916289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:34:59+00:00 2026-05-27T09:34:59+00:00

(for the D programming language) I’ve been struggling trying to initialise an associative array

  • 0

(for the “D” programming language)

I’ve been struggling trying to initialise an associative array that has struct elements and should be index-able by a string. I would import it as a module from a separate file.

This is what I want to achieve (and it doesn’t work — I don’t know if this is even possible):

mnemonic_info[string] mnemonic_table = [
        /* name,         format,          opcode */
        "ADD": {mnemonic_format.Format3M, 0x18},
        ...

        /* NOTE: mnemonic_format is an enum type. */
        /* mnemonic_info is a struct with a mnemonic_format and an ubyte */
];

Note that this works fine for arrays indexable by integers.

Optimally, I would like this to be evaluated at compile-time, as I won’t be changing it. However, if it’s not possible, I would be glad if you told me of the best way to build such an array at/before immediate run-time.

I need this because I’m writing an assembler.

I have searched SO and the internets for an answer, but could only find examples with integers, and other things I didn’t understand or couldn’t make to work.

I really like D so far but it seems hard to learn due to there not being many tutorials online.

Thanks!

On a side note: is it possible to use Tuples for associative array elements instead of a custom struct?

Edit

There is one way I found so far, but it’s pretty ugly:

mnemonic_info[string] mnemonic_table;
static this() { // Not idea what this does.
        mnemonic_info entry;

        entry.format = mnemonic_format.Format3M;
        entry.opcode = 0x18;
        mnemonic_table["ADD"] = entry;

        /* ... for all entries. */
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:35:00+00:00Added an answer on May 27, 2026 at 9:35 am

    In D, built-in associative array literals are always created in runtime, so initializing a global associative array by assigning it some value at declaration place is currently impossible.

    As you found yourself, you can workaround that by assigning a value to associative array in module constructor.

    The other problem in your code is struct initialization literals. You should prefer D-style struct initializers to C-style ones.

    Example:

    struct Foo {
        int a;
        string b;
    }
    
    Foo[string] global;
    
    static this() {
        global = [
            "foo" : Foo(1, "hurr"),
            "bar" : Foo(2, "durr")
        ];
    }
    
    void main() {
        assert(global["foo"].a == 1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every programming language I have ever seen has been based on the Latin alphabet,
C programming language is known as a zero index array language. The first item
I know that each programming language has certain guideline and styles. My question is
Programming language books usually explain that value types are created on the stack, and
In The C++ Programming Language, Bjarne writes that the null pointer is not the
Which programming language is it easier to support developers in (namely developers that have
Is there a programming language that uses inflections (suffixing a word to add a
What programming language has short and beautiful grammars (in EBNF)? Some languages are easer
Is there yet a programming language that will prevent XSS and SQL Injection. My
What is the best programming language for artificial intelligence purposes? Mind that using suggested

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.