SafeAccess

Allows to build access chains of class members as done with the ?. operator in other languages. In the chain, any null member that is a class instance or that returns one, has for effect to shortcut the complete evaluation.

This function is copied from https://github.com/BBasile/iz to avoid a new submodule. Any change made to this copy should also be applied to the origin.

Constructors

this
this()
Undocumented in source.
this
this(M m)

Instantiate.

Alias This

m

Members

Aliases

unwrap
alias unwrap = m

Unprotect the class instance.

Functions

as
auto ref as()

Allows cast to interfaces and classes inside the chain.

opDispatch
auto ref opDispatch(A a)

Handles safe access.

Variables

m
M m;
Undocumented in source.

Parameters

M

The class type of the chain entry point.

Bugs

Assigning a member only works with unwrap.

Examples

General usage

class LongLineOfIdent3{int foo; void setFoo(int v) @safe{foo = v;}}
class LongLineOfIdent2{LongLineOfIdent3 longLineOfIdent3;}
class LongLineOfIdent1{LongLineOfIdent2 longLineOfIdent2;}
class Root {LongLineOfIdent1 longLineOfIdent1;}

SafeAccess!Root sar = SafeAccess!Root(new Root);
// without the SafeAccess we would receive a SIGSEGV here
sar.longLineOfIdent1.longLineOfIdent2.longLineOfIdent3.setFoo(0xDEADBEEF);

bool notAccessed = true;
// the same with `&&` whould be much longer
if (LongLineOfIdent3 a = sar.longLineOfIdent1.longLineOfIdent2.longLineOfIdent3)
{
	notAccessed = false;
}
assert(notAccessed);

// checks that forwarding actually works
sar.m.longLineOfIdent1 = new LongLineOfIdent1;
sar.m.longLineOfIdent1.longLineOfIdent2 = new LongLineOfIdent2;
sar.m.longLineOfIdent1.longLineOfIdent2.longLineOfIdent3 = new LongLineOfIdent3;

sar.longLineOfIdent1.longLineOfIdent2.longLineOfIdent3.setFoo(42);
assert(sar.longLineOfIdent1.longLineOfIdent2.longLineOfIdent3.unwrap.foo == 42);

Meta