Function Tag.getValue

Retrieve a value of type T from this tag. Returns a default value if not found.

Useful if you only expect one value of type T from this tag. Only looks for values of this tag, it does not search child tags. If you wish to search for a value in a child tag (for example, if this current tag is a root tag), try getTagValue.

If you want to get more than one value from this tag, use values instead.

If this tag has multiple values, the first value matching the requested type will be returned. Ie, Extra values in the tag are ignored.

You may provide a default value to be returned in case no value of the requested type can be found. If you don't provide a default value, T.init will be used.

If you'd rather an exception be thrown when a value cannot be found, use expectValue instead.

Prototype

T getValue(T)(
  T defaultValue = T.init
)
if (isValueType!T);

Example

import std.exception;
import std.math;
import sdlang.parser;

auto root = parseSource(`
	foo 1 true 2 false
`);
auto foo = root.getTag("foo");
assert( foo.getValue!int() == 1 );
assert( foo.getValue!bool() == true );

// Value found, default value ignored.
assert( foo.getValue!int(999) == 1 );

// No strings found
// If you'd prefer an exception, use `expectValue` instead.
assert( foo.getValue!string("Default") == "Default" );
assert( foo.getValue!string() is null );

// No floats found
assert( foo.getValue!float(99.9).approxEqual(99.9) );
assert( foo.getValue!float().isNaN() );

Authors

Copyright

License