Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question on user-defined function
#1
How do you handle the case of a user-defined function in which I have 2 different types.
Let say I have one like this:
Function Function10
Code:
Copy      Help
function [ARRAY(int)'aInt] [~textStr]

if (aInt.len)
;do something
,,
if (textStr.len)
;do something else

The problem is if I pass in the int array it works fine. But if I pass in the string param only, it complains about "type mismatch", since it expect to have an int array too?

Note: I know if I create 2 separated functions, each with one param then no problem at all. But
I want to combine them in one function only. Is it possible?
#2
Pass 0 for unused parameters.
Function10 0 "string"
#3
I've just found out too, but I pass in an empty int array.
Your solution is much simpler.
Thanks a lot!
#4
Or use VARIANT. It is simple for the caller, but slow because copies data etc.
Function Function292
Code:
Copy      Help
function `v ;;` is for VARIANT; can pass string or array or int etc, don't need multiple parameters.

;outx v.vt
sel v.vt
,case VT_I4|VT_ARRAY
,ARRAY(int)& a=v.parray
,out a.len
,
,case VT_BSTR
,str s=v
,out s
,
,case else end ERR_BADARG
,
#5
Function292 works just fine but just accept one parameter only.
What if I want to pass in 2 params?
#6
function `v [$k]

But better don't do it, looks not good. Better use Function10 then.
#7
I come up with this but then there are almost 2 identical "sel" statements:

Function Function12
Code:
Copy      Help
function `v `v2;;` is for VARIANT;
;Can pass string or array or int etc, don't need multiple parameters.

;outx v.vt
ARRAY(int)& a
str s
sel v.vt
,case VT_I4|VT_ARRAY
,,a=v.parray
,,;do somethine with a
,
,case VT_BSTR
,,s=v
,,;do something with s
,
,case else end ERR_BADARG

sel v2.vt
,case VT_I4|VT_ARRAY
,,a=v2.parray
,,;do somethine with a
,
,case VT_BSTR
,,s=v2
,,;do something with s
,
,case else end ERR_BADARG
Is there anyway to combine the 2 select statements into one?
#8
Difficult to combine.
#9
You're right! I'd better stick to Function10 unless my function just have one param then I will use Function292.
Thanks for the info.


Forum Jump:


Users browsing this thread: 1 Guest(s)