vibe.d beta banner
get vibe.d

Template ChainedPolicy

Chains serialization policy.

Constructs a serialization policy that given a type T will apply the first compatible policy toRepresentation and fromRepresentation functions. Policies are evaluated left-to-right according to isPolicySerializable.


template ChainedPolicy(alias Primary, Fallbacks...);

See Also


import std.conv;

// To be represented as the boxed value when serialized
static struct Box(T) {
	T value;
// Also to berepresented as the boxed value when serialized, but has
// a different way to access the value.
static struct Box2(T) {
	private T v;
	ref T get() {
		return v;
template BoxPol(S)
	auto toRepresentation(S s) {
		return s.value;

	S fromRepresentation(typeof(toRepresentation(S.init)) v) {
		return S(v);
template Box2Pol(S)
	auto toRepresentation(S s) {
		return s.get();

	S fromRepresentation(typeof(toRepresentation(S.init)) v) {
		S s;
		s.get() = v;
		return s;
alias ChainPol = ChainedPolicy!(BoxPol, Box2Pol);
static assert(!isPolicySerializable!(BoxPol, Box2!int));
static assert(!isPolicySerializable!(Box2Pol, Box!int));
static assert(isPolicySerializable!(ChainPol, Box!int));
static assert(isPolicySerializable!(ChainPol, Box2!int));

Sönke Ludwig


© 2013-2014 rejectedsoftware e.K.


Subject to the terms of the MIT license, as written in the included LICENSE.txt file.