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
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
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
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
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
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
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
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
}
8 matches
Mail list logo