Accepting two or more types in a function?

BlitzMax Forums/BlitzMax Programming/Accepting two or more types in a function?

kronholm(Posted 2008) [#1]
Is it possible to pass two different types to a function and have it accept it, and recognize which types it receives?

For instance you can't pass a string to a function that expects an int. Here's the way I'm doing it right now, but perhaps there's another way?

Pseudocode:
function myFunction(incomingType1:myType1=null,incomingType2:myType2=null)

and then call it with:
myFunction(something:myType1,null)
myFunction(null,somethingElse:myType2)


Possible, or not? :)


Brucey(Posted 2008) [#2]
How's about :
Function myFunction(incomingType:MySuperType)
...
   If MySubType1(incomingType) .....
...
End Function


or far more vague..
Function myFunction(incomingType:Object)
...
   If MyType1(incomingType) .....
...
End Function



kronholm(Posted 2008) [#3]
Smashing, thanks Brucey! I like the vague one better ;)

It's getting kind of weird heh, you're always answering my silly questions :)


Brucey(Posted 2008) [#4]
Or... ;-)

How's about turning the problem over to the Types themselves?

Type MySuperType

    Method SomeThing() Abstract

End Type

Type MyType1 Extends MySuperType

    Method SomeThing()
     ... do this
    End Method

End Type

Type MyType1 Extends MySuperType

    Method SomeThing()
     ... do that
    End Method

End Type


...

Local myInstance1:MySuperType = new MyType1

...
myInstance1.SomeThing()




kronholm(Posted 2008) [#5]
Hmm, your first two examples didn't work for me. Here's a snippet

	method runCommand(obj:object)
		local cmd$
		if tMesg(obj) then getCmdFromStr(obj.text)
		if tTell(obj) then getCmdFromStr(obj.text)


Blitz says it can't find obj.text, but it exists on both types.

I can't use the type extension solution either, because the types are already extended from another type :( Great idea though..


Brucey(Posted 2008) [#6]
You always need to cast to access the Type-specific fields :
	method runCommand(obj:object)
		local cmd$
		if tMesg(obj) then getCmdFromStr(tMesg(obj).text)
		if tTell(obj) then getCmdFromStr(tTell(obj).text)



Perturbatio(Posted 2008) [#7]
Blitz says it can't find obj.text, but it exists on both types.

try:
getCmdFromStr( tMesg(obj).text )



*edit*

Dammit brucey!


kronholm(Posted 2008) [#8]
Haha. Thanks guys! :D
Such a wonderful and helpful community this is, people falling over themselves to help! ;)


kronholm(Posted 2008) [#9]
Here's how I ended up using your advice :)

	method getCmdFromText$(obj:object)	'simply extracts command wether or not a " " is present.
		local txt$
		local mesg:tmesg,tell:ttell
		if tmesg(obj) then mesg = tmesg(obj) ; txt = mesg.text
		if ttell(obj) then tell = ttell(obj) ; txt = tell.text	
		local pos = instr(txt," ")
		if pos then txt = txt[..pos-1]
		if txt[..1] = triggerchar then return txt[1..]	'return cmd without triggerchar
		print "Unknown cmd: "+txt			
	endmethod


It's for a chatbot to recognize commands in tell as well as guildchat in Age of Conan :)


kronholm(Posted 2008) [#10]
Grr. Well apparantly bmax still can't read the .text field in my types, no matter what approac I take. In the above example, the txt variable is empty, but shouldn't be. Even if I use txt = tmesg(obj).text and txt = ttell(obj).text like this:

	method getCmdFromText$(obj:object)	'simply extracts command wether or not a " " is present.
		local txt$
		if tmesg(obj) then txt = tmesg(obj).text
		if ttell(obj) then txt = ttell(obj).text
		local pos = instr(txt," ")
		if pos then txt = txt[..pos-1]
		if txt[..1] = triggerchar then return txt[1..]	'return cmd without triggerchar
		print "Unknown cmd: "+txt			
	endmethod


The function always ends up printing "Unknown cmd: ".

Ideas?


Brucey(Posted 2008) [#11]
Seems okay here :


You wanna get yourself some kind of testing framework. Although it adds to development time, it can help by allowing you to know that any given method is always doing what you expect it to.

Anyhoo... HTH :-)


kronholm(Posted 2008) [#12]
Thanks again Brucey :)

I really don't know what you mean by testing framework :p Care to explain it to me in further detail?


Brucey(Posted 2008) [#13]
Unit testing... where you take parts of your code, and thrash them through a set of tests. You know that method A should handle a null parameter, for example, so the test will fail if it doesn't.

The idea is to make your code more robust, by having a suite of tests you can re-run at any time.

Here's a small example of one way of using a testing framework.

Just one of those things that can assist you in ironing out those nasty little bugs.