-
Notifications
You must be signed in to change notification settings - Fork 165
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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); | ||
|
@@ -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")}) | ||
{{ | ||
_index = index; | ||
{RangeJoined(@" | ||
", j => $"_value{j} = value{j};")} | ||
}}", | ||
$@"protected OneOfBase(OneOf<{genericArg}> input) | ||
$@"protected internal OneOfBase(OneOf<{genericArg}> input) | ||
{{ | ||
_index = input.Index; | ||
switch (_index) | ||
|
@@ -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>() => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see what you mean - so in your case you're using That makes sense. I'd have to see how that works with the existing methods of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking it over, maybe I should be redoing this as OneOf<T1, T2, T3> myOneOf = SomeFunc();
return myOneOf.WithT4<SomeClass>(); Then the case you describe could be done as a Or maybe just call that There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wat thinking something like
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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
new OneOf<{genericArg}, TNew>(_index, {RangeJoined(", ", j => $"_value{j}")}, default); | ||
":"")} | ||
|
||
{RangeJoined(@" | ||
", j=> $"public bool IsT{j} => _index == {j};")} | ||
|
||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 ofOneOfTN+1
. This is also whyWithType
doesn't work with large size OneOfs - it can't access the constructors of the next size beyondextendedSizeLimit - 1
because those classes are located in a different assembly.There was a problem hiding this comment.
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.