Highlighted
curtiplas Frequent Contributor.
Frequent Contributor.
658 views

Modscript - Optional Parameters on method

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
Tags (3)
0 Likes
7 Replies
curtiplas Frequent Contributor.
Frequent Contributor.

Re: Modscript - Optional Parameters on method

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

}

0 Likes
Micro Focus Contributor
Micro Focus Contributor

Re: Modscript - Optional Parameters on method

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.
curtiplas Frequent Contributor.
Frequent Contributor.

Re: Modscript - Optional Parameters on method

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?
0 Likes
Micro Focus Contributor
Micro Focus Contributor

Re: Modscript - Optional Parameters on method

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.
0 Likes
PM Thompson Outstanding Contributor.
Outstanding Contributor.

Re: Modscript - Optional Parameters on method

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.
Micro Focus Contributor
Micro Focus Contributor

Re: Modscript - Optional Parameters on method

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)} 🙂
0 Likes
dinghram Absent Member.
Absent Member.

Re: Modscript - Optional Parameters on method

Thanks, Paul. This is correct, but I'd like to point out one last tidbit. The function overloads can invoke each other, so if you just want a default value for p3, do something like this:

def TestFunct( p1, p2, p3 ) {
// do stuff
}

def TestFunct( p1, p2 ) {
TestFunct( p1, p2, "my default value" );
}


By the way, defining the function overloads in this order helps Composer script validation to ensure that TestFunct with 3 parameters is not invoked before it is defined.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.