You could also leave off the typecasting on the argument. You don't have to type your arguments in AS2, and the compiler won't complain.
If the methods inside the argument check what type of argument it is, then it's pretty apparent to anybody reading the code why there is no type on the argument. Typing has no effect except for the debugging and readability. Flash gets no speed benefit from strict typing like other languages do. > -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf > Of Adrian Park > Sent: Thursday, June 15, 2006 9:33 AM > To: Flashcoders mailing list > Subject: Re: [Flashcoders] Accepting 1 parameter with 2 > possible types in amethod? > > Hi all. Thanks for the responses. > > I think my favoured route would be by Typing the parameter as > generic Object > as it means it's still one parameter which seems neater. > > Ian, I'm intrigued if your suggestion works - I had considered it but > assumed it wouldn't work because the result of the typeOf > operation would > just be 'object' (since that is what the parameter is typed as). > > I'm going to try it as that is exactly the kind of solution I > was after. > > Thanks > Adrian P > > > On 6/15/06, Ian Thomas <[EMAIL PROTECTED]> wrote: > > > > Hi Adrian, > > I'd do it like this: > > > > private function myMethod(i:Object):Void > > { > > if (i instanceof String) { > > // do this > > } > > else if (i instanceof Number) { > > // do this > > } > > else > > { > > // report an error > > } > > } > > > > And, as you see, stick in an error condition - because by > changing the > > input type to Object you throw away all type-safety and can't > > guarantee that someone won't try passing in a completely different > > type of object... > > > > As to whether it's dirty or not - that's your call, really. :-) It > > really depends on the purpose of the function. I've used it > > occasionally to fake overloading of functions just to make > APIs easier > > to understand. > > > > Sorry to be blurry about it, but without knowing a bit more > about the > > circumstances, it's kind of hard to say... > > > > HTH, > > Ian > > > > On 6/15/06, Adrian Park <[EMAIL PROTECTED]> wrote: > > > Hi List, > > > > > > What's the neatest way of accepting a single parameter > with 2 possible > > types > > > into a method and then working out what type of parameter has been > > passed? > > > > > > e.g. in pseudo code > > > > > > private function myMethod( i:Number/String ):Void { > > > > > > if ( i is a String ) { > > > // do this > > > } else if ( i is a Number ) { > > > // do this > > > } > > > } > > > > > > I can think of several alternatives : > > > - passing a generic object which contains the property > and then using > > typeof > > > on that property > > > - calling 2 different methods but, in this case, it just > makes sense to > > be > > > one since it's doing the same thing with either parameter > (retrieving a > > bit > > > of data from an Array which may be identified using a > numerical id or a > > > String ID) > > > - limiting my method to accepting a String only and then > defining a > > second > > > method that returns the correspoding String identifier > given a numerical > > > identifier > > > > > > All of the ways I can think of seem dirty. Is there a > nice clean way or > > is > > > it wrong to expect the method to accept one parameter of different > > types? > > > > > > Thanks in advance, > > > Adrian P. > > _______________________________________________ > > [email protected] > > To change your subscription options or search the archive: > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > > > Brought to you by Fig Leaf Software > > Premier Authorized Adobe Consulting and Training > > http://www.figleaf.com > > http://training.figleaf.com > > > _______________________________________________ > [email protected] > To change your subscription options or search the archive: > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > Brought to you by Fig Leaf Software > Premier Authorized Adobe Consulting and Training > http://www.figleaf.com > http://training.figleaf.com _______________________________________________ [email protected] To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com

