0
Hello All,

I'm wondering if there is a way to define a method that accepts an optional parameter?




myfunction( var1, var2, var3);
myfunction( var1, var2, var3, var4);

def myfunction( var1, var2, var3, var4){

//Do Stuff.

}



-Thanks
Curtis
Responses (5)
  • Accepted Answer

    Monday, May 20 2019, 11:57 AM - #Permalink
    0
    I would think I would be able to handle an empty variable passed to the 2nd function, but when I try to validate this via composer I get an error saying "unexpected value in the parameter list."


    Pseudo code:


    myfunction ( param1, param2, param3, );

    def myfunction( param1, param2, param3, param4);{

    //do stuff

    }

    The reply is currently minimized Show
  • Accepted Answer

    Monday, May 20 2019, 12:53 PM - #Permalink
    0
    Hi Curtis,

    I'm not sure whether this translates to modscript, but in Java I would create the same function with 3 and with 4 parameters, with the 3 parameter function being a wrapper that calls the 4 parameter one, passing a null for param4. The 4 parameter function then checks for null in param4 to determine its optional-ness :-)

    I haven't written modscript yet, so I have no idea whether this translates to modscript, but I wanted to pass it along just in case it does...

    Cheers,

    - Richard.
    Like
    The reply is currently minimized Show
  • Accepted Answer

    Monday, May 20 2019, 01:04 PM - #Permalink
    0
    Richard Gering wrote:

    Hi Curtis,

    I'm not sure whether this translates to modscript, but in Java I would create the same function with 3 and with 4 parameters, with the 3 parameter function being a wrapper that calls the 4 parameter one, passing a null for param4. The 4 parameter function then checks for null in param4 to determine its optional-ness :-)

    I haven't written modscript yet, so I have no idea whether this translates to modscript, but I wanted to pass it along just in case it does...

    Cheers,

    - Richard.


    I could try this out; so in theory if I supply 4 parameters the call would bypass the 3 parameter wrapper?
    The reply is currently minimized Show
  • Accepted Answer

    Tuesday, May 21 2019, 10:52 AM - #Permalink
    0
    Yes, that's the theory. The only thing that's unclear to me is whether modscript (ChaiScript) lets you define the same function with multiple arguments, and call the correct function based on how many arguments you pass. Given its origin I'm inclined to say yes, but the ChaiScript docs I read were surprisingly unclear about it.

    Best of luck,

    - Richard.
    The reply is currently minimized Show
  • Accepted Answer

    Monday, June 03 2019, 12:43 AM - #Permalink
    0
    As it turns out, you can overload function definitions with different numbers of parameters. For example the code below works. I'm only mildly surprised. ChaiScript has some nice, although sparsely documented, functionality.

    def TestFunct(p1) {
    Ext.LogInfoMsg("TestFunct(p1)") ;
    Ext.LogInfoMsg(" p1 is a ${VarInfo(p1)} == ${p1}") ;
    }

    def TestFunct(p1,p2) {
    Ext.LogInfoMsg("TestFunct(p1,p2)") ;
    Ext.LogInfoMsg(" p1 is a ${VarInfo(p1)} == ${p1}") ;
    Ext.LogInfoMsg(" p2 is a ${VarInfo(p2)} == ${p2}") ;
    }

    def TestFunct(p1,p2,p3) {
    Ext.LogInfoMsg("TestFunct(p1,p2,p3)") ;
    Ext.LogInfoMsg(" p1 is a ${VarInfo(p1)} == ${p1}") ;
    Ext.LogInfoMsg(" p2 is a ${VarInfo(p2)} == ${p2}") ;
    Ext.LogInfoMsg(" p3 is a ${VarInfo(p3)} == ${p3}") ;
    }


    // Main()
    Ext.LogInfoMsg("Try TestFunct with 1 param");
    TestFunct("one") ;

    Ext.LogInfoMsg("Try TestFunct with 2 params");
    TestFunct("one","two") ;

    Ext.LogInfoMsg("Try TestFunct with 3 params");
    TestFunct("one","two?","Three!") ;



    NOTE: The "VarInfo" function is defined here.
    Like
    • Richard Gering
      5 days ago
      I was hoping it would be able to do this, given the origin of ChaiScript, but thanks for confirming!

      And thumbs up on ${VarInfo(parm)} :-)
    The reply is currently minimized Show
Your Reply

Recent Tweets