2.6.3 Any type

Any is a type that is compatible with any other type in both directions. It serves one purpose - to hold values of any type. Explicit casting is required to use these values in order to guarantee that the code does not suddenly become dynamically typed. This restriction maintains Haxe's static typing, and allows for the continued use of advanced type system features and optimizations associated with the type system.

The implementation is quite simple:

abstract Any(Dynamic) from Dynamic to Dynamic {}

The 'Any' type does not make assumptions about what the value actually is or whether it supports fields or operations - this is up to the user to handle.

class Main {
  static function setAnyValue(value:Any) {
    trace(value);
  }

  static function getAnyValue():Any {
    return 42;
  }

  static function main() {
    // value of any type works
    setAnyValue("someValue");
    setAnyValue(42);

    var value = getAnyValue();
    $type(value); // Any, not Unknown<0>

    // won't compile: no dynamic field access
    // value.charCodeAt(0);

    if (value is String) {
      // explicit promotion, type-safe
      trace((value : String).charCodeAt(0));
    }
  }
}

Any is a more type-safe alternative to Dynamic because it doesn't support field access or operators and is bound to monomorphs. To work with the actual value, it needs to be explicitly promoted to another type.