Sara
Sara

Reputation: 31

How to implement casting for my own class in Actionscript 3?

I made a class in AS3 for representing complex numbers. It does not inherit anything. How can I enable casting from e.g. Numbers? I.e. I want this:

5 as Complex

to be the same as this:

new Complex(5);

is there a magic cast() function I can put in my class?

Upvotes: 3

Views: 127

Answers (1)

Allan
Allan

Reputation: 3314

Afraid not. Number is an unrelated type to your Complex class. All you will receive is a:

1067: Implicit coercion of a value of type Number to an unrelated type Complex

The only thing I can think of would be to do something like:

asComplex(5);

public function asComplex(num:Number):Complex
{
    return new Complex(num);
}

but not sure there is much point in that.

Upvotes: 3

Related Questions