Feature - Rejected Map of records

Veracity

Developer
Staff member
Per another (bug report), ASH records do not do "equality" correctly. If they did, it would be nice if a map could have records as keys.

Code:
record myrec
{
    int a;
    int b;
    int c;
};

string to_string(myrec rec)
{
    return "(" + rec.a + "," + rec.b + "," + rec.c + ")";
}

myrec rec1 = new myrec(1, 2, 3);
myrec rec2 = new myrec(10, 20, 30);
myrec rec3 = new myrec(100, 200, 300);

myrec [string] name_map = {
    "rec1" : rec1,
    "rec2" : rec2,
    "rec3" : rec3
};

print("name_map contains " + count(name_map) + " names:");
foreach name, rec in name_map {
    print(name + " -> " + rec);
}

print("name_map contains 'rec1' = " + (name_map contains "rec1"));
print("name_map contains 'rec1' = " + (name_map contains "rec2"));
print("name_map contains 'rec3' = " + (name_map contains "rec3"));

string [myrec] rec_map = {
    rec1 : "rec1",
    rec2 : "rec2",
    rec3 : "rec3"
};


print("rec_map contains " + count(rec_map) + " records:");
foreach rec, name in rec_map {
    print(rec + " -> " + name);
}

print("rec_map contains " + rec1 + " = " + (rec_map contains rec1));
print("rec_map contains " + rec2 + " = " + (rec_map contains rec2));
print("rec_map contains " + rec3 + " = " + (rec_map contains rec3));

yields

Code:
Index type 'myrec' is not a primitive type (map-of-records.ash, line 3, char 9 to char 14)

but I would like it to yield:

Code:
name_map contains 3 names:
rec1 -> (1,2,3)
rec2 -> (10,20,30)
rec3 -> (100,200,300)
name_map contains 'rec1' = true
name_map contains 'rec1' = true
name_map contains 'rec3' = true
rec_map contains 3 records:
(1,2,3) -> rec1
(10,20,30) -> rec2
(100,200,300) -> rec3
rec_map contains (1,2,3) = true
rec_map contains (10,20,30) = true
rec_map contains (100,200,300) = true
 

Veracity

Developer
Staff member
Further consideration reveals that this cannot work, since the key in a map must be immutable.

Your program may treat certain record types as immutable, but nothing in the language stops you from changing fields after creation.
 
Top