1 import mir.ndslice.topology: map; 2 import mir.array.allocation: array; 3 4 IonExtAlgebraic value; 5 6 StringMap!IonExtAlgebraic object; 7 8 // Default 9 assert(value._is!IonNull); 10 assert(value.get!IonNull == IonTypeCode.null_.IonNull); 11 assert(value.kind == IonExtAlgebraic.Kind.null_); 12 13 // Boolean 14 value = object["bool"] = true; 15 assert(value == true); 16 assert(value.kind == IonExtAlgebraic.Kind.boolean); 17 // access 18 assert(value.boolean == true); 19 assert(value.get!bool == true); 20 assert(value.get!"boolean" == true); 21 assert(value.get!(IonExtAlgebraic.Kind.boolean) == true); 22 // nothrow access 23 assert(value.trustedGet!bool == true); 24 assert(value.trustedGet!"boolean" == true); 25 assert(value.trustedGet!(IonExtAlgebraic.Kind.boolean) == true); 26 // checks 27 assert(!value._is!string); 28 assert(value._is!bool); 29 assert(value._is!"boolean"); 30 assert(value._is!(IonExtAlgebraic.Kind.boolean)); 31 32 // Null 33 value = object["null"] = IonTypeCode.string.IonNull; 34 assert(value._is!IonNull); 35 assert(value == IonTypeCode.string.IonNull); 36 assert(value.kind == IonExtAlgebraic.Kind.null_); 37 assert(value.null_ == IonTypeCode.string.IonNull); 38 assert(value.get!IonNull == IonTypeCode.string.IonNull); 39 assert(value.get!(IonExtAlgebraic.Kind.null_) == IonTypeCode.string.IonNull); 40 41 // String 42 value = object["string"] = "s"; 43 assert(value.kind == IonExtAlgebraic.Kind.string); 44 assert(value == "s"); 45 // access 46 // Yep, `string` here is an alias to `get!(immutable(char)[])` method 47 assert(value.string == "s"); 48 // `string` here is an alias of type `immutable(char)[]` 49 assert(value.get!string == "s"); 50 assert(value.get!"string" == "s"); 51 // finally, `string` here is an enum meber 52 assert(value.get!(IonExtAlgebraic.Kind.string) == "s"); 53 54 // Integer 55 value = object["integer"] = 4; 56 assert(value.kind == IonExtAlgebraic.Kind.integer); 57 assert(value == 4); 58 assert(value != 4.0); 59 assert(value.integer == 4); 60 61 // Float 62 value = object["float"] = 3.0; 63 assert(value.kind == IonExtAlgebraic.Kind.float_); 64 assert(value != 3); 65 assert(value == 3.0); 66 assert(value.float_ == 3.0); 67 68 // Array 69 IonExtAlgebraic[] arr = [0, 1, 2, 3, 4].map!IonExtAlgebraic.array; 70 71 value = object["array"] = arr; 72 assert(value.kind == IonExtAlgebraic.Kind.array); 73 assert(value == arr); 74 assert(value == [0, 1, 2, 3, 4].map!IonExtAlgebraic.array); 75 assert(value.array[3] == 3); 76 77 // Object 78 assert(object.keys == ["bool", "null", "string", "integer", "float", "array"]); 79 object.values[0] = "false"; 80 assert(object["bool"] == "false"); // it is a string now 81 object.remove("bool"); // remove the member 82 83 value = object["array"] = object; 84 assert(value.kind == IonExtAlgebraic.Kind.object); 85 assert(value.object.keys is object.keys); 86 87 IonExtAlgebraic[string] aa = object.toAA; 88 object = aa.StringMap!IonExtAlgebraic; 89 90 IonExtAlgebraic fromAA = ["a" : IonExtAlgebraic(3), "b" : IonExtAlgebraic("b")]; 91 assert(fromAA.object["a"] == 3); 92 assert(fromAA.object["b"] == "b"); 93 94 auto annotated = Annotated!IonExtAlgebraic(["birthday"], Timestamp("2001-01-01")); 95 value = annotated; 96 assert(value == annotated); 97 value = annotated.IonExtAlgebraic; 98 assert(value == annotated);
Ion tagged algebraic alias.
The example below shows only the basic features. Advanced API to work with algebraic types can be found at $(GMREF mir-core, mir,algebraic). See also mir.string_map - ordered string-value associative array.