I am seeking to create a logging function that can accommodate any data type. As I know that traditional function overloading is not a thing in RAPID, I would like to be able to take in data types of “anytype” (similar to Type and ValToStr). The intention is to be able to split up composite data types into columns for placing in a CSV. This enables easy loading into a database or otherwise without post processing.
I see some workarounds here. Mutually exclusive optional parameters. ValToString used for all inputs followed by decomposition of brackets to pull out individual data. Individual functions based on type. Etc.
Function ‘overloading’ using anytype seems like it would be a much cleaner implementation though. Also anytype is used several times by system functions, so it would be nice to take advantage of it. Any way to accomplish function overloading in RS/RAPID?
Implementation would look something like below. I see the issue that would be flagged at runtime wherein calling attributes of value would not be so kosher because it is type-less until specified.
string stLog,
string stValueName,
\num2 nDate,
\num2 nTime,
ANYTYPE value)
VAR string stValueType;
VAR string stDataLabel{4};
VAR string stDataValue{4};
VAR orient or1;
stValueType := Type(value);
TEST stValueType
CASE “num”:
stDataLabel{1} := stValueType;
stDataValue{1} := ValToStr(value);
CASE “dnum”:
stDataLabel{1} := stValueType;
stDataValue{1} := ValToStr(value);
CASE “bool”:
stDataLabel{1} := stValueType;
stDataValue{1} := ValToStr(value);
CASE “orient”:
stDataLabel{1} := “orient Q1”;
stDataLabel{2} := “orient Q2”;
stDataLabel{3} := “orient Q3”;
stDataLabel{4} := “orient Q4”;
stDataValue{1} := ValToStr(value.q1);
stDataValue{2} := ValToStr(value.q2);
stDataValue{3} := ValToStr(value.q3);
stDataValue{4} := ValToStr(value.q4);
CASE “pos”:
stDataLabel{1} := “pos x”;
stDataLabel{2} := “pos y”;
stDataLabel{3} := “pos z”;
stDataValue{1} := ValToStr(value.x);
stDataValue{1} := ValToStr(value.y);
stDataValue{1} := ValToStr(value.z);
DEFAULT: RETURN;
ENDTEST
!CODE GOES HERE - Output strings to CSV
Some threads that have asked similar but different questions:
https://forums.robotstudio.com/discussion/6677/function-overloading
https://forums.robotstudio.com/discussion/12405/universal-data-type-function