Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new method WithType to add types to a OneOf #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
using System.Collections.Generic;

var sourceRoot = GetFullPath(Combine(GetDirectoryName(GetExecutingAssembly().Location)!, @"..\..\..\.."));
const int extendedSizeLimit = 10;

for (var i = 1; i < 10; i++) {
for (var i = 1; i < extendedSizeLimit; i++) {
var output = GetContent(true, i);
var outpath = Combine(sourceRoot, $"OneOf\\OneOfT{i - 1}.generated.cs");
File.WriteAllText(outpath, output);
Expand All @@ -18,7 +19,7 @@
File.WriteAllText(outpath2, output2);
}

for (var i = 10; i < 33; i++) {
for (var i = extendedSizeLimit; i < 33; i++) {
var output3 = GetContent(true, i);
var outpath3 = Combine(sourceRoot, $"OneOf.Extended\\OneOfT{i - 1}.generated.cs");
File.WriteAllText(outpath3, output3);
Expand Down Expand Up @@ -49,13 +50,13 @@ namespace OneOf
readonly int _index;

{IfStruct( // constructor
$@"OneOf(int index, {RangeJoined(", ", j => $"T{j} value{j} = default")})
$@"internal OneOf(int index, {RangeJoined(", ", j => $"T{j} value{j} = default")})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why add internal to these, it will break anyone's code who has inherited from these with these constructors

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's necessary. If you look in WithType, it uses the constructor of OneOfTN+1. This is also why WithType doesn't work with large size OneOfs - it can't access the constructors of the next size beyond extendedSizeLimit - 1 because those classes are located in a different assembly.

Copy link

@jacob7395 jacob7395 Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, ye I see that for this constructor now, but the $@"protected internal OneOfBase(OneOf<{genericArg}> input)1 can stay as just protected?

That way it wont effect anyone who is inheriting.

{{
_index = index;
{RangeJoined(@"
", j => $"_value{j} = value{j};")}
}}",
$@"protected OneOfBase(OneOf<{genericArg}> input)
$@"protected internal OneOfBase(OneOf<{genericArg}> input)
{{
_index = input.Index;
switch (_index)
Expand All @@ -77,6 +78,12 @@ namespace OneOf

public int Index => _index;

{((i < extendedSizeLimit - 1) ?
// can go up to the limit before extended because OneOfT8 cannot see OneOfT9
$@"public OneOf<{genericArg}, TNew> WithType<TNew>() =>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like a good method but, I am not sure if this interface meets the use cases I have in mind. When this is used would it not makes sense to provide a value and set the index to that value.

I think the method should at least be provided.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean - so in your case you're using this as a convenience object to construct a new object with a new WithType and its own value, and we're just going to throw out the old value-content.

That makes sense. I'd have to see how that works with the existing methods of OneOf. Is there some equivalent "use an existing object for a convenience-object for constructing a new one" thing to build off of?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking it over, maybe I should be redoing this as .WithTN+1 so that it becomes something like

OneOf<T1, T2, T3> myOneOf = SomeFunc();
return myOneOf.WithT4<SomeClass>();

Then the case you describe could be done as a .FromTN+1; so OneOfT3 would have a .FromT4 for cases where you want to expand a T3 by one.

Or maybe just call that .FromNewType, which would make sense as a counterpart to .WithType, althought I don't know how much the .FromTN things get used.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm being silly, you can't call a static method from an instance so I think the only time you could ever use .FromTN+1 would be after literally defining var myOneOf = OneOf<T1, T2, T3>.FromT4<T4>(someValue). Why would you ever do that?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wat thinking something like

public OneOf<T0, TNew> WithType<TNew>(TNew value) => value;

This seems to work;

        {((i < extendedSizeLimit - 1) ?
            // can go up to the limit before extended because OneOfT8 cannot see OneOfT9
            $@"public OneOf<{genericArg}, TNew> WithType<TNew>(TNew value) => value;
        ":"")}

Could still keep the WithType that doesn't take a value.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I was being way too verbose in my old replies, thinking out loud. My point: in this case you're essentially constructing a new OneOf and throwing out the old value. I mean, I see how it works but I don't get what it's for.

new OneOf<{genericArg}, TNew>(_index, {RangeJoined(", ", j => $"_value{j}")}, default);
":"")}

{RangeJoined(@"
", j=> $"public bool IsT{j} => _index == {j};")}

Expand Down
8 changes: 5 additions & 3 deletions OneOf.Extended/OneOfBaseT10.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> : IOneOf
readonly T10 _value10;
readonly int _index;

protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> input)
protected internal OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> input)
{
_index = input.Index;
switch (_index)
Expand Down Expand Up @@ -57,6 +57,8 @@ protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> input)

public int Index => _index;



public bool IsT0 => _index == 0;
public bool IsT1 => _index == 1;
public bool IsT2 => _index == 2;
Expand Down Expand Up @@ -458,7 +460,7 @@ public bool TryPickT10(out T10 value, out OneOf<T0, T1, T2, T3, T4, T5, T6, T7,
_ => throw new InvalidOperationException()
};
return this.IsT10;
}
}

bool Equals(OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> other) =>
_index == other._index &&
Expand Down Expand Up @@ -531,4 +533,4 @@ public override int GetHashCode()
}
}
}
}
}
8 changes: 5 additions & 3 deletions OneOf.Extended/OneOfBaseT11.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> : IOneO
readonly T11 _value11;
readonly int _index;

protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> input)
protected internal OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> input)
{
_index = input.Index;
switch (_index)
Expand Down Expand Up @@ -60,6 +60,8 @@ protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> inpu

public int Index => _index;



public bool IsT0 => _index == 0;
public bool IsT1 => _index == 1;
public bool IsT2 => _index == 2;
Expand Down Expand Up @@ -508,7 +510,7 @@ public bool TryPickT11(out T11 value, out OneOf<T0, T1, T2, T3, T4, T5, T6, T7,
_ => throw new InvalidOperationException()
};
return this.IsT11;
}
}

bool Equals(OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> other) =>
_index == other._index &&
Expand Down Expand Up @@ -584,4 +586,4 @@ public override int GetHashCode()
}
}
}
}
}
8 changes: 5 additions & 3 deletions OneOf.Extended/OneOfBaseT12.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> :
readonly T12 _value12;
readonly int _index;

protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> input)
protected internal OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> input)
{
_index = input.Index;
switch (_index)
Expand Down Expand Up @@ -63,6 +63,8 @@ protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>

public int Index => _index;



public bool IsT0 => _index == 0;
public bool IsT1 => _index == 1;
public bool IsT2 => _index == 2;
Expand Down Expand Up @@ -560,7 +562,7 @@ public bool TryPickT12(out T12 value, out OneOf<T0, T1, T2, T3, T4, T5, T6, T7,
_ => throw new InvalidOperationException()
};
return this.IsT12;
}
}

bool Equals(OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> other) =>
_index == other._index &&
Expand Down Expand Up @@ -639,4 +641,4 @@ public override int GetHashCode()
}
}
}
}
}
8 changes: 5 additions & 3 deletions OneOf.Extended/OneOfBaseT13.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T1
readonly T13 _value13;
readonly int _index;

protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> input)
protected internal OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> input)
{
_index = input.Index;
switch (_index)
Expand Down Expand Up @@ -66,6 +66,8 @@ protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,

public int Index => _index;



public bool IsT0 => _index == 0;
public bool IsT1 => _index == 1;
public bool IsT2 => _index == 2;
Expand Down Expand Up @@ -614,7 +616,7 @@ public bool TryPickT13(out T13 value, out OneOf<T0, T1, T2, T3, T4, T5, T6, T7,
_ => throw new InvalidOperationException()
};
return this.IsT13;
}
}

bool Equals(OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> other) =>
_index == other._index &&
Expand Down Expand Up @@ -696,4 +698,4 @@ public override int GetHashCode()
}
}
}
}
}
8 changes: 5 additions & 3 deletions OneOf.Extended/OneOfBaseT14.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T1
readonly T14 _value14;
readonly int _index;

protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> input)
protected internal OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> input)
{
_index = input.Index;
switch (_index)
Expand Down Expand Up @@ -69,6 +69,8 @@ protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,

public int Index => _index;



public bool IsT0 => _index == 0;
public bool IsT1 => _index == 1;
public bool IsT2 => _index == 2;
Expand Down Expand Up @@ -670,7 +672,7 @@ public bool TryPickT14(out T14 value, out OneOf<T0, T1, T2, T3, T4, T5, T6, T7,
_ => throw new InvalidOperationException()
};
return this.IsT14;
}
}

bool Equals(OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> other) =>
_index == other._index &&
Expand Down Expand Up @@ -755,4 +757,4 @@ public override int GetHashCode()
}
}
}
}
}
8 changes: 5 additions & 3 deletions OneOf.Extended/OneOfBaseT15.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T1
readonly T15 _value15;
readonly int _index;

protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> input)
protected internal OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> input)
{
_index = input.Index;
switch (_index)
Expand Down Expand Up @@ -72,6 +72,8 @@ protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,

public int Index => _index;



public bool IsT0 => _index == 0;
public bool IsT1 => _index == 1;
public bool IsT2 => _index == 2;
Expand Down Expand Up @@ -728,7 +730,7 @@ public bool TryPickT15(out T15 value, out OneOf<T0, T1, T2, T3, T4, T5, T6, T7,
_ => throw new InvalidOperationException()
};
return this.IsT15;
}
}

bool Equals(OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> other) =>
_index == other._index &&
Expand Down Expand Up @@ -816,4 +818,4 @@ public override int GetHashCode()
}
}
}
}
}
8 changes: 5 additions & 3 deletions OneOf.Extended/OneOfBaseT16.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T1
readonly T16 _value16;
readonly int _index;

protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> input)
protected internal OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> input)
{
_index = input.Index;
switch (_index)
Expand Down Expand Up @@ -75,6 +75,8 @@ protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,

public int Index => _index;



public bool IsT0 => _index == 0;
public bool IsT1 => _index == 1;
public bool IsT2 => _index == 2;
Expand Down Expand Up @@ -788,7 +790,7 @@ public bool TryPickT16(out T16 value, out OneOf<T0, T1, T2, T3, T4, T5, T6, T7,
_ => throw new InvalidOperationException()
};
return this.IsT16;
}
}

bool Equals(OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> other) =>
_index == other._index &&
Expand Down Expand Up @@ -879,4 +881,4 @@ public override int GetHashCode()
}
}
}
}
}
8 changes: 5 additions & 3 deletions OneOf.Extended/OneOfBaseT17.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T1
readonly T17 _value17;
readonly int _index;

protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17> input)
protected internal OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17> input)
{
_index = input.Index;
switch (_index)
Expand Down Expand Up @@ -78,6 +78,8 @@ protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,

public int Index => _index;



public bool IsT0 => _index == 0;
public bool IsT1 => _index == 1;
public bool IsT2 => _index == 2;
Expand Down Expand Up @@ -850,7 +852,7 @@ public bool TryPickT17(out T17 value, out OneOf<T0, T1, T2, T3, T4, T5, T6, T7,
_ => throw new InvalidOperationException()
};
return this.IsT17;
}
}

bool Equals(OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17> other) =>
_index == other._index &&
Expand Down Expand Up @@ -944,4 +946,4 @@ public override int GetHashCode()
}
}
}
}
}
8 changes: 5 additions & 3 deletions OneOf.Extended/OneOfBaseT18.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T1
readonly T18 _value18;
readonly int _index;

protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18> input)
protected internal OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18> input)
{
_index = input.Index;
switch (_index)
Expand Down Expand Up @@ -81,6 +81,8 @@ protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,

public int Index => _index;



public bool IsT0 => _index == 0;
public bool IsT1 => _index == 1;
public bool IsT2 => _index == 2;
Expand Down Expand Up @@ -914,7 +916,7 @@ public bool TryPickT18(out T18 value, out OneOf<T0, T1, T2, T3, T4, T5, T6, T7,
_ => throw new InvalidOperationException()
};
return this.IsT18;
}
}

bool Equals(OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18> other) =>
_index == other._index &&
Expand Down Expand Up @@ -1011,4 +1013,4 @@ public override int GetHashCode()
}
}
}
}
}
8 changes: 5 additions & 3 deletions OneOf.Extended/OneOfBaseT19.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T1
readonly T19 _value19;
readonly int _index;

protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19> input)
protected internal OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19> input)
{
_index = input.Index;
switch (_index)
Expand Down Expand Up @@ -84,6 +84,8 @@ protected OneOfBase(OneOf<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,

public int Index => _index;



public bool IsT0 => _index == 0;
public bool IsT1 => _index == 1;
public bool IsT2 => _index == 2;
Expand Down Expand Up @@ -980,7 +982,7 @@ public bool TryPickT19(out T19 value, out OneOf<T0, T1, T2, T3, T4, T5, T6, T7,
_ => throw new InvalidOperationException()
};
return this.IsT19;
}
}

bool Equals(OneOfBase<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19> other) =>
_index == other._index &&
Expand Down Expand Up @@ -1080,4 +1082,4 @@ public override int GetHashCode()
}
}
}
}
}
Loading