Enforce immutability.

To install, run:

haxelib install immutable 0.10.5 

See using Haxelib in Haxelib documentation for more information.


Make your classes and local vars immutable with

haxelib install immutable

-lib immutable

class YourClass implements Immutable
	public var test : String;

	// Fields can be mutable, but will emit a compiler warning:
	@mutable public var specialCase : String;

	public function new() {
		test = "It's ok to assign in the constructor";

	public function test() {
		var a = 123;
		a = 234; // *** Compilation error

		@mutable var b = 123;
		b = 234; // Ok

		this.test = "mutated!"; // *** Compilation error, incl. external access
		this.specialCase = "special"; // Ok

The library is using macros for enforcing this at compile-time only, so it won't slow down your code. It may affect compilation time a bit, so in certain cases you may choose to disable all checking with -D disable-immutable.


When implementing Immutable, vars will behave like const and let, used in modern javascript:

const a = 123;var a = 123;
let b = 234;@mutable var b = 234;

For local vars only

If you want your class vars and properties to behave as usual, you can implement ImmutableLocalVars instead, and it will only affect vars in methods.


Please open an issue if you happened to trick the library, or if you think something is conceptually or semantically wrong. Using Reflect isn't tricking though, it's intentional!

Build Status

2 years ago

All libraries are free

Every month, more than thousand developers use haxelib to find, share, and reuse code — and assemble it in powerful new ways. Enjoy Haxe; It is great!

Explore Haxe

Haxe Manual

Haxe Code Cookbook

Haxe API documentation

You can try Haxe in the browser!

Join us on Github!

Haxe is being developed on GitHub. Feel free to contribute or report issues to our projects.

Haxe on Github