Optional function arguments?

Several DOORS built-in functions offer variable length argument lists (e.g. DBE apply()).

Is there a way to implement the same in your own DXL functions??

Regards,
Strathglass
strathglass - Mon Mar 16 14:02:49 EDT 2009

Re: Optional function arguments?
kbmurphy - Mon Mar 16 17:07:26 EDT 2009

Write your own function more than once. ie....

void myfunction(int i) {

print i "\n"

}

void myfunction(int i, int j) {

print i " " j "\n"

}
int theInt = 1
int theInt2 = 2

myfunction(theInt)
myfunction(theInt, theInt2)

Re: Optional function arguments?
strathglass - Mon Mar 16 20:38:39 EDT 2009

kbmurphy - Mon Mar 16 17:07:26 EDT 2009
Write your own function more than once. ie....

void myfunction(int i) {

print i "\n"

}

void myfunction(int i, int j) {

print i " " j "\n"

}
int theInt = 1
int theInt2 = 2

myfunction(theInt)
myfunction(theInt, theInt2)

Cool, thanks. That is kind of sneaky, but if it is allowed I can try it!

Of course it would seem the logical way to code this is to have the function that supports the full arg list be the main/full implementation at the top.
The others could just be shells that add the needed default args and call the full implementation.
I suppose that would work fine! (?)

void myfunc(int i, bool flag) {
if (flag) {print "i=" i "\n"}
else {print "i "\n"}
}

void myfunc(int i) {
myfunc(i,true)
}

int theInt = 1

myfunction(theInt)
myfunction(theInt, false)

Re: Optional function arguments?
Tony_Goodman - Tue Mar 17 09:18:36 EDT 2009

strathglass - Mon Mar 16 20:38:39 EDT 2009
Cool, thanks. That is kind of sneaky, but if it is allowed I can try it!

Of course it would seem the logical way to code this is to have the function that supports the full arg list be the main/full implementation at the top.
The others could just be shells that add the needed default args and call the full implementation.
I suppose that would work fine! (?)

void myfunc(int i, bool flag) {
if (flag) {print "i=" i "\n"}
else {print "i "\n"}
}

void myfunc(int i) {
myfunc(i,true)
}

int theInt = 1

myfunction(theInt)
myfunction(theInt, false)

DXL allows overloading of functions. All you have to do is be sure that each can be distinguished by the interpreter.

Yes it is a good idea to declare the versions with the most parameters first.

You cannot overload a function with no parameters.

Re: Optional function arguments?
ePiallat - Tue Mar 17 10:42:36 EDT 2009

Tony_Goodman - Tue Mar 17 09:18:36 EDT 2009
DXL allows overloading of functions. All you have to do is be sure that each can be distinguished by the interpreter.

Yes it is a good idea to declare the versions with the most parameters first.

You cannot overload a function with no parameters.

Yes you can ! (and it's a DXL specificity)
By defining a different return type.

Look at the example below (the really really bad practice exemple below).

bool head () {
if (null current Object) return false
return ((current Object)."Object Heading" ""!="")
}

string head () {
if (null current Object) return ""
return (current Object)."Object Heading" ""
}
if head then print head else print "empty"

Re: Optional function arguments?
Tony_Goodman - Thu Mar 19 05:14:18 EDT 2009

ePiallat - Tue Mar 17 10:42:36 EDT 2009
Yes you can ! (and it's a DXL specificity)
By defining a different return type.

Look at the example below (the really really bad practice exemple below).

bool head () {
if (null current Object) return false
return ((current Object)."Object Heading" ""!="")
}

string head () {
if (null current Object) return ""
return (current Object)."Object Heading" ""
}
if head then print head else print "empty"

You are correct, but what I wanted to warn people of is that the following does not work when the return type is the same but the parameters differ:

string doStuff()
{
}

string doStuff(string s)
{
}

In the following order, this does work:

string doStuff(string s)
{
}

string doStuff()
{
}

Re: Optional function arguments?
llandale - Thu Mar 19 13:44:05 EDT 2009

Tony_Goodman - Thu Mar 19 05:14:18 EDT 2009
You are correct, but what I wanted to warn people of is that the following does not work when the return type is the same but the parameters differ:

string doStuff()
{
}

string doStuff(string s)
{
}

In the following order, this does work:

string doStuff(string s)
{
}

string doStuff()
{
}

// Huh? This works:

string doStuff()
{ print "doStuff()\n"
return("A")
}

string doStuff(string s)
{ print "doStuff(s)\n"
return(s)
}

doStuff()
doStuff("FF")

// correctly prints "doStuff()"; followed by "doStuff(s)"

Re: Optional function arguments?
llandale - Thu Mar 19 13:49:23 EDT 2009

Just to clarify: the manual presents them as optional parameters because its easier for folks to understand the function's meaning. However, there is no function with optional parameters, there are just several functions with the same name and same return type that have different parameters.

So if the manual says "User = find(string NameUser)", it means there are two functions, one has a string parameter and one does not. Further, its reasonable to presume that the function that does not have a string parameter, looks something like this:
User find()
{ return(find(doorsname()))
}

Not always, but usually the overloaded functions that have fewer parameters just call the main function (with all the parameters) using default values for the missing parameters; doorsname() in this case.

> Louie

Re: Optional function arguments?
Tony_Goodman - Fri Mar 20 05:19:06 EDT 2009

llandale - Thu Mar 19 13:44:05 EDT 2009
// Huh? This works:

string doStuff()
{ print "doStuff()\n"
return("A")
}

string doStuff(string s)
{ print "doStuff(s)\n"
return(s)
}

doStuff()
doStuff("FF")

// correctly prints "doStuff()"; followed by "doStuff(s)"

I am out of date - they must have fixed it!
I will edit my post and pretend I never said that...oh...I can't.

Re: Optional function arguments?
llandale - Fri Mar 20 12:06:16 EDT 2009

Tony_Goodman - Fri Mar 20 05:19:06 EDT 2009
I am out of date - they must have fixed it!
I will edit my post and pretend I never said that...oh...I can't.

"I will edit my post and pretend I never said that.." Yup, that technique worked so well in the old days, especially since it made some the replies look foolish... :)

Re: Optional function arguments?
kbmurphy - Fri Mar 20 12:14:51 EDT 2009

llandale - Fri Mar 20 12:06:16 EDT 2009
"I will edit my post and pretend I never said that.." Yup, that technique worked so well in the old days, especially since it made some the replies look foolish... :)

You can only edit your post if you work for IBM, apparently. They've done it in some other threads....