getterLazy

Similar to "getter", but for values that are to be lazily generated and cached. This is useful for values that are complex to generate, not always used, and either never or infrequently change.

The first time the getter is called, the generator function you have provided is run, and it's return value is cached and returned. On subsequent calls to the getter, the cached value is returned without the generator function being called. The cache can be cleared, thus forcing the value to be re-generated upon the next getter call, by setting "_myVarName_cached" to false.

Example use-case: If you have a property created by getter() and want to change the "get" from a trivial "return _blah" to a more involved function, you will most likely just simply switch from getter to getterLazy.

Additional Info:

If you don't want the value to ever be cached, just set "_myVarName_cached" to false within your provided generator function.

Usage:

mixin(getterLazy!(int, "myVar", `
	// Ordinarily, this function would be more complex
	return 7;
`));

mixin(getterLazy!("protected", int, "myVar2", `return 7;`));

mixin(getterLazy!(string, "str"));
private string _str_gen()
{
	return "Hello";
}

Turns Into:

private int _myVar;
private bool _myVar_cached = false;
@property public int myVar() {
	if(!_myVar_cached) {
		_myVar_cached = true;
		_myVar = _myVar_gen();
	}
	return _myVar;
}
private int _myVar_gen()
{
	// Ordinarily, this function would be more complex
	return 7;
}

protected int _myVar2;
protected bool _myVar2_cached = false;
@property public int myVar2() {
	if(!_myVar2_cached) {
		_myVar2_cached = true;
		_myVar2 = _myVar2_gen();
	}
	return _myVar2;
}
protected int _myVar2_gen()
{
	return 7;
}

private string _str;
private bool _str_cached = false;
@property public string str() {
	if(!_str_cached) {
		_str_cached = true;
		_str = customGenFunc();
	}
	return _str;
}
private string customGenFunc()
{
	return "Hello";
}
  1. template getterLazy(varType, string name, string genFunc = "")
    template getterLazy (
    varType
    string name
    string genFunc = ""
    ) {}
  2. template getterLazy(string writeAccess, varType, string name, string genFunc = "")

Members

Manifest constants

getterLazy
enum getterLazy;
Undocumented in source.

Meta