detecting if an optional parameter is given

Discussions about Coding and Scripting
User avatar
Barbie
Godlike
Posts: 2480
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

detecting if an optional parameter is given

Post by Barbie » Thu Jul 21, 2022 1:09 pm

UScript functions allow optional parameters. Is there a possibility to detect if a parameter was provided? (Like "IsMissing" in VBA or "undefined" property in JS?)

Example:

Code: Select all

function WhatEver(int Param1, optional int Param2) {
	if (IsMissing(Param2))
		DoThis();
	else
		DoSomethingOther();
...
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett

Buggie
Godlike
Posts: 1766
Joined: Sat Mar 21, 2020 5:32 am

Re: detecting if an optional parameter is given

Post by Buggie » Thu Jul 21, 2022 1:15 pm

No, AFAIK.

Partial solution - never send here 0. So if there 0 you can be sure it default value.

Dennis
Average
Posts: 63
Joined: Tue Jan 12, 2021 9:18 pm

Re: detecting if an optional parameter is given

Post by Dennis » Fri Jul 22, 2022 9:01 pm

Have you tried: if ( SomeOptionalVariable == none )?
Fraggers hangout place: http://fraggers.online/

Buggie
Godlike
Posts: 1766
Joined: Sat Mar 21, 2020 5:32 am

Re: detecting if an optional parameter is given

Post by Buggie » Fri Jul 22, 2022 9:24 pm

Did you try it? It is not even compile, just because int can not be None.

User avatar
Feralidragon
Godlike
Posts: 5462
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: detecting if an optional parameter is given

Post by Feralidragon » Sun Jul 24, 2022 2:58 pm

Yeah, you cannot detect that, not directly at least, but then again, in most other languages of the same kind you cannot do that either.

In other languages you can define the default value though, and in some you can even define primitive types as nullable, in order to define proper default values for the default behavior that you aim for, which is probably the reason why you would ever want to do this (because maybe 0 is a bad default value for your optional parameter in that function, right?).

However, there is a way for you to achieve what you're aiming for, namely by doing something like this:

Code: Select all

function WhatEver(int Param1)
{
	WhatEverInternal(Param1, 1); //let's say you want "1" to be the default value instead
}

function WhatEverWithParam2(int Param1, int Param2)
{
	WhatEverInternal(Param1, Param2);
}

function WhatEverInternal(int Param1, optional int Param2)
{
	...
}
Basically, you can have 2 function variations, one with Param2 and another without, and then you use whichever one you need.

Buggie
Godlike
Posts: 1766
Joined: Sat Mar 21, 2020 5:32 am

Re: detecting if an optional parameter is given

Post by Buggie » Sun Jul 24, 2022 3:50 pm

Well. Strictly speaking you can make something awful like this:

Code: Select all

function WhatEver(int Param1, optional coerce string strParam2) { // OFC call it with second param as int.
	local int Param2;
	if (strParam2 == "") // Param not set!
		DoThis();
	else
	{
		Param2 = int(strParam2);
		DoSomethingOther(); // use Param2 here
	}