Passing a dictionary to a function

If I pass a dictionary to a function I would expect that the parameter in the function keeps the type - but the dict gets converted to a string:

var:dictionary target;
target["myA"] = "A";
target["myB"] = "B";
target["myC"]  = "C";
target["myD"]  = "D";

callMyFunction(target);

and

function callMyFunction(theDict){
   $Text = theDict.size;
};

returns 23 instead of 4.

1 Like

I will only comment that theDict.size has been consistently mis-reported or miscalculated through the ages

You want

function f(theDict:dictionary) {...
1 Like

Well, the first thing I’d do in this context is use available syntax:

function callMyFunction(theDict:dictionary){...

Though this isn’t that issue. Consider this simple test:

var:dictionary target;
target["myA"] = "A";
target["myB"] = "B";
target["myC"]  = "C";
target["myD"]  = "D";
var:list targetList = target;

$Text(/logA) = "String value is: `"+target+"'";
$Text(/logA) += "\ntarget.size is: "+target.size;
$Text(/logA) += "\ntarget.keys.size is: "+target.keys.size;
$Text(/logA) += "\ntarget.keys.count is: "+target.keys.count;
$Text(/logA) += "\ntarget.asString.size is: "+target.asString.size;
$Text(/logA) += "\ndictionary as list, targetList.size is: "+targetList.size;

results:

String value is: `myC:C;myB:B;myA:A;myD:D'
target.size is: 4
target.keys.size is: 4
target.keys.count is: 4
target.asString.size is: 23
dictionary as list, targetList.size is: 4

A dictionary is a list of key:pair values. Tinderbox is treating the dictionary ‘size’ as that number of list items (list.size) not the number of characters (string.size). so what’s at play here is some internal type coercion. IOW, a (dictionary) string that looks to Tinderbox link a list is treated as a list when using .size.

No foul here, but this is exactly what List.asString() is for. So, your test needs to be:

target.asString.size

TBH, I didn’t have a fixed assumption as to Dictionary.size should be as its not a documented form. I’d use Dictionary.keys.count (.size deprecated here for .count) for the key count or Dictionary.asString.size for the character count of all key:value; contents

My simple test doc for the above: dict-sizetbx.tbx (183.7 KB)