Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/seiren-games/wing-immutable

Immutable variable
https://github.com/seiren-games/wing-immutable

haxe immutable

Last synced: 3 months ago
JSON representation

Immutable variable

Awesome Lists containing this project

README

        

# wing-immutable
Make a variable immutable.
Raise a runtime error on reassignment.

Normally if it is final field, it must be assigned in the constructor.
The purpose is to allow the first assignment in other functions.

# Usage
```haxe
import wing.Immutable;

class Foo {
final foo:Immutable = new Immutable();

public function new() {
setup();
}

function setup():Void {
// The first assignment is OK.
foo.assign(true);
// By `@:forward` metadata, can be used like the underlying type.
trace(foo == true);

// After the second time, runtime error.
foo.assign(false);
}
}
```

## Tips: When public, prevent assignment from outside the class
```haxe
final _foo:Immutable = new Immutable();
public var foo(get, never):Null;
inline function get_foo():Null {
return _foo;
}
```
or
```haxe
final foo:Immutable = new Immutable();
public inline function getFoo():Null {
return foo;
}
```