Posts: 133
Threads: 15
Joined: Jun 2014
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
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?
Posts: 12,140
Threads: 142
Joined: Dec 2002
Pass 0 for unused parameters.
Function10 0 "string"
Posts: 133
Threads: 15
Joined: Jun 2014
I've just found out too, but I pass in an empty int array.
Your solution is much simpler.
Thanks a lot!
Posts: 12,140
Threads: 142
Joined: Dec 2002
Or use VARIANT. It is simple for the caller, but slow because copies data etc.
Function Function292
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
,
Posts: 133
Threads: 15
Joined: Jun 2014
Function292 works just fine but just accept one parameter only.
What if I want to pass in 2 params?
Posts: 12,140
Threads: 142
Joined: Dec 2002
function `v [$k]
But better don't do it, looks not good. Better use Function10 then.
Posts: 133
Threads: 15
Joined: Jun 2014
I come up with this but then there are almost 2 identical "sel" statements:
Function Function12
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?
Posts: 12,140
Threads: 142
Joined: Dec 2002
Posts: 133
Threads: 15
Joined: Jun 2014
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.
|