I am trying to understand if it's possible to do function overloading in codesys.
Simple example: I want to create a function SQ(x) to calculate the square of x (x*x).
I could create a SQ_INT, SQ_REAL, SQ_LREAL, ... for every needed datatype - a very ineffiecient way which would flood my project with many unneccessary functions that I would have to maintain.
My investigations lead me to the ANY_-types like ANY_NUM and ANY_REAL.
Correct me if I am wrong:
1) I can use them to pass values of the corresponding types to my function: ANY_REAL would accept REAL and LREAL.
2) Inside my function I can write specific code for every type by checking the type with x.TypeClass
So far so good - what I need is to change my return-type to the corresponding input type.
The square of an INT should return an INT, REAL should return REAL.
Is that possible? I feel like there is some way to achieve that: When calculating the squareroot of a REAL with SQRT(), codesys automatically passes me a REAL and calculating the SQRT() of a LREAL delivers a LREAL. I even get warnings when I try to assign the SQRT(LREAL) to a REAL without explicit conversion because of possible dataloss.
How does this work? And can I also use this mechanism?
Thank you.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
since the ANY type gives you the size, type, and a pointer to the value, you can write your result to the pointer at Out.pValue. Create a bunch of pointers to whatever data types you want to use, for example:
Otherwise make one block with all the different inputs (int, real, uint etc).
Then just use one of the inputs and leave the others at 0 and if input_type > 0 convert to real and do the calculation.
Do the same with the outputs, convert the result to every type you want and then you can use whatever you need for the moment.
This wont work as return value though.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
There are multiple ways as suggested here, you could create a function with multiple datatypes for INs and OUTs, initialize them with 0, and then select whichever data type you need to use. Another method is to leverage ANY like what TVM pointed out. So for example, the below allows you to take two variables and multiple them to gain output using ANY datatype. You could modify the below to mix and match certain datatypes if needed.
Thanks for all the responses.
Your suggestions work and are for sure a workaround - however I wonder how codesys has done it and, whether its possible to replicate for user functions.
I pass a REAL to SQRT() and receive a REAL, I pass a LREAL to SQRT and receive a LREAL.
It looks like there is some hidden type-deduction going on.
Hello,
I am trying to understand if it's possible to do function overloading in codesys.
Simple example: I want to create a function SQ(x) to calculate the square of x (x*x).
I could create a SQ_INT, SQ_REAL, SQ_LREAL, ... for every needed datatype - a very ineffiecient way which would flood my project with many unneccessary functions that I would have to maintain.
My investigations lead me to the ANY_-types like ANY_NUM and ANY_REAL.
Correct me if I am wrong:
1) I can use them to pass values of the corresponding types to my function: ANY_REAL would accept REAL and LREAL.
2) Inside my function I can write specific code for every type by checking the type with x.TypeClass
So far so good - what I need is to change my return-type to the corresponding input type.
The square of an INT should return an INT, REAL should return REAL.
Is that possible? I feel like there is some way to achieve that: When calculating the squareroot of a REAL with SQRT(), codesys automatically passes me a REAL and calculating the SQRT() of a LREAL delivers a LREAL. I even get warnings when I try to assign the SQRT(LREAL) to a REAL without explicit conversion because of possible dataloss.
How does this work? And can I also use this mechanism?
Thank you.
what if you did this?
since the ANY type gives you the size, type, and a pointer to the value, you can write your result to the pointer at Out.pValue. Create a bunch of pointers to whatever data types you want to use, for example:
Otherwise make one block with all the different inputs (int, real, uint etc).
Then just use one of the inputs and leave the others at 0 and if input_type > 0 convert to real and do the calculation.
Do the same with the outputs, convert the result to every type you want and then you can use whatever you need for the moment.
This wont work as return value though.
@unzu,
There are multiple ways as suggested here, you could create a function with multiple datatypes for INs and OUTs, initialize them with 0, and then select whichever data type you need to use. Another method is to leverage ANY like what TVM pointed out. So for example, the below allows you to take two variables and multiple them to gain output using ANY datatype. You could modify the below to mix and match certain datatypes if needed.
Usage is straight forward, just do:
Thanks for all the responses.
Your suggestions work and are for sure a workaround - however I wonder how codesys has done it and, whether its possible to replicate for user functions.
I pass a REAL to SQRT() and receive a REAL, I pass a LREAL to SQRT and receive a LREAL.
It looks like there is some hidden type-deduction going on.
Something like:
Last edit: unzu 2021-07-09