Associative arrays give compile error

bearophile bearophileHUGS at lycos.com
Tue Oct 5 09:48:05 PDT 2010


Denis Koroskin:

> import std.stdio;
> import std.typecons;
> 
> void main()
> {
> 	auto aa = ["hello": tuple(100.0, 6100.0)];
> 	auto result = aa["hello"];
> 	
> 	writeln(result.field[0], " ", result._1); // primary and alternative way
> }

Now Tuples accept the natural syntax too:
writeln(result[0], " ", result[1]);

----------------------------------------

Bob Cowdery:

> enum E_MODE
> {
>   LSB,                //  0
>   USB,                //  1
>   DSB,                //  2
>   CWL,                //  3
>   CWU,                //  4
>   FMN,                //  5
>   AM,                //  6
>   DIGU,                //  7
>   SPEC,                //  8
>   DIGL,                //  9
>   SAM,                // 10
>   DRM                // 11
> }
> // Associative array for translation
> auto A_MODE = [
>     "LSB": E_MODE.LSB,
>     "USB": E_MODE.USB,
>     "DSB": E_MODE.DSB,
>     "CWL": E_MODE.CWL,
>     "CWU": E_MODE.CWU,
>     "FMN": E_MODE.FMN,
>     "AM": E_MODE.AM,
>     "DIGU": E_MODE.DIGU,
>     "SPEC": E_MODE.SPEC,
>     "DIGL": E_MODE.DIGL,
>     "SAM": E_MODE.SAM,
>     "DRM": E_MODE.DRM
> ];

I suggest code similar to:

enum E_MODE {
    LSB,  //  0
    USB,  //  1
    DSB,  //  2
    CWL,  //  3
    CWU,  //  4
    FMN,  //  5
    AM,   //  6
    DIGU, //  7
    SPEC, //  8
    DIGL, //  9
    SAM,  // 10
    DRM   // 11
}

void main() {
    // associative array for translation
    with (E_MODE) immutable auto a_mode = [
            "LSB":  LSB,
            "USB":  USB,
            "DSB":  DSB,
            "CWL":  CWL,
            "CWU":  CWU,
            "FMN":  FMN,
            "AM":   AM,
            "DIGU": DIGU,
            "SPEC": SPEC,
            "DIGL": DIGL,
            "SAM":  SAM,
            "DRM":  DRM
        ];
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list