RobotStudio event

Making an if-statement (or make an temporary function) inside a function or procedure argument

Greetings, I am trying to make as the title suggest an if-statement inside a function- or
procedure-argument in order to decrease the amount of code and learn to handle RAPID
in a more professional way. I have previously experience of Java-programming and know
that something similar to this concept can be done in Java, and like many other subjects
in programming it perhaps can be done in RAPID as well.

 For example I have the following code snippet (that doesn't work sadly but of course):

 TPReadFK reg1, ( fr DInput(DiG) = 1 THEN return "Yes" ENDIF return "No";) ,"Done","","","","";

note that the second argument in method TPReadFK requires a STRING, that's what Im
trying to return here. Sure I could write a method separately which makes this
if-statement and return the correct string, or even place the if-statement precedingly
to the TPReadFK-call, but thats now what Im after, since: -Writing a new Function
that Im not going to use anywhere else is from my viewpoint not a sign of good programming
practice. -place the if-statement precedingly makes the amount of code increase, number
of rows and so forth. It doesn't have to be an if-statement, if I can create a method at the
same time Im calling it, that would be sufficent.
 

If you read all the way down here I would like to thank you for your attention

Yours truly // Martin

Comments


  • What you want to accomplish is not possible. Lambda expressions (or temporary functions) are not supported in RAPID. Furthermore, only two kinds of IF statements are available:
    - Compact IF  -> Execute one instruction only if a condition is satisfied
    - IF                 -> Execute a sequence of different instructions depending on whether or not a condition is satisfied

    A more professional way of programming is not always the same as decreasing the amount of code. In some cases it would be necessary (for example because of memory limitations), but most of the time readability and re-usability are more important keywords. 

    I agree that using a new function for just a single if/else case is not preferable. I would suggest to put the TPReadFK inside an if/else statement or to use an additional variable which is defined with an if/else statement.
  • Greetings,
    <br><br>
    I want to thank you helping me bringing this java-concept back to memory. Your mention of Lambda Expressions led me after searching for it into what I had remembered, which was using a new object of an abstract class with one singular and abstract method, called a "functional interface".What I found along the text was the following example illustrating my initial remembrance and thought of derived application into RAPID<br><br>

    printPersons( roster,
        new CheckPerson() {
            public boolean test(Person p) {
                return p.getGender() == Person.Sex.MALE
                    &amp;&amp; p.getAge() &gt;= 18
                    &amp;&amp; p.getAge() &lt;= 25;
            }
        }
    );

    from searching "lambda expressions" I went into
    <br>
    <br><br>
    It seems I have hereby reached a dead end.
    If no other input is presented I will personally lay this matter on a hold til further do
    and go with the if and TPReadFk-options you suggested.
    <br><br>
    I want to thank you again for helping me sorting this out.
    <br><br>
    Yours truly, Martin
  • Your example with 'printPersons' is possible within Rapid. You will get something like this:

    <div>PROC readFuncKeys(bool Yes, var num Answer)&nbsp;<br></div><div>&nbsp; IF Yes THEN<br></div><div>&nbsp; &nbsp; TPReadFK Answer,"Yes","Done","","","","";&nbsp;<br></div><div>&nbsp; ELSE<br></div><div>&nbsp; &nbsp; TPReadFK Answer,"No","Done","","","","";&nbsp;<br></div><div>&nbsp; ENDIF<br></div><div>ENDPROC</div>


    readFuncKeys&nbsp;<span>DInput(DiG) </span>= 1, reg1;