Feature - Implemented Feature request: Record literals

Big Money Sylvia

New member
I just intuitively tried to assign a record using a literal. I learned that is not a thing, but veracity suggested making it into a feature request.
The example syntax in my not-currently-working code looks like this:
JavaScript:
exp_buff_score[int] scores;

if(my_primestat() == $stat[muscle]) {
    exp_buff_score[int] scores = {
        0: new exp_buff_score {
            buff: $effect[Quiet Determination],
            buffing_skill: $skill[Quiet Determination],
            availability: have_skill($skill[Quiet Determination]),
            score: scaling_monster  ?  my_basestat($stat[muscle]) / 32 * mainstat_gain_multiplier  :  0,
        },
        1: new exp_buff_score {
            buff: $effect[Patient Smile],
            buffing_skill: $skill[Patient Smile],
            availability: have_skill($skill[Patient Smile]),
            score: 1 * mainstat_gain_multiplier,
        },
    };
}
 
Last edited:

Veracity

Developer
Staff member
An array literal looks like this:

Code:
exp_buff_score [] scores = {
    det_scoring,
    patient_coring
};

Notice that an "array" is a (un-extendable) map from int to (something) which is indexed from 0 to (something).
You do not specify the index when initializing an array like that.

A map literal looks like this:
Code:
exp_buff_score[int] scores = {
   0: det_scoring,
   1: patient_scoring
}

Since a map can be sparse - and is modifiable to add new elements - you have to specify the (int, in this case), key.

Notice that it knows the data type - exp_buff_score - because you declared it.

By analogy with a map literal, I could see a record literal looking like this:

Code:
exp_buff_score def_scoring = {
    buff: $effect[Quiet Determination],
    buffing_skill: $skill[Quiet Determination],
    availability: have_skill($skill[Quiet Determination]),
    score: scaling_monster  ?  my_basestat($stat[muscle]) / 32 * mainstat_gain_multiplier  :  0,
};
No need to specify "new exp_buff_score", since it knows what it is defining.

Given that, you could embed it in an array literal, say:

Code:
exp_buff_score [] scores = {
    {... record literal #0 for an exp_buff_score ...},
    {... record literal #1 for an exp_buff_score ...}
};

I quite like the idea.
 

Veracity

Developer
Staff member
Revision r27476 adds this.

This test script:

Code:
record Point
{
    int x;
    int y;
};

Point printPoint(string name, Point p)
{
    print(name + ": x = " + p.x + " y = " + p.y);
    return p;
}

Point point1 = { x: 1, y : 2 };
Point point2 = new Point(13, 31);
Point point3 = { y: 4, x : 3 };

printPoint("point1", point1);
printPoint("point2", point2);
printPoint("point3", point3);

record PointArray
{
    string name;
    Point[] points;
};

PointArray printPointArray(PointArray pa)
{
    print(pa.name + " has " + count(pa.points) + " points");
    foreach n, p in pa.points {
        string id = pa.name + "[" + n + "]";
        printPoint(id, p);
    }
    return pa;
}

PointArray pa1 = {
 name : "pa1",
 points : {
     { x : 1, y : 2 },
     new Point(10, 20),
     { x : 100, y : 200 }
 }
};

printPointArray(pa1);

record PointMap
{
    string name;
    Point[int] points;
};

PointMap printPointMap(PointMap pm)
{
    print(pm.name + " has " + count(pm.points) + " points");
    foreach n, p in pm.points {
        string id = pm.name + "[" + n + "]";
        printPoint(id, p);
    }
    return pm;
}

PointMap pm1 = {
 name : "pm1",
 points : {
    1 : { x : 1, y : 2 },
    3 : new Point(10, 20),
    5 : { x : 100, y : 200 }
 }
};

printPointMap(pm1);

yields this:

Code:
point1: x = 1 y = 2
point2: x = 13 y = 31
point3: x = 3 y = 4
pa1 has 3 points
pa1[0]: x = 1 y = 2
pa1[1]: x = 10 y = 20
pa1[2]: x = 100 y = 200
pm1 has 3 points
pm1[1]: x = 1 y = 2
pm1[3]: x = 10 y = 20
pm1[5]: x = 100 y = 200

Note that a record literal can be used anywhere a "new RECNAME(FIELD1, FIELD2, ...)" can be used.
You don't need to specify all the fields. Nor do you need to specify them in a particular order.
Record literals can be used in map or array literals, if a record is the data type.
Similarly, map and array literals can be used inside a record literal, if that's what a record field requires.
 

Big Money Sylvia

New member
That's so cool. Thank you for the detailed explanation, too. Sorry it took me a while to respond, I wanted to make sure I'm in a state to really understand every step of it.
 
Top