ModScript question - Map object '.insert" method doesn't seem to work as documented

I attempted to solve this through tech support but failed.

Lines 4 and 5 reassign the variable "mymap_pair2". Why does this fail?
Lines 11 and 12 attempt to add a Map_Pair object to a Map. According to the documentation this should be a valid operation. Lines 8 and 9 do this and don't fail. Why do lines 11 and 12 fail?

// this is line 0
var my_map = Map();                                         // works. -- my_map is a : chai:Map :: .size = '0' :: to_json = '{}'

var mymap_pair2 = Map_Pair();                               // works. -- mymap_pair2 is a : chai:Map_Pair :: .first() is a chai:string ; .second() is a chai:UNDEFINED
mymap_pair2 = Map_Pair();                                   // DOES NOT WORK --- WHY???
mymap_pair2 = Map_Pair("key string 3","value string 3");    // DOES NOT WORK --- WHY???

my_map["key string 1"] = "value string 1" ;                 // works. -- my_map is a : chai:Map :: .size = '1' :: to_json = '{"key string 1" : "value string 1"}'
my_map.insert(["key string 2":"value string 2"]);           // works. -- my_map is a : chai:Map :: .size = '2' :: to_json = '{"key string 1" : "value string 1","key string 2" : "value string 2"}'
var mymap_pair = Map_Pair("key string 3","value string 3"); // works. --mymap_pair is a : chai:Map_Pair :: .first() is a : chai:string ='key string 3' ; .second() is a : chai:string ='value string 3'

my_map.insert(mymap_pair);                                  // DOES NOT WORK --- I THINK THIS SHOULD WORK ACCORDING TO DOCUMENTATION
my_map.insert(Map_Pair("key string 4","value string 4"));   // DOES NOT WORK --- I THINK THIS SHOULD WORK ACCORDING TO DOCUMENTATION
// this is line 13

Tags:

  • 0  

    Hi Paul. Regarding 4/5, I have seen that before. Same thing happens if you assign a Field object (such as from AppRecord.Fields()) to a var. It does not have an issue in a loop though. I do know that the first time you assign a value to a var, it's type is locked in. So apparently if the object would have been "set" in AppScript, it probably can't change in ModScript.

    I haven't tried the insert approach. But it should be sufficient to do something like my_map["key string 4"]="value string 4".

  • Suggested Answer

    0   in reply to   

    Garry's interpretation seems correct on the 4/5, that Modscript does not want to reassign the Map Pair to one that is already verified. When I do a try /catch on it: 

    try{mymap_pair2 = Map_Pair()}
    catch(error){out.push_back("Error with mymap_pair2 = Map_Pair() : ${error.what()}.  " )}

    Here is what I get:

    "Error with mymap_pair2 = Map_Pair() : Error: \"Unable to find appropriate'=' operator.\" With parameters: (Map_Pair, Map_Pair) .  "

    I tried the latter one as well,my_map.insert(mymap_pair) , and I think that that broken, at least compared to what the documentation says. Here is the error:

    "Error with function dispatch for function 'insert'\" With parameters: (Map).(Map_Pair) . "

    Note that I tried the .insert function with another Map and it works fine.

  • 0 in reply to   

    Thanks Garry.  I'm still at the point where I'm trying to figure out some of the nuances of Chai/Mod script.  When I run into stuff like this I have to wonder if my brain is off-track or there's something else.

    RE lines 4,5; I know about the type-lock feature.  In this case it appears that the variable becomes readonly or perhaps ModScript thinks that assigning a different Map_Pair object is changing the variable's underlying type.

  • 0   in reply to   

    Nice use of the Try/Catch