Skip to content

Instantly share code, notes, and snippets.

@kirushik
Last active December 14, 2015 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirushik/5125778 to your computer and use it in GitHub Desktop.
Save kirushik/5125778 to your computer and use it in GitHub Desktop.
Нашёл на просторах интернетов (http://stackoverflow.com/a/14155541/181800) вот этот клёвый хак. При попытке создания лишних инстансов Direction сваливается с ошибкой, что в общем-то ожидаемое поведение в отсутствие приватных конструкторов.
package {
class Direction {
private static const CREATE:Object = {};
/*
* The actual enumeration values. Note that the sorting order doesn't matter.
*/
public static const NONE:Direction = new Direction(CREATE);
public static const UP:Direction = new Direction(CREATE);
public static const DOWN:Direction = new Direction(CREATE);
public static const LEFT:Direction = new Direction(CREATE);
public static const RIGHT:Direction = new Direction(CREATE);
/* Constructor */
public function Direction(permission:Object) {
if (permission !== CREATE){
throw new Error("Enum cannot be instantiated from outside");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment