ShaneEP
(MM club member)
2011-08-26 06:31 PM
Non-Existing Function returns

Ran into an interesting situation today...Should this return true? What is it returning? @Error still says 0.

 Code:
If NonExistentFunction()
   ? "yes"
else
   ? "no"
endif

get $


Björn
(Korg Regular)
2011-08-26 06:55 PM
Re: Non-Existing Function returns

0 = true - it's non existing ;).
I've never seen an if statement checking if a function exists before tho..


ShaneEP
(MM club member)
2011-08-26 07:14 PM
Re: Non-Existing Function returns

Ha I know right. Me either... But been messing around with kix2exe, and trying to get it's built-in kixforms registration UDF to work.

http://kix2exe.ramonitor.nl/udfs.php#k2e_registerkixforms

From the site...

 Code:
If $K2E_KixformsEnabled = True
  If K2E_RegisterKixforms() = True
    $System = CreateObject("Kixtart.System")
  EndIf
Else
  ; code if K2E_RegisterKixforms() failed
EndIf

That first line isn't always returning true like it should, so I tried removing it and just checking for UDF. But...That second line always returns true lol.


Richard H.Administrator
(KiX Supporter)
2011-09-06 11:56 AM
Re: Non-Existing Function returns

It's correct, if a little confusing when you know that:

  • In KiXtart speech marks around strings are optional. Any string (in the role of a parameter) which is not recognised as KiXtart syntax is assumed to be a string.
  • In KiXtart a non-zero length string is always true (even is it is "0")


So because the function does not exist in the KiXtart syntax dictionary it is assumed to be a simple string and as it is not null it's boolean value is true.

In your example even the word True is not a boolean, it is a non-zero string. If you replaced your True with False it would work exactly the same, which could be a bit confusing.

KiXtart does catch bare strings in some cases, which can also be a little confusing to neophytes, In the following example the first three work but the last does not:
 Code:
""+Hello+@CRLF
CStr(Hello) @CRLF
"" "Hello" @CRLF
"" Hello @CRLF


Richard H.Administrator
(KiX Supporter)
2011-09-06 12:06 PM
Re: Non-Existing Function returns

As parenthesis are optional for functions, here is a simple way to test for function existance:
 Code:
If CStr(Foo) = "Foo" "Foo does not exist!"+@CRLF Else "Foo exists"+@CRLF EndIf
If CStr(Bar) = "Bar" "Bar does not exist!"+@CRLF Else "Bar exists"+@CRLF EndIf

Function Foo()
	exit 0
EndFunction


This correctly reports:
 Quote:
Foo exists
Bar does not exist!


ShaneEP
(MM club member)
2011-09-06 04:28 PM
Re: Non-Existing Function returns

Nice. Thanks for the info Richard, much appreciated.

Richard H.Administrator
(KiX Supporter)
2011-09-07 10:34 AM
Re: Non-Existing Function returns

You're welcome.

If the function has mandatory parameters you'll need to adjust the check accordingly of course.