Variant Type
Variant (Virtufin.Data) is one value of a fixed set of kinds, or none:
| Kind | C# case type | Getter |
|---|---|---|
| None | (none: null contents) |
None |
| Bool, Byte, Int16, Int32, Int64, Float, Double | bool, byte, short, int, long, float, double |
Bool, Byte, … |
| String | string |
String |
| ArrayBool … ArrayString | bool[], byte[], short[], int[], long[], float[], double[], string[] |
ArrayBool, … |
| ArrayVariant | IVariant[] |
ArrayVariant |
| Dictionary | IDictionary<string, IVariant> |
Dictionary |
IVariant is the interface of every variant: Is<Kind> and the getters, IsPrimitive, IsArray,
IsArrayOfPrimitives, IsDictionary, and VariantType (the case's CLR type, null for none).
Variant is its in-memory implementation; FlatBuffersVariant reads one from a FlatBuffers buffer
(FlatBuffers).
Construction
using System.Collections.Generic;
using Virtufin.Data;
Variant a = 42; // implicit conversion: Int32
Variant b = "BTC_USDT"; // String
Variant c = new[] { 1.5, 2.5 }; // ArrayDouble
Variant d = new Variant(42L); // a case constructor: Int64
Variant e = Variant.CreateNone(); // or new Variant()
Variant f = new Dictionary<string, IVariant> { ["side"] = (Variant)"buy", ["qty"] = (Variant)0.5 };
There is a constructor and a Create<Kind> factory per kind, and an implicit conversion each way
for every kind but none and the dictionary (into a Variant only).
Reading a value
A getter returns the value of its own kind and throws InvalidCastException for any other:
int n = a.Int32; // 42
long m = a.Int64; // InvalidCastException: Cannot cast System.Int32 to System.Int64
int i = a; // implicit conversion: the Int32 getter
Pattern matching (a C# 15 union)
Variant is a C# 15 union ([Union], IUnion) with one case per kind. A switch over a variant
matches its case types, and the compiler checks that every case is handled. Leaving one out warns
CS8509; null is none:
static string Describe(Variant v) => v switch
{
bool b => $"bool {b}",
byte or short or int or long => $"integer {v.Value}",
float or double => $"number {v.Value}",
string s => $"text {s}",
bool[] or byte[] or short[] or int[] or long[] or float[] or double[] or string[] => $"array {v}",
IVariant[] items => $"{items.Length} variants",
IDictionary<string, IVariant> fields => $"{fields.Count} fields",
null => "none",
};
A primitive is kept unboxed inside the variant. Matching it calls the case's TryGetValue(out T),
which allocates nothing. Only Value (object?, the union's contents) boxes. HasValue is false
for none.
Conversions and printing
ToArrayVariant()turns an array of primitives into an array of variants;Variant.ToArrayInt32(v)(and the otherToArray<Kind>) turns an array of variants of one kind back into a primitive array. Mixed kinds throwArgumentException.ToString()printsKind(value):Int32(42),ArrayDouble(1.5,2.5),Dictionary((side: String(buy)),(qty: Double(0.5))),None.VariantListRepresentationflattens a nested variant into its leaves, each at the path of selectors ((0)/[price]) that reaches it, and back.
Equality
Variant is a class without value equality: two variants are equal only if they are the same
object. Compare the kind and the getter's value, or the printed form.