RobotStudio event

Universal data type Function

Hello,
I am trying to create universal function to check if array contains something. Parameters should be of any type, how "Present(ANYTYPE# DatObj)" function works, for example.
Is this possible?
Thank you
D.V.

Comments

  • Could you explain some more?  I do not fully understand.  If the array contains something in particular, or just has data, not null values.
    Lee Justice
  • dvlcek
    dvlcek
    edited September 2020
    lemster68 said:
    Could you explain some more?  I do not fully understand.  If the array contains something in particular, or just has data, not null values.
    I am sorry.
    I have function which returns TRUE if num array contains specific number. Like this, for example:

    FUNC bool IfArrayContains(num nArray{*}, num nValue)
    VAR bool contains:=FALSE;
    VAR num i := 1;
    WHILE i<=Dim(nArray, 1) AND contains=FALSE DO
    IF nArray{i}=nValue THEN
    contains:=TRUE;
    ENDIF
    incr i;
    ENDWHILE
    RETURN contains;
    ENDFUNC

    And my target is to make this function universal for multiple datatypes.
    Something like this:

    FUNC bool IfArrayContains(ANYTYPE# Array{*}, ANYTYPE# Value)
    IF Type(Array)=Type(Value) THEN
    ......
    ......
    ELSEIF .......................

    And so on.
  • OK, so you do not necessarily need ALL datatypes, just some?  You could use switch parameters and the function Present() to see which types were passed in.  I would also suggest to change your WHILE into FOR loop.
    Lee Justice
  • in other words, I want ANYTYPE# as an input parameter :)
    But it causes syntax error.
  • dvlcek
    dvlcek
    edited September 2020
    lemster68 said:
    OK, so you do not necessarily need ALL datatypes, just some?  You could use switch parameters and the function Present() to see which types were passed in.  I would also suggest to change your WHILE into FOR loop.
    Yes, I thought about it, but it's ugly. It would be easier to have one implicit parameter and call the Type() function with it.
    Edit: And yes, you are right. Put RETURN into FOR loop is better.
  • I was suggesting switch type argument, not in parameter.  How many datatypes do you want?  Just some or All?
    Lee Justice
  • dvlcek
    dvlcek
    edited September 2020
    lemster68 said:
    I was suggesting switch type argument, not in parameter.  How many datatypes do you want?  Just some or All?
    All of them. The idea is to have one universal function for all datatypes.
    Post edited by dvlcek on


  • I mean this.
  • OK, you really got me thinking now.  Let's start by making a record of all the datatypes.

    A parameter reference may mean the entire parameter, an element of a parameter (array) or a component of a parameter (record).

    If they are all listed in a record, then you can pass it in as a component of the record which lists all datatypes.

    Next issue is the second parameter...
    Lee Justice
  • So what you are seeing there is that IF you use an atomic type, THEN it can be anytype.  Not that there is an anytype#.  I started something in RS with the Record, man there are a lot of types!  Too many and most would be of no use to check (In my opinion).  It was not working like that anyway.  So I went back to my earlier suggestion of using switches, you do not have to use all of them, just the one you want.  As a matter of fact, you do not have to use any of them.  But then the function will do nothing.  I put together a little template with three common datatypes which I seem to think that you would want to check.  Here it is:

    FUNC bool check(\switch swbool,\switch swTRUE|switch swFALSE, \switch swnum, num nArray{*}, VAR num nValue, \switch swString, VAR string stArray{*}, VAR string stString)
            IF Present(swbool) THEN
              ! this is one way to do it with more switches
              ! simple for bool operation
              IF Present(swTRUE) THEN
                ! evaluate here to match the value for TRUE
              ELSEIF Present(swFalse) THEN
                ! evaluate here to match the value for FALSE
              ENDIF
            ENDIF
            IF Present(swnum) THEN
              ! evaluate here to match the value passed by nValue
              FOR i FROM 1 TO Dim(nArray, 1) DO
                IF nArray{i}=nValue THEN
                  RETURN TRUE;
                ENDIF
              ENDFOR
            ENDIF
            IF Present(swString) THEN
              ! evaluate here to match the value passed by stString
              FOR i FROM 1 TO Dim(stArray, 1) DO
                IF stArray{i}=stString THEN
                  RETURN TRUE;
                ENDIF
              ENDFOR
            ENDIF
        ENDFUNC
    


    Lee Justice
  • Yes, I used something similar to what you put here. Sure, there are a lot of useless types. Good point. So, commonly used atomic types (num, dnum, string - bool is useless because it has only 2 values ​​and so there is no need to look for a specific value in the array - other types are aliases that can be interchanged or non-value data types - RECORD types like robtarget , jointtarget, etc., you usually don't need to store them in array and look for a specific value), I thought I could use some reference or implicit data type or something. But I did some research and it's not possible. Anyway, thank you for your help.
  • You are quite welcome.
    Lee Justice