Re: [Flashcoders] Accepting 1 parameter with 2 possible types in a method?
> 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 >} > } > Funny, I just added the optional arguments to haXe and you could do the following : function myMethod( ?i : Int, ?s : String ) : Void { if( i != null ) { // Int argument } else if( s != null ) { // String argument } } myMethod(12345); // same as myMethod(12345,null); myMethod("hello"); // same as myMethod(null,"hello"); Nicolas ___ Flashcoders@chattyfig.figleaf.com 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
Re: [Flashcoders] Accepting 1 parameter with 2 possible types in a method?
Hi Adrian, Your i:Object declaration is a _compile-time_ declaration. All it's there for is to make the compiler happy. instanceof, which is what I'm using (typeof is an older construct which is more limited and less safe) is an operator which does a comparison at runtime on the underlying object that you're passing around. Rather than at compile time. So it doesn't know or care whether you typed i:Object or i:String - it's just working with the actual value of i, which has an underlying type. This code works because a number is represented by a Number class, which is a subclass of Object - and because a string is represented by a String class, which is also a subclass of Object. (This isn't quite true, but is true enough for these circumstances!). Because Object is the common superclass, the compiler doesn't get upset when you type myFunction(5) or myFunction("fred"). It's somewhat clearer if I show you an example of a class (rather than Number or String - they are both a little bit different internally but Flash hides the differences from us using something called autoboxing): class A { function A(){} } class B extends A{ function B(){} } var a:A=new A(); var b:B=new B(); var c:A=new B(); trace(a instanceof A); //true trace(b instanceof A); //true trace(c instanceof A); //true trace(a instanceof B); //false trace(b instanceof B); //true trace(c instanceof B); //true As you can probably see, instanceof is working on the underlying object that's been created, never mind what you typed after the colon. The reason I use instanceof instead of typeof is that typeof would give you a less useful answer: trace(typeof A); // "function" (because it's a constructor function) trace(typeof B); // "function" (because it's a constructor function) In your case, typeof should do the job. But just bear in mind that it's an old operator, and should probably be retired. ;-) HTH, Ian On 6/15/06, Adrian Park <[EMAIL PROTECTED]> wrote: 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). ___ Flashcoders@chattyfig.figleaf.com 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
Re: [Flashcoders] Accepting 1 parameter with 2 possible types in a method?
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. ___ Flashcoders@chattyfig.figleaf.com 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 ___ Flashcoders@chattyfig.figleaf.com 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
Re: [Flashcoders] Accepting 1 parameter with 2 possible types in a method?
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. ___ Flashcoders@chattyfig.figleaf.com 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
Re: [Flashcoders] Accepting 1 parameter with 2 possible types in a method?
Hello :) you can try this example : private function _myMethod( args ):Void { var arg0 = arguments[0] ; switch (true) { case arg0 instanceof String : break ; case arg0 instanceof Number : break ; default throw new Error("Illegal Argument, " + arg0 + " must be String or Number") ; ) } For me ... this method is a good alternative :) EKA+ :) 2006/6/15, Adrian Park <[EMAIL PROTECTED]>: 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. ___ Flashcoders@chattyfig.figleaf.com 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 ___ Flashcoders@chattyfig.figleaf.com 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
Re: [Flashcoders] Accepting 1 parameter with 2 possible types in a method?
you could use 2 params... use String for one, Number for the other - and then use accordingly? 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. ___ Flashcoders@chattyfig.figleaf.com 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 ___ Flashcoders@chattyfig.figleaf.com 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
Re: [Flashcoders] Accepting 1 parameter with 2 possible types in a method?
you could try this: private function myMethod( i:Object ):Void 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. ___ Flashcoders@chattyfig.figleaf.com 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 -- Daniel Cascais Tel: +56 (0)2 4589495 Cel: +56 (0)9 9417355 ___ Flashcoders@chattyfig.figleaf.com 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
[Flashcoders] Accepting 1 parameter with 2 possible types in a method?
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. ___ Flashcoders@chattyfig.figleaf.com 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