Accepting two or more types in a function?
BlitzMax Forums/BlitzMax Programming/Accepting two or more types in a function?
| ||
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? :) |
| ||
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 |
| ||
Smashing, thanks Brucey! I like the vague one better ;) It's getting kind of weird heh, you're always answering my silly questions :) |
| ||
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() |
| ||
Hmm, your first two examples didn't work for me. Here's a snippetmethod 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.. |
| ||
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) |
| ||
Blitz says it can't find obj.text, but it exists on both types. try: getCmdFromStr( tMesg(obj).text ) *edit* Dammit brucey! |
| ||
Haha. Thanks guys! :D Such a wonderful and helpful community this is, people falling over themselves to help! ;) |
| ||
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 :) |
| ||
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? |
| ||
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 :-) |
| ||
Thanks again Brucey :) I really don't know what you mean by testing framework :p Care to explain it to me in further detail? |
| ||
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. |