lu.objmanip

This module contains functions that in some way or another manipulates struct and class instances, as well as (associative) arrays.

Members

Classes

SetMemberException
class SetMemberException

Exception, to be thrown when setMemberByName fails for some given reason.

Functions

pruneAA
void pruneAA(AA aa)

Iterates an associative array and deletes invalid entries, either if the value is in a default .init state or as per the optionally passed predicate.

replaceMembers
void replaceMembers(Thing thing, Token token, Token replacement)

Inspects a passed struct or class for members whose values match that of the passed token. Matches members are set to a replacement value, which is an optional parameter that defaults to the .init value of the token's type.

setMemberByName
bool setMemberByName(Thing thing, string memberToSet, string valueToSet)

Given a struct/class object, sets one of its members by its string name to a specified value. Overload that takes the value as a string and tries to convert it into the target type.

setMemberByName
bool setMemberByName(Thing thing, string memberToSet, Val valueToSet)

Given a struct/class object, sets one of its members by its string name to a specified value. Overload that takes a value of the same type as the target member, rather than a string to convert. Integer promotion applies.

Examples

struct Foo
{
    string nickname;
    string address;
}

Foo foo;

foo.setMemberByName("nickname", "foobar");
foo.setMemberByName("address", "subdomain.address.tld");

assert(foo.nickname == "foobar");
assert(foo.address == "subdomain.address.tld");

foo.replaceMembers("subdomain.address.tld", "foobar");
assert(foo.address == "foobar");

foo.replaceMembers("foobar", string.init);
assert(foo.nickname.length == 0);
assert(foo.address.length == 0);

Meta