diff --git a/src/dotVariant.Generator.Test/samples/Variant-class-nullable-disable.out.cs b/src/dotVariant.Generator.Test/samples/Variant-class-nullable-disable.out.cs
index 85c07cc..4d59c3f 100644
--- a/src/dotVariant.Generator.Test/samples/Variant-class-nullable-disable.out.cs
+++ b/src/dotVariant.Generator.Test/samples/Variant-class-nullable-disable.out.cs
@@ -86,6 +86,11 @@ public static Variant_class_nullable_disable Create(string s)
public bool IsEmpty
=> _variant.IsEmpty;
+ ///
+ [global::System.Diagnostics.DebuggerNonUserCode]
+ public byte Index
+ => _variant.Index;
+
///
[global::System.Diagnostics.DebuggerNonUserCode]
public override bool Equals(object other)
@@ -113,6 +118,11 @@ public override int GetHashCode()
public override string ToString()
=> _variant.ToString();
+ ///
+ [global::System.Diagnostics.DebuggerNonUserCode]
+ public int UnsafeGet(global::dotVariant.Accessor._1 accessor)
+ => _variant.UnsafeGet(accessor);
+
///
[global::System.Diagnostics.DebuggerNonUserCode]
public bool TryMatch(out int i)
@@ -153,6 +163,11 @@ public TResult Match(global::System.Func i, TResult _)
public TResult Match(global::System.Func i, global::System.Func _)
=> _variant.Match(i, _);
+ ///
+ [global::System.Diagnostics.DebuggerNonUserCode]
+ public float UnsafeGet(global::dotVariant.Accessor._2 accessor)
+ => _variant.UnsafeGet(accessor);
+
///
[global::System.Diagnostics.DebuggerNonUserCode]
public bool TryMatch(out float f)
@@ -193,6 +208,11 @@ public TResult Match(global::System.Func f, TResult _)
public TResult Match(global::System.Func f, global::System.Func _)
=> _variant.Match(f, _);
+ ///
+ [global::System.Diagnostics.DebuggerNonUserCode]
+ public string UnsafeGet(global::dotVariant.Accessor._3 accessor)
+ => _variant.UnsafeGet(accessor);
+
///
[global::System.Diagnostics.DebuggerNonUserCode]
public bool TryMatch(out string s)
@@ -266,23 +286,6 @@ public __DebuggerTypeProxy(Variant_class_nullable_disable v)
#pragma warning restore 8604, 8625
}
}
-
- [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
- [global::System.Diagnostics.DebuggerNonUserCode]
- public static explicit operator global::dotVariant.GeneratorSupport.Discriminator(Variant_class_nullable_disable v)
- => (global::dotVariant.GeneratorSupport.Discriminator)v._variant;
- [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
- [global::System.Diagnostics.DebuggerNonUserCode]
- public static explicit operator global::dotVariant.GeneratorSupport.Accessor_1(Variant_class_nullable_disable v)
- => (global::dotVariant.GeneratorSupport.Accessor_1)v._variant;
- [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
- [global::System.Diagnostics.DebuggerNonUserCode]
- public static explicit operator global::dotVariant.GeneratorSupport.Accessor_2(Variant_class_nullable_disable v)
- => (global::dotVariant.GeneratorSupport.Accessor_2)v._variant;
- [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
- [global::System.Diagnostics.DebuggerNonUserCode]
- public static explicit operator global::dotVariant.GeneratorSupport.Accessor_3(Variant_class_nullable_disable v)
- => (global::dotVariant.GeneratorSupport.Accessor_3)v._variant;
}
}
@@ -318,38 +321,35 @@ public Union(string value)
}
private readonly Union _x;
- private readonly byte _n;
+
+ ///
+ /// The 1-based index of the currently stored type,
+ /// counted left-ro-right from the parameter list.
+ /// 0 if the variant is empty.
+ ///
+ public readonly byte Index;
public Variant_class_nullable_disable(int i)
{
- _n = 1;
+ Index = 1;
_x = new Union(i);
}
public Variant_class_nullable_disable(float f)
{
- _n = 2;
+ Index = 2;
_x = new Union(f);
}
public Variant_class_nullable_disable(string s)
{
- _n = 3;
+ Index = 3;
_x = new Union(s);
}
- public static explicit operator global::dotVariant.GeneratorSupport.Discriminator(in Variant_class_nullable_disable v)
- => (global::dotVariant.GeneratorSupport.Discriminator)v._n;
- public static explicit operator global::dotVariant.GeneratorSupport.Accessor_1(in Variant_class_nullable_disable v)
- => new global::dotVariant.GeneratorSupport.Accessor_1(v._x._1);
- public static explicit operator global::dotVariant.GeneratorSupport.Accessor_2(in Variant_class_nullable_disable v)
- => new global::dotVariant.GeneratorSupport.Accessor_2(v._x._2);
- public static explicit operator global::dotVariant.GeneratorSupport.Accessor_3(in Variant_class_nullable_disable v)
- => new global::dotVariant.GeneratorSupport.Accessor_3(v._x._3);
-
///
/// if Variant_class_nullable_disable was constructed without a value.
///
- public bool IsEmpty => _n == 0;
+ public bool IsEmpty => this.Index == 0;
///
/// The string representation of the stored value's type.
@@ -358,7 +358,7 @@ public string TypeString
{
get
{
- switch (_n)
+ switch (this.Index)
{
case 0:
return "";
@@ -379,7 +379,7 @@ public string TypeString
///
public override string ToString()
{
- switch (_n)
+ switch (this.Index)
{
case 0:
return "";
@@ -401,7 +401,7 @@ public object AsObject
{
get
{
- switch (_n)
+ switch (this.Index)
{
case 0:
return null;
@@ -419,11 +419,11 @@ public object AsObject
public bool Equals(in Variant_class_nullable_disable other)
{
- if (_n != other._n)
+ if (this.Index != other.Index)
{
return false;
}
- switch (_n)
+ switch (Index)
{
case 0:
return true;
@@ -440,7 +440,7 @@ public bool Equals(in Variant_class_nullable_disable other)
public override int GetHashCode()
{
- switch (_n)
+ switch (this.Index)
{
case 0:
return 0;
@@ -455,6 +455,16 @@ public override int GetHashCode()
}
}
+ ///
+ /// Retrieve the stored value assuming it is of type .
+ ///
+ /// Only call this if you have ensured that Index == 1,
+ /// otherwise the correctness of the returned value is not guaranteed,
+ /// nor that any value is returned at all.
+ ///
+ public int UnsafeGet(global::dotVariant.Accessor._1 _)
+ => _x._1;
+
///
/// Retrieve the value stored within Variant_class_nullable_disable if it is of type .
///
@@ -462,8 +472,8 @@ public override int GetHashCode()
/// if Variant_class_nullable_disable contained a value of type .
public bool TryMatch(out int i)
{
- i = _n == 1 ? _x._1 : default;
- return _n == 1;
+ i = this.Index == 1 ? _x._1 : default;
+ return this.Index == 1;
}
///
@@ -474,7 +484,7 @@ public bool TryMatch(out int i)
/// Any exception thrown from is rethrown.
public bool TryMatch(global::System.Action i)
{
- if (_n == 1)
+ if (this.Index == 1)
{
i(_x._1);
return true;
@@ -490,7 +500,7 @@ public bool TryMatch(global::System.Action i)
/// Variant_class_nullable_disable does not contain a value of type .
public void Match(out int i)
{
- if (_n == 1)
+ if (this.Index == 1)
{
i = _x._1;
return;
@@ -507,7 +517,7 @@ public void Match(out int i)
/// Any exception thrown from is rethrown.
public void Match(global::System.Action i)
{
- if (_n == 1)
+ if (this.Index == 1)
{
i(_x._1);
return;
@@ -524,7 +534,7 @@ public void Match(global::System.Action i)
/// Any exception thrown from or is rethrown.
public void Match(global::System.Action i, global::System.Action _)
{
- if (_n == 1)
+ if (this.Index == 1)
{
i(_x._1);
}
@@ -544,7 +554,7 @@ public void Match(global::System.Action i, global::System.Action _)
/// Any exception thrown from is rethrown.
public TResult Match(global::System.Func i)
{
- if (_n == 1)
+ if (this.Index == 1)
{
return i(_x._1);
}
@@ -561,7 +571,7 @@ public TResult Match(global::System.Func i)
/// Any exception thrown from or is rethrown.
public TResult Match(global::System.Func i, TResult _)
{
- return _n == 1 ? i(_x._1) : _;
+ return this.Index == 1 ? i(_x._1) : _;
}
///
@@ -573,8 +583,18 @@ public TResult Match(global::System.Func i, TResult _)
/// Any exception thrown from or is rethrown.
public TResult Match(global::System.Func i, global::System.Func _)
{
- return _n == 1 ? i(_x._1) : _();
+ return this.Index == 1 ? i(_x._1) : _();
}
+ ///
+ /// Retrieve the stored value assuming it is of type .
+ ///
+ /// Only call this if you have ensured that Index == 2,
+ /// otherwise the correctness of the returned value is not guaranteed,
+ /// nor that any value is returned at all.
+ ///
+ public float UnsafeGet(global::dotVariant.Accessor._2 _)
+ => _x._2;
+
///
/// Retrieve the value stored within Variant_class_nullable_disable if it is of type .
///
@@ -582,8 +602,8 @@ public TResult Match(global::System.Func i, global::Syste
/// if Variant_class_nullable_disable contained a value of type .
public bool TryMatch(out float f)
{
- f = _n == 2 ? _x._2 : default;
- return _n == 2;
+ f = this.Index == 2 ? _x._2 : default;
+ return this.Index == 2;
}
///
@@ -594,7 +614,7 @@ public bool TryMatch(out float f)
/// Any exception thrown from is rethrown.
public bool TryMatch(global::System.Action f)
{
- if (_n == 2)
+ if (this.Index == 2)
{
f(_x._2);
return true;
@@ -610,7 +630,7 @@ public bool TryMatch(global::System.Action f)
/// Variant_class_nullable_disable does not contain a value of type .
public void Match(out float f)
{
- if (_n == 2)
+ if (this.Index == 2)
{
f = _x._2;
return;
@@ -627,7 +647,7 @@ public void Match(out float f)
/// Any exception thrown from is rethrown.
public void Match(global::System.Action f)
{
- if (_n == 2)
+ if (this.Index == 2)
{
f(_x._2);
return;
@@ -644,7 +664,7 @@ public void Match(global::System.Action f)
/// Any exception thrown from or is rethrown.
public void Match(global::System.Action f, global::System.Action _)
{
- if (_n == 2)
+ if (this.Index == 2)
{
f(_x._2);
}
@@ -664,7 +684,7 @@ public void Match(global::System.Action f, global::System.Action _)
/// Any exception thrown from is rethrown.
public TResult Match(global::System.Func f)
{
- if (_n == 2)
+ if (this.Index == 2)
{
return f(_x._2);
}
@@ -681,7 +701,7 @@ public TResult Match(global::System.Func f)
/// Any exception thrown from or is rethrown.
public TResult Match(global::System.Func f, TResult _)
{
- return _n == 2 ? f(_x._2) : _;
+ return this.Index == 2 ? f(_x._2) : _;
}
///
@@ -693,8 +713,18 @@ public TResult Match(global::System.Func f, TResult _)
/// Any exception thrown from or is rethrown.
public TResult Match(global::System.Func f, global::System.Func _)
{
- return _n == 2 ? f(_x._2) : _();
+ return this.Index == 2 ? f(_x._2) : _();
}
+ ///
+ /// Retrieve the stored value assuming it is of type .
+ ///
+ /// Only call this if you have ensured that Index == 3,
+ /// otherwise the correctness of the returned value is not guaranteed,
+ /// nor that any value is returned at all.
+ ///
+ public string UnsafeGet(global::dotVariant.Accessor._3 _)
+ => _x._3;
+
///
/// Retrieve the value stored within Variant_class_nullable_disable if it is of type .
///
@@ -702,8 +732,8 @@ public TResult Match(global::System.Func f, global::Sys
/// if Variant_class_nullable_disable contained a value of type .
public bool TryMatch(out string s)
{
- s = _n == 3 ? _x._3 : default;
- return _n == 3;
+ s = this.Index == 3 ? _x._3 : default;
+ return this.Index == 3;
}
///
@@ -714,7 +744,7 @@ public bool TryMatch(out string s)
/// Any exception thrown from is rethrown.
public bool TryMatch(global::System.Action s)
{
- if (_n == 3)
+ if (this.Index == 3)
{
s(_x._3);
return true;
@@ -730,7 +760,7 @@ public bool TryMatch(global::System.Action s)
/// Variant_class_nullable_disable does not contain a value of type .
public void Match(out string s)
{
- if (_n == 3)
+ if (this.Index == 3)
{
s = _x._3;
return;
@@ -747,7 +777,7 @@ public void Match(out string s)
/// Any exception thrown from is rethrown.
public void Match(global::System.Action s)
{
- if (_n == 3)
+ if (this.Index == 3)
{
s(_x._3);
return;
@@ -764,7 +794,7 @@ public void Match(global::System.Action s)
/// Any exception thrown from or is rethrown.
public void Match(global::System.Action s, global::System.Action _)
{
- if (_n == 3)
+ if (this.Index == 3)
{
s(_x._3);
}
@@ -784,7 +814,7 @@ public void Match(global::System.Action s, global::System.Action _)
/// Any exception thrown from is rethrown.
public TResult Match(global::System.Func s)
{
- if (_n == 3)
+ if (this.Index == 3)
{
return s(_x._3);
}
@@ -801,7 +831,7 @@ public TResult Match(global::System.Func s)
/// Any exception thrown from or is rethrown.
public TResult Match(global::System.Func s, TResult _)
{
- return _n == 3 ? s(_x._3) : _;
+ return this.Index == 3 ? s(_x._3) : _;
}
///
@@ -813,7 +843,7 @@ public TResult Match(global::System.Func s, TResult _)
/// Any exception thrown from or is rethrown.
public TResult Match(global::System.Func s, global::System.Func _)
{
- return _n == 3 ? s(_x._3) : _();
+ return this.Index == 3 ? s(_x._3) : _();
}
///
@@ -827,7 +857,7 @@ public TResult Match(global::System.Func s, global::Sy
/// Any exception thrown from a delegate is rethrown.
public void Visit(global::System.Action i, global::System.Action f, global::System.Action s, global::System.Action _)
{
- switch (_n)
+ switch (this.Index)
{
case 0:
_();
@@ -858,7 +888,7 @@ public void Visit(global::System.Action i, global::System.Action f,
/// Any exception thrown from a delegate is rethrown.
public void Visit(global::System.Action i, global::System.Action f, global::System.Action s)
{
- switch (_n)
+ switch (this.Index)
{
case 0:
global::dotVariant.GeneratorSupport.Errors.ThrowEmptyError("Foo.Variant_class_nullable_disable");
@@ -890,7 +920,7 @@ public void Visit(global::System.Action i, global::System.Action f,
/// The return type of all delegates, and by extension the return type of this function.
public TResult Visit(global::System.Func i, global::System.Func f, global::System.Func s, global::System.Func _)
{
- switch (_n)
+ switch (this.Index)
{
case 0:
return _();
@@ -917,7 +947,7 @@ public TResult Visit(global::System.Func i, global::Syste
/// The return type of all delegates, and by extension the return type of this function.
public TResult Visit(global::System.Func i, global::System.Func f, global::System.Func s)
{
- switch (_n)
+ switch (this.Index)
{
case 0:
return global::dotVariant.GeneratorSupport.Errors.ThrowEmptyError("Foo.Variant_class_nullable_disable");
@@ -955,9 +985,9 @@ public static partial class Variant_class_nullable_disableEx
{
foreach (var variant in source)
{
- if (((int)(global::dotVariant.GeneratorSupport.Discriminator)variant) == 1)
+ if (variant.Index == 1)
{
- yield return i(((global::dotVariant.GeneratorSupport.Accessor_1)variant).Value);
+ yield return i(variant.UnsafeGet(new global::dotVariant.Accessor._1()));
}
}
}
@@ -977,9 +1007,9 @@ public static partial class Variant_class_nullable_disableEx
{
foreach (var variant in source)
{
- if (((int)(global::dotVariant.GeneratorSupport.Discriminator)variant) == 2)
+ if (variant.Index == 2)
{
- yield return f(((global::dotVariant.GeneratorSupport.Accessor_2)variant).Value);
+ yield return f(variant.UnsafeGet(new global::dotVariant.Accessor._2()));
}
}
}
@@ -999,9 +1029,9 @@ public static partial class Variant_class_nullable_disableEx
{
foreach (var variant in source)
{
- if (((int)(global::dotVariant.GeneratorSupport.Discriminator)variant) == 3)
+ if (variant.Index == 3)
{
- yield return s(((global::dotVariant.GeneratorSupport.Accessor_3)variant).Value);
+ yield return s(variant.UnsafeGet(new global::dotVariant.Accessor._3()));
}
}
}
@@ -1024,9 +1054,9 @@ public static partial class Variant_class_nullable_disableEx
{
foreach (var variant in source)
{
- if (((int)(global::dotVariant.GeneratorSupport.Discriminator)variant) == 1)
+ if (variant.Index == 1)
{
- yield return i(((global::dotVariant.GeneratorSupport.Accessor_1)variant).Value);
+ yield return i(variant.UnsafeGet(new global::dotVariant.Accessor._1()));
}
else
{
@@ -1052,9 +1082,9 @@ public static partial class Variant_class_nullable_disableEx
{
foreach (var variant in source)
{
- if (((int)(global::dotVariant.GeneratorSupport.Discriminator)variant) == 2)
+ if (variant.Index == 2)
{
- yield return f(((global::dotVariant.GeneratorSupport.Accessor_2)variant).Value);
+ yield return f(variant.UnsafeGet(new global::dotVariant.Accessor._2()));
}
else
{
@@ -1080,9 +1110,9 @@ public static partial class Variant_class_nullable_disableEx
{
foreach (var variant in source)
{
- if (((int)(global::dotVariant.GeneratorSupport.Discriminator)variant) == 3)
+ if (variant.Index == 3)
{
- yield return s(((global::dotVariant.GeneratorSupport.Accessor_3)variant).Value);
+ yield return s(variant.UnsafeGet(new global::dotVariant.Accessor._3()));
}
else
{
@@ -1109,9 +1139,9 @@ public static partial class Variant_class_nullable_disableEx
{
foreach (var variant in source)
{
- if (((int)(global::dotVariant.GeneratorSupport.Discriminator)variant) == 1)
+ if (variant.Index == 1)
{
- yield return i(((global::dotVariant.GeneratorSupport.Accessor_1)variant).Value);
+ yield return i(variant.UnsafeGet(new global::dotVariant.Accessor._1()));
}
else
{
@@ -1137,9 +1167,9 @@ public static partial class Variant_class_nullable_disableEx
{
foreach (var variant in source)
{
- if (((int)(global::dotVariant.GeneratorSupport.Discriminator)variant) == 2)
+ if (variant.Index == 2)
{
- yield return f(((global::dotVariant.GeneratorSupport.Accessor_2)variant).Value);
+ yield return f(variant.UnsafeGet(new global::dotVariant.Accessor._2()));
}
else
{
@@ -1165,9 +1195,9 @@ public static partial class Variant_class_nullable_disableEx
{
foreach (var variant in source)
{
- if (((int)(global::dotVariant.GeneratorSupport.Discriminator)variant) == 3)
+ if (variant.Index == 3)
{
- yield return s(((global::dotVariant.GeneratorSupport.Accessor_3)variant).Value);
+ yield return s(variant.UnsafeGet(new global::dotVariant.Accessor._3()));
}
else
{
@@ -1196,19 +1226,19 @@ public static partial class Variant_class_nullable_disableEx
{
foreach (var variant in source)
{
- switch (((int)(global::dotVariant.GeneratorSupport.Discriminator)variant))
+ switch (variant.Index)
{
case 0:
global::dotVariant.GeneratorSupport.Errors.ThrowEmptyError("Foo.Variant_class_nullable_disable");
yield break;
case 1:
- yield return i(((global::dotVariant.GeneratorSupport.Accessor_1)variant).Value);
+ yield return i(variant.UnsafeGet(new global::dotVariant.Accessor._1()));
break;
case 2:
- yield return f(((global::dotVariant.GeneratorSupport.Accessor_2)variant).Value);
+ yield return f(variant.UnsafeGet(new global::dotVariant.Accessor._2()));
break;
case 3:
- yield return s(((global::dotVariant.GeneratorSupport.Accessor_3)variant).Value);
+ yield return s(variant.UnsafeGet(new global::dotVariant.Accessor._3()));
break;
default:
global::dotVariant.GeneratorSupport.Errors.ThrowInternalError("Foo.Variant_class_nullable_disable");
@@ -1237,19 +1267,19 @@ public static partial class Variant_class_nullable_disableEx
{
foreach (var variant in source)
{
- switch (((int)(global::dotVariant.GeneratorSupport.Discriminator)variant))
+ switch (variant.Index)
{
case 0:
yield return _();
break;
case 1:
- yield return i(((global::dotVariant.GeneratorSupport.Accessor_1)variant).Value);
+ yield return i(variant.UnsafeGet(new global::dotVariant.Accessor._1()));
break;
case 2:
- yield return f(((global::dotVariant.GeneratorSupport.Accessor_2)variant).Value);
+ yield return f(variant.UnsafeGet(new global::dotVariant.Accessor._2()));
break;
case 3:
- yield return s(((global::dotVariant.GeneratorSupport.Accessor_3)variant).Value);
+ yield return s(variant.UnsafeGet(new global::dotVariant.Accessor._3()));
break;
default:
global::dotVariant.GeneratorSupport.Errors.ThrowInternalError("Foo.Variant_class_nullable_disable");
@@ -1277,8 +1307,8 @@ public static partial class Variant_class_nullable_disableEx
global::System.Func i)
{
return global::System.Reactive.Linq.Observable.Select(
- global::System.Reactive.Linq.Observable.Where(source, _variant => ((int)(global::dotVariant.GeneratorSupport.Discriminator)_variant) == 1),
- _variant => i(((global::dotVariant.GeneratorSupport.Accessor_1)_variant).Value));
+ global::System.Reactive.Linq.Observable.Where(source, _variant => _variant.Index == 1),
+ _variant => i(_variant.UnsafeGet(new global::dotVariant.Accessor._1())));
}
///
/// Projects each element of an observable sequence
@@ -1294,8 +1324,8 @@ public static partial class Variant_class_nullable_disableEx
global::System.Func f)
{
return global::System.Reactive.Linq.Observable.Select(
- global::System.Reactive.Linq.Observable.Where(source, _variant => ((int)(global::dotVariant.GeneratorSupport.Discriminator)_variant) == 2),
- _variant => f(((global::dotVariant.GeneratorSupport.Accessor_2)_variant).Value));
+ global::System.Reactive.Linq.Observable.Where(source, _variant => _variant.Index == 2),
+ _variant => f(_variant.UnsafeGet(new global::dotVariant.Accessor._2())));
}
///
/// Projects each element of an observable sequence
@@ -1311,8 +1341,8 @@ public static partial class Variant_class_nullable_disableEx
global::System.Func s)
{
return global::System.Reactive.Linq.Observable.Select(
- global::System.Reactive.Linq.Observable.Where(source, _variant => ((int)(global::dotVariant.GeneratorSupport.Discriminator)_variant) == 3),
- _variant => s(((global::dotVariant.GeneratorSupport.Accessor_3)_variant).Value));
+ global::System.Reactive.Linq.Observable.Where(source, _variant => _variant.Index == 3),
+ _variant => s(_variant.UnsafeGet(new global::dotVariant.Accessor._3())));
}
///
@@ -1332,9 +1362,9 @@ public static partial class Variant_class_nullable_disableEx
{
return global::System.Reactive.Linq.Observable.Select(source, _variant =>
{
- if (((int)(global::dotVariant.GeneratorSupport.Discriminator)_variant) == 1)
+ if (_variant.Index == 1)
{
- return i(((global::dotVariant.GeneratorSupport.Accessor_1)_variant).Value);
+ return i(_variant.UnsafeGet(new global::dotVariant.Accessor._1()));
}
else
{
@@ -1359,9 +1389,9 @@ public static partial class Variant_class_nullable_disableEx
{
return global::System.Reactive.Linq.Observable.Select(source, _variant =>
{
- if (((int)(global::dotVariant.GeneratorSupport.Discriminator)_variant) == 2)
+ if (_variant.Index == 2)
{
- return f(((global::dotVariant.GeneratorSupport.Accessor_2)_variant).Value);
+ return f(_variant.UnsafeGet(new global::dotVariant.Accessor._2()));
}
else
{
@@ -1386,9 +1416,9 @@ public static partial class Variant_class_nullable_disableEx
{
return global::System.Reactive.Linq.Observable.Select(source, _variant =>
{
- if (((int)(global::dotVariant.GeneratorSupport.Discriminator)_variant) == 3)
+ if (_variant.Index == 3)
{
- return s(((global::dotVariant.GeneratorSupport.Accessor_3)_variant).Value);
+ return s(_variant.UnsafeGet(new global::dotVariant.Accessor._3()));
}
else
{
@@ -1414,9 +1444,9 @@ public static partial class Variant_class_nullable_disableEx
{
return global::System.Reactive.Linq.Observable.Select(source, _variant =>
{
- if (((int)(global::dotVariant.GeneratorSupport.Discriminator)_variant) == 1)
+ if (_variant.Index == 1)
{
- return i(((global::dotVariant.GeneratorSupport.Accessor_1)_variant).Value);
+ return i(_variant.UnsafeGet(new global::dotVariant.Accessor._1()));
}
else
{
@@ -1441,9 +1471,9 @@ public static partial class Variant_class_nullable_disableEx
{
return global::System.Reactive.Linq.Observable.Select(source, _variant =>
{
- if (((int)(global::dotVariant.GeneratorSupport.Discriminator)_variant) == 2)
+ if (_variant.Index == 2)
{
- return f(((global::dotVariant.GeneratorSupport.Accessor_2)_variant).Value);
+ return f(_variant.UnsafeGet(new global::dotVariant.Accessor._2()));
}
else
{
@@ -1468,9 +1498,9 @@ public static partial class Variant_class_nullable_disableEx
{
return global::System.Reactive.Linq.Observable.Select(source, _variant =>
{
- if (((int)(global::dotVariant.GeneratorSupport.Discriminator)_variant) == 3)
+ if (_variant.Index == 3)
{
- return s(((global::dotVariant.GeneratorSupport.Accessor_3)_variant).Value);
+ return s(_variant.UnsafeGet(new global::dotVariant.Accessor._3()));
}
else
{
@@ -1496,16 +1526,16 @@ public static partial class Variant_class_nullable_disableEx
{
return global::System.Reactive.Linq.Observable.Select(source, _variant =>
{
- switch (((int)(global::dotVariant.GeneratorSupport.Discriminator)_variant))
+ switch (_variant.Index)
{
case 0:
return global::dotVariant.GeneratorSupport.Errors.ThrowEmptyError("Foo.Variant_class_nullable_disable");
case 1:
- return i(((global::dotVariant.GeneratorSupport.Accessor_1)_variant).Value);
+ return i(_variant.UnsafeGet(new global::dotVariant.Accessor._1()));
case 2:
- return f(((global::dotVariant.GeneratorSupport.Accessor_2)_variant).Value);
+ return f(_variant.UnsafeGet(new global::dotVariant.Accessor._2()));
case 3:
- return s(((global::dotVariant.GeneratorSupport.Accessor_3)_variant).Value);
+ return s(_variant.UnsafeGet(new global::dotVariant.Accessor._3()));
default:
return global::dotVariant.GeneratorSupport.Errors.ThrowInternalError("Foo.Variant_class_nullable_disable");
}
@@ -1531,16 +1561,16 @@ public static partial class Variant_class_nullable_disableEx
{
return global::System.Reactive.Linq.Observable.Select(source, _variant =>
{
- switch (((int)(global::dotVariant.GeneratorSupport.Discriminator)_variant))
+ switch (_variant.Index)
{
case 0:
return _();
case 1:
- return i(((global::dotVariant.GeneratorSupport.Accessor_1)_variant).Value);
+ return i(_variant.UnsafeGet(new global::dotVariant.Accessor._1()));
case 2:
- return f(((global::dotVariant.GeneratorSupport.Accessor_2)_variant).Value);
+ return f(_variant.UnsafeGet(new global::dotVariant.Accessor._2()));
case 3:
- return s(((global::dotVariant.GeneratorSupport.Accessor_3)_variant).Value);
+ return s(_variant.UnsafeGet(new global::dotVariant.Accessor._3()));
default:
return global::dotVariant.GeneratorSupport.Errors.ThrowInternalError("Foo.Variant_class_nullable_disable");
}
@@ -1698,7 +1728,7 @@ public void Dispose()
public void OnNext(global::Foo.Variant_class_nullable_disable _variant)
{
- switch (((int)(global::dotVariant.GeneratorSupport.Discriminator)_variant))
+ switch (_variant.Index)
{
case 0:
if (_accept0)
@@ -1711,13 +1741,13 @@ public void OnNext(global::Foo.Variant_class_nullable_disable _variant)
}
break;
case 1:
- Subject1.OnNext(((global::dotVariant.GeneratorSupport.Accessor_1)_variant).Value);
+ Subject1.OnNext(_variant.UnsafeGet(new global::dotVariant.Accessor._1()));
break;
case 2:
- Subject2.OnNext(((global::dotVariant.GeneratorSupport.Accessor_2)_variant).Value);
+ Subject2.OnNext(_variant.UnsafeGet(new global::dotVariant.Accessor._2()));
break;
case 3:
- Subject3.OnNext(((global::dotVariant.GeneratorSupport.Accessor_3)_variant).Value);
+ Subject3.OnNext(_variant.UnsafeGet(new global::dotVariant.Accessor._3()));
break;
default:
OnError(global::dotVariant.GeneratorSupport.Errors.MakeInternalError("Foo.Variant_class_nullable_disable"));
diff --git a/src/dotVariant.Generator.Test/samples/Variant-class-nullable-enable.out.cs b/src/dotVariant.Generator.Test/samples/Variant-class-nullable-enable.out.cs
index eed34aa..2574406 100644
--- a/src/dotVariant.Generator.Test/samples/Variant-class-nullable-enable.out.cs
+++ b/src/dotVariant.Generator.Test/samples/Variant-class-nullable-enable.out.cs
@@ -107,6 +107,11 @@ public static Variant_class_nullable_enable Create(global::System.Array? a)
public bool IsEmpty
=> _variant.IsEmpty;
+ ///
+ [global::System.Diagnostics.DebuggerNonUserCode]
+ public byte Index
+ => _variant.Index;
+
///
[global::System.Diagnostics.DebuggerNonUserCode]
public override bool Equals(object? other)
@@ -134,6 +139,11 @@ public override int GetHashCode()
public override string ToString()
=> _variant.ToString();
+ ///
+ [global::System.Diagnostics.DebuggerNonUserCode]
+ public int UnsafeGet(global::dotVariant.Accessor._1 accessor)
+ => _variant.UnsafeGet(accessor);
+
///
[global::System.Diagnostics.DebuggerNonUserCode]
public bool TryMatch([global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out int i)
@@ -174,6 +184,11 @@ public TResult Match(global::System.Func i, TResult _)
public TResult Match(global::System.Func i, global::System.Func _)
=> _variant.Match(i, _);
+ ///
+ [global::System.Diagnostics.DebuggerNonUserCode]
+ public float UnsafeGet(global::dotVariant.Accessor._2 accessor)
+ => _variant.UnsafeGet(accessor);
+
///
[global::System.Diagnostics.DebuggerNonUserCode]
public bool TryMatch([global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out float f)
@@ -214,6 +229,11 @@ public TResult Match(global::System.Func f, TResult _)
public TResult Match(global::System.Func f, global::System.Func _)
=> _variant.Match(f, _);
+ ///
+ [global::System.Diagnostics.DebuggerNonUserCode]
+ public string UnsafeGet(global::dotVariant.Accessor._3 accessor)
+ => _variant.UnsafeGet(accessor);
+
///
[global::System.Diagnostics.DebuggerNonUserCode]
public bool TryMatch([global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out string? s)
@@ -254,6 +274,11 @@ public TResult Match(global::System.Func s, TResult _)
public TResult Match(global::System.Func s, global::System.Func _)
=> _variant.Match(s, _);
+ ///
+ [global::System.Diagnostics.DebuggerNonUserCode]
+ public global::System.Array? UnsafeGet(global::dotVariant.Accessor._4 accessor)
+ => _variant.UnsafeGet(accessor);
+
///
[global::System.Diagnostics.DebuggerNonUserCode]
public bool TryMatch(out global::System.Array? a)
@@ -327,27 +352,6 @@ public __DebuggerTypeProxy(Variant_class_nullable_enable v)
#pragma warning restore 8604, 8625
}
}
-
- [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
- [global::System.Diagnostics.DebuggerNonUserCode]
- public static explicit operator global::dotVariant.GeneratorSupport.Discriminator(Variant_class_nullable_enable v)
- => (global::dotVariant.GeneratorSupport.Discriminator)v._variant;
- [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
- [global::System.Diagnostics.DebuggerNonUserCode]
- public static explicit operator global::dotVariant.GeneratorSupport.Accessor_1(Variant_class_nullable_enable v)
- => (global::dotVariant.GeneratorSupport.Accessor_1)v._variant;
- [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
- [global::System.Diagnostics.DebuggerNonUserCode]
- public static explicit operator global::dotVariant.GeneratorSupport.Accessor_2(Variant_class_nullable_enable v)
- => (global::dotVariant.GeneratorSupport.Accessor_2)v._variant;
- [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
- [global::System.Diagnostics.DebuggerNonUserCode]
- public static explicit operator global::dotVariant.GeneratorSupport.Accessor_3(Variant_class_nullable_enable v)
- => (global::dotVariant.GeneratorSupport.Accessor_3)v._variant;
- [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
- [global::System.Diagnostics.DebuggerNonUserCode]
- public static explicit operator global::dotVariant.GeneratorSupport.Accessor_4(Variant_class_nullable_enable v)
- => (global::dotVariant.GeneratorSupport.Accessor_4)v._variant;
}
}
@@ -394,45 +398,40 @@ public Union(global::System.Array? value)
}
private readonly Union _x;
- private readonly byte _n;
+
+ ///
+ /// The 1-based index of the currently stored type,
+ /// counted left-ro-right from the parameter list.
+ /// 0 if the variant is empty.
+ ///
+ public readonly byte Index;
public Variant_class_nullable_enable(int i)
{
- _n = 1;
+ Index = 1;
_x = new Union(i);
}
public Variant_class_nullable_enable(float f)
{
- _n = 2;
+ Index = 2;
_x = new Union(f);
}
public Variant_class_nullable_enable(string s)
{
- _n = 3;
+ Index = 3;
_x = new Union(s);
}
public Variant_class_nullable_enable(global::System.Array? a)
{
- _n = 4;
+ Index = 4;
_x = new Union(a);
}
- public static explicit operator global::dotVariant.GeneratorSupport.Discriminator(in Variant_class_nullable_enable v)
- => (global::dotVariant.GeneratorSupport.Discriminator)v._n;
- public static explicit operator global::dotVariant.GeneratorSupport.Accessor_1(in Variant_class_nullable_enable v)
- => new global::dotVariant.GeneratorSupport.Accessor_1(v._x._1);
- public static explicit operator global::dotVariant.GeneratorSupport.Accessor_2(in Variant_class_nullable_enable v)
- => new global::dotVariant.GeneratorSupport.Accessor_2(v._x._2);
- public static explicit operator global::dotVariant.GeneratorSupport.Accessor_3(in Variant_class_nullable_enable v)
- => new global::dotVariant.GeneratorSupport.Accessor_3(v._x._3);
- public static explicit operator global::dotVariant.GeneratorSupport.Accessor_4(in Variant_class_nullable_enable v)
- => new global::dotVariant.GeneratorSupport.Accessor_4(v._x._4);
-
///
/// if Variant_class_nullable_enable was constructed without a value.
///
- public bool IsEmpty => _n == 0;
+ public bool IsEmpty => this.Index == 0;
///
/// The string representation of the stored value's type.
@@ -441,7 +440,7 @@ public string TypeString
{
get
{
- switch (_n)
+ switch (this.Index)
{
case 0:
return "";
@@ -464,7 +463,7 @@ public string TypeString
///
public override string ToString()
{
- switch (_n)
+ switch (this.Index)
{
case 0:
return "";
@@ -488,7 +487,7 @@ public object? AsObject
{
get
{
- switch (_n)
+ switch (this.Index)
{
case 0:
return null;
@@ -508,11 +507,11 @@ public object? AsObject
public bool Equals(in Variant_class_nullable_enable other)
{
- if (_n != other._n)
+ if (this.Index != other.Index)
{
return false;
}
- switch (_n)
+ switch (Index)
{
case 0:
return true;
@@ -531,7 +530,7 @@ public bool Equals(in Variant_class_nullable_enable other)
public override int GetHashCode()
{
- switch (_n)
+ switch (this.Index)
{
case 0:
return 0;
@@ -548,6 +547,16 @@ public override int GetHashCode()
}
}
+ ///
+ /// Retrieve the stored value assuming it is of type .
+ ///
+ /// Only call this if you have ensured that Index == 1,
+ /// otherwise the correctness of the returned value is not guaranteed,
+ /// nor that any value is returned at all.
+ ///
+ public int UnsafeGet(global::dotVariant.Accessor._1 _)
+ => _x._1;
+
///
/// Retrieve the value stored within Variant_class_nullable_enable if it is of type .
///
@@ -555,8 +564,8 @@ public override int GetHashCode()
/// if Variant_class_nullable_enable contained a value of type .
public bool TryMatch([global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out int i)
{
- i = _n == 1 ? _x._1 : default;
- return _n == 1;
+ i = this.Index == 1 ? _x._1 : default;
+ return this.Index == 1;
}
///
@@ -567,7 +576,7 @@ public bool TryMatch([global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
/// Any exception thrown from is rethrown.
public bool TryMatch(global::System.Action i)
{
- if (_n == 1)
+ if (this.Index == 1)
{
i(_x._1);
return true;
@@ -583,7 +592,7 @@ public bool TryMatch(global::System.Action i)
/// Variant_class_nullable_enable does not contain a value of type .
public void Match([global::System.Diagnostics.CodeAnalysis.NotNull] out int i)
{
- if (_n == 1)
+ if (this.Index == 1)
{
i = _x._1;
return;
@@ -600,7 +609,7 @@ public void Match([global::System.Diagnostics.CodeAnalysis.NotNull] out int i)
/// Any exception thrown from is rethrown.
public void Match(global::System.Action i)
{
- if (_n == 1)
+ if (this.Index == 1)
{
i(_x._1);
return;
@@ -617,7 +626,7 @@ public void Match(global::System.Action i)
/// Any exception thrown from or is rethrown.
public void Match(global::System.Action i, global::System.Action _)
{
- if (_n == 1)
+ if (this.Index == 1)
{
i(_x._1);
}
@@ -637,7 +646,7 @@ public void Match(global::System.Action i, global::System.Action _)
/// Any exception thrown from is rethrown.
public TResult Match(global::System.Func i)
{
- if (_n == 1)
+ if (this.Index == 1)
{
return i(_x._1);
}
@@ -654,7 +663,7 @@ public TResult Match(global::System.Func i)
/// Any exception thrown from or is rethrown.
public TResult Match(global::System.Func i, TResult _)
{
- return _n == 1 ? i(_x._1) : _;
+ return this.Index == 1 ? i(_x._1) : _;
}
///
@@ -666,8 +675,18 @@ public TResult Match(global::System.Func i, TResult _)
/// Any exception thrown from or is rethrown.
public TResult Match(global::System.Func i, global::System.Func _)
{
- return _n == 1 ? i(_x._1) : _();
+ return this.Index == 1 ? i(_x._1) : _();
}
+ ///
+ /// Retrieve the stored value assuming it is of type .
+ ///
+ /// Only call this if you have ensured that Index == 2,
+ /// otherwise the correctness of the returned value is not guaranteed,
+ /// nor that any value is returned at all.
+ ///
+ public float UnsafeGet(global::dotVariant.Accessor._2 _)
+ => _x._2;
+
///
/// Retrieve the value stored within Variant_class_nullable_enable if it is of type .
///
@@ -675,8 +694,8 @@ public TResult Match(global::System.Func i, global::Syste
/// if Variant_class_nullable_enable contained a value of type .
public bool TryMatch([global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out float f)
{
- f = _n == 2 ? _x._2 : default;
- return _n == 2;
+ f = this.Index == 2 ? _x._2 : default;
+ return this.Index == 2;
}
///
@@ -687,7 +706,7 @@ public bool TryMatch([global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
/// Any exception thrown from is rethrown.
public bool TryMatch(global::System.Action f)
{
- if (_n == 2)
+ if (this.Index == 2)
{
f(_x._2);
return true;
@@ -703,7 +722,7 @@ public bool TryMatch(global::System.Action f)
/// Variant_class_nullable_enable does not contain a value of type .
public void Match([global::System.Diagnostics.CodeAnalysis.NotNull] out float f)
{
- if (_n == 2)
+ if (this.Index == 2)
{
f = _x._2;
return;
@@ -720,7 +739,7 @@ public void Match([global::System.Diagnostics.CodeAnalysis.NotNull] out float f)
/// Any exception thrown from is rethrown.
public void Match(global::System.Action f)
{
- if (_n == 2)
+ if (this.Index == 2)
{
f(_x._2);
return;
@@ -737,7 +756,7 @@ public void Match(global::System.Action f)
/// Any exception thrown from or is rethrown.
public void Match(global::System.Action f, global::System.Action _)
{
- if (_n == 2)
+ if (this.Index == 2)
{
f(_x._2);
}
@@ -757,7 +776,7 @@ public void Match(global::System.Action f, global::System.Action _)
/// Any exception thrown from is rethrown.
public TResult Match(global::System.Func f)
{
- if (_n == 2)
+ if (this.Index == 2)
{
return f(_x._2);
}
@@ -774,7 +793,7 @@ public TResult Match(global::System.Func f)
/// Any exception thrown from or is rethrown.
public TResult Match(global::System.Func f, TResult _)
{
- return _n == 2 ? f(_x._2) : _;
+ return this.Index == 2 ? f(_x._2) : _;
}
///
@@ -786,8 +805,18 @@ public TResult Match(global::System.Func f, TResult _)
/// Any exception thrown from or is rethrown.
public TResult Match(global::System.Func f, global::System.Func _)
{
- return _n == 2 ? f(_x._2) : _();
+ return this.Index == 2 ? f(_x._2) : _();
}
+ ///
+ /// Retrieve the stored value assuming it is of type .
+ ///
+ /// Only call this if you have ensured that Index == 3,
+ /// otherwise the correctness of the returned value is not guaranteed,
+ /// nor that any value is returned at all.
+ ///
+ public string UnsafeGet(global::dotVariant.Accessor._3 _)
+ => _x._3;
+
///
/// Retrieve the value stored within Variant_class_nullable_enable if it is of type .
///
@@ -795,8 +824,8 @@ public TResult Match(global::System.Func f, global::Sys
/// if Variant_class_nullable_enable contained a value of type .
public bool TryMatch([global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out string? s)
{
- s = _n == 3 ? _x._3 : default;
- return _n == 3;
+ s = this.Index == 3 ? _x._3 : default;
+ return this.Index == 3;
}
///
@@ -807,7 +836,7 @@ public bool TryMatch([global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
/// Any exception thrown from is rethrown.
public bool TryMatch(global::System.Action s)
{
- if (_n == 3)
+ if (this.Index == 3)
{
s(_x._3);
return true;
@@ -823,7 +852,7 @@ public bool TryMatch(global::System.Action s)
/// Variant_class_nullable_enable does not contain a value of type .
public void Match([global::System.Diagnostics.CodeAnalysis.NotNull] out string? s)
{
- if (_n == 3)
+ if (this.Index == 3)
{
s = _x._3;
return;
@@ -840,7 +869,7 @@ public void Match([global::System.Diagnostics.CodeAnalysis.NotNull] out string?
/// Any exception thrown from is rethrown.
public void Match(global::System.Action s)
{
- if (_n == 3)
+ if (this.Index == 3)
{
s(_x._3);
return;
@@ -857,7 +886,7 @@ public void Match(global::System.Action s)
/// Any exception thrown from or is rethrown.
public void Match(global::System.Action s, global::System.Action _)
{
- if (_n == 3)
+ if (this.Index == 3)
{
s(_x._3);
}
@@ -877,7 +906,7 @@ public void Match(global::System.Action s, global::System.Action _)
/// Any exception thrown from is rethrown.
public TResult Match(global::System.Func s)
{
- if (_n == 3)
+ if (this.Index == 3)
{
return s(_x._3);
}
@@ -894,7 +923,7 @@ public TResult Match(global::System.Func s)
/// Any exception thrown from or is rethrown.
public TResult Match(global::System.Func s, TResult _)
{
- return _n == 3 ? s(_x._3) : _;
+ return this.Index == 3 ? s(_x._3) : _;
}
///
@@ -906,8 +935,18 @@ public TResult Match(global::System.Func s, TResult _)
/// Any exception thrown from or is rethrown.
public TResult Match(global::System.Func s, global::System.Func _)
{
- return _n == 3 ? s(_x._3) : _();
+ return this.Index == 3 ? s(_x._3) : _();
}
+ ///
+ /// Retrieve the stored value assuming it is of type .
+ ///
+ /// Only call this if you have ensured that Index == 4,
+ /// otherwise the correctness of the returned value is not guaranteed,
+ /// nor that any value is returned at all.
+ ///
+ public global::System.Array? UnsafeGet(global::dotVariant.Accessor._4 _)
+ => _x._4;
+
///
/// Retrieve the value stored within Variant_class_nullable_enable if it is of type .
///
@@ -915,8 +954,8 @@ public TResult Match(global::System.Func s, global::Sy
/// if Variant_class_nullable_enable contained a value of type .
public bool TryMatch(out global::System.Array? a)
{
- a = _n == 4 ? _x._4 : default;
- return _n == 4;
+ a = this.Index == 4 ? _x._4 : default;
+ return this.Index == 4;
}
///
@@ -927,7 +966,7 @@ public bool TryMatch(out global::System.Array? a)
/// Any exception thrown from is rethrown.
public bool TryMatch(global::System.Action a)
{
- if (_n == 4)
+ if (this.Index == 4)
{
a(_x._4);
return true;
@@ -943,7 +982,7 @@ public bool TryMatch(global::System.Action a)
/// Variant_class_nullable_enable does not contain a value of type .
public void Match(out global::System.Array? a)
{
- if (_n == 4)
+ if (this.Index == 4)
{
a = _x._4;
return;
@@ -960,7 +999,7 @@ public void Match(out global::System.Array? a)
/// Any exception thrown from is rethrown.
public void Match(global::System.Action a)
{
- if (_n == 4)
+ if (this.Index == 4)
{
a(_x._4);
return;
@@ -977,7 +1016,7 @@ public void Match(global::System.Action a)
/// Any exception thrown from or is rethrown.
public void Match(global::System.Action a, global::System.Action _)
{
- if (_n == 4)
+ if (this.Index == 4)
{
a(_x._4);
}
@@ -997,7 +1036,7 @@ public void Match(global::System.Action a, global::System
/// Any exception thrown from is rethrown.
public TResult Match(global::System.Func a)
{
- if (_n == 4)
+ if (this.Index == 4)
{
return a(_x._4);
}
@@ -1014,7 +1053,7 @@ public TResult Match(global::System.Func