Constants in ActionScript

Go supports *constants* of character, string, boolean, and numeric values.

```actionscript
package {
    import flash.display.Sprite;
    import flash.text.TextField;
    
    public class ConstantsExample extends Sprite {
        public function ConstantsExample() {
            // const declares a constant value.
            const s:String = "constant";
            trace(s);
            
            // A const statement can appear anywhere a var statement can.
            const n:Number = 500000000;
            
            // Constant expressions perform arithmetic with arbitrary precision.
            const d:Number = 3e20 / n;
            trace(d);
            trace(int(d));
            
            // A number can be given a type by using it in a context that requires one,
            // such as a variable assignment or function call. For example, here Math.sin expects a Number.
            trace(Math.sin(n));
        }
    }
}

Explanation

  1. Const Declarations:

    • const s:String = "constant"; declares a constant string.
    • trace(s); outputs the value of s.
  2. Numeric Constants:

    • const n:Number = 500000000; declares a numeric constant n.
  3. Constant Expressions:

    • const d:Number = 3e20 / n; demonstrates a constant expression performing arithmetic with arbitrary precision.
    • trace(d); outputs d.
    • trace(int(d)); converts d to an integer and outputs it.
  4. Type Conversion:

    • trace(Math.sin(n)); passes n to Math.sin, which expects a Number.

Output

To run the program, compile and execute it using Adobe Flash Player or an appropriate compiler for ActionScript.

$ mxmlc ConstantsExample.as
$ fdb ConstantsExample.swf
constant
6e+11
600000000000
-0.28470407323754404

Next example: For.