Flexible variable type for functions: ANY_TO_xxx

Anonymous
2019-11-05
2019-11-06
  • Anonymous - 2019-11-05

    Originally created by: fk5

    (I accidentally posted this question in the v2.3 section instead of the v3.5 section)

    I was able to get the input of the function to accept any variable type, but I also want it to output into the same type as whatever is connected to it.
    Is this possible? (Example: I'd like to modify this function to work regardless if dd is a REAL, INT, BYTE, ...)

    IMG: any_num_01.png

     
  • i-campbell

    i-campbell - 2019-11-06

    Hi fk5, a common question but it is not implemented (and I think not allowed by the IEC 61131-3).
    You can:
    1. use a function block and output all possible values
    2. return a STRUCT of all possible values
    3. Only return the maximal resolution Datatype and require the calling code to do an TO. Obviously this is limiting, as LREAL and LINT can hold numbers that the other can't.
    4. write a separate function for each Datatype, eg anyTestFunction_AsReal(), anyTestFunction_AsLint()

    In practice I haven't found this a big limitation, as you have to think about what datatypes you're using anyway, and knowing that someone (me?) has taken the time to write a specific _AsReal() function rather than just hoping a generic typecast will fit is OK with me.

    Which do you think you'll use?

     
  • Anonymous - 2019-11-06

    Originally created by: fk5

    Yeah... This is what I suspected.
    I currently have functions like intScale(), realScale() and intRamp(), realRamp(), etc.
    And I have functions that are done in REAL, then I have to use XXX_TO_XXX everywhere.
    Its annoying. I was hoping for a magical variable type that I didn't know about.

     

Log in to post a comment.