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

Added overload to switch method for use with async code. #106

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
16 changes: 16 additions & 0 deletions Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ string GetContent(bool isStruct, int i) {
var sb = new StringBuilder();

sb.Append(@$"using System;

#if !NET35
using System.Threading.Tasks;
#endif
using static OneOf.Functions;

namespace OneOf
Expand Down Expand Up @@ -100,6 +104,18 @@ public void Switch({RangeJoined(", ", e => $"Action<T{e}> f{e}")})
throw new InvalidOperationException();
}}

#if !NET35
Copy link

@ChristopherHaws ChristopherHaws Dec 15, 2022

Choose a reason for hiding this comment

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

This should probably be (!NETFRAMEWORK || NET40_OR_GREATER) instead of !NET35

public Task Switch({RangeJoined(", ", e => $"Func<T{e}, Task> f{e}")})
Copy link

Choose a reason for hiding this comment

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

Rename this to SwitchAsync.

{{
{RangeJoined(@"
", j => @$"if (_index == {j} && f{j} != null)
{{
return f{j}(_value{j});
}}")}
throw new InvalidOperationException();
}}
#endif

public TResult Match<TResult>({RangeJoined(", ", e => $"Func<T{e}, TResult> f{e}")})
{{
{RangeJoined(@"
Expand Down
55 changes: 55 additions & 0 deletions OneOf.Extended/OneOfBaseT10.generated.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;

#if !NET35
using System.Threading.Tasks;
#endif
using static OneOf.Functions;

namespace OneOf
Expand Down Expand Up @@ -176,6 +180,57 @@ public void Switch(Action<T0> f0, Action<T1> f1, Action<T2> f2, Action<T3> f3, A
throw new InvalidOperationException();
}

#if !NET35
public Task Switch(Func<T0, Task> f0, Func<T1, Task> f1, Func<T2, Task> f2, Func<T3, Task> f3, Func<T4, Task> f4, Func<T5, Task> f5, Func<T6, Task> f6, Func<T7, Task> f7, Func<T8, Task> f8, Func<T9, Task> f9, Func<T10, Task> f10)
{
if (_index == 0 && f0 != null)
{
return f0(_value0);
}
if (_index == 1 && f1 != null)
{
return f1(_value1);
}
if (_index == 2 && f2 != null)
{
return f2(_value2);
}
if (_index == 3 && f3 != null)
{
return f3(_value3);
}
if (_index == 4 && f4 != null)
{
return f4(_value4);
}
if (_index == 5 && f5 != null)
{
return f5(_value5);
}
if (_index == 6 && f6 != null)
{
return f6(_value6);
}
if (_index == 7 && f7 != null)
{
return f7(_value7);
}
if (_index == 8 && f8 != null)
{
return f8(_value8);
}
if (_index == 9 && f9 != null)
{
return f9(_value9);
}
if (_index == 10 && f10 != null)
{
return f10(_value10);
}
throw new InvalidOperationException();
}
#endif

public TResult Match<TResult>(Func<T0, TResult> f0, Func<T1, TResult> f1, Func<T2, TResult> f2, Func<T3, TResult> f3, Func<T4, TResult> f4, Func<T5, TResult> f5, Func<T6, TResult> f6, Func<T7, TResult> f7, Func<T8, TResult> f8, Func<T9, TResult> f9, Func<T10, TResult> f10)
{
if (_index == 0 && f0 != null)
Expand Down
59 changes: 59 additions & 0 deletions OneOf.Extended/OneOfBaseT11.generated.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;

#if !NET35
using System.Threading.Tasks;
#endif
using static OneOf.Functions;

namespace OneOf
Expand Down Expand Up @@ -189,6 +193,61 @@ public void Switch(Action<T0> f0, Action<T1> f1, Action<T2> f2, Action<T3> f3, A
throw new InvalidOperationException();
}

#if !NET35
public Task Switch(Func<T0, Task> f0, Func<T1, Task> f1, Func<T2, Task> f2, Func<T3, Task> f3, Func<T4, Task> f4, Func<T5, Task> f5, Func<T6, Task> f6, Func<T7, Task> f7, Func<T8, Task> f8, Func<T9, Task> f9, Func<T10, Task> f10, Func<T11, Task> f11)
{
if (_index == 0 && f0 != null)
{
return f0(_value0);
}
if (_index == 1 && f1 != null)
{
return f1(_value1);
}
if (_index == 2 && f2 != null)
{
return f2(_value2);
}
if (_index == 3 && f3 != null)
{
return f3(_value3);
}
if (_index == 4 && f4 != null)
{
return f4(_value4);
}
if (_index == 5 && f5 != null)
{
return f5(_value5);
}
if (_index == 6 && f6 != null)
{
return f6(_value6);
}
if (_index == 7 && f7 != null)
{
return f7(_value7);
}
if (_index == 8 && f8 != null)
{
return f8(_value8);
}
if (_index == 9 && f9 != null)
{
return f9(_value9);
}
if (_index == 10 && f10 != null)
{
return f10(_value10);
}
if (_index == 11 && f11 != null)
{
return f11(_value11);
}
throw new InvalidOperationException();
}
#endif

public TResult Match<TResult>(Func<T0, TResult> f0, Func<T1, TResult> f1, Func<T2, TResult> f2, Func<T3, TResult> f3, Func<T4, TResult> f4, Func<T5, TResult> f5, Func<T6, TResult> f6, Func<T7, TResult> f7, Func<T8, TResult> f8, Func<T9, TResult> f9, Func<T10, TResult> f10, Func<T11, TResult> f11)
{
if (_index == 0 && f0 != null)
Expand Down
63 changes: 63 additions & 0 deletions OneOf.Extended/OneOfBaseT12.generated.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;

#if !NET35
using System.Threading.Tasks;
#endif
using static OneOf.Functions;

namespace OneOf
Expand Down Expand Up @@ -202,6 +206,65 @@ public void Switch(Action<T0> f0, Action<T1> f1, Action<T2> f2, Action<T3> f3, A
throw new InvalidOperationException();
}

#if !NET35
public Task Switch(Func<T0, Task> f0, Func<T1, Task> f1, Func<T2, Task> f2, Func<T3, Task> f3, Func<T4, Task> f4, Func<T5, Task> f5, Func<T6, Task> f6, Func<T7, Task> f7, Func<T8, Task> f8, Func<T9, Task> f9, Func<T10, Task> f10, Func<T11, Task> f11, Func<T12, Task> f12)
{
if (_index == 0 && f0 != null)
{
return f0(_value0);
}
if (_index == 1 && f1 != null)
{
return f1(_value1);
}
if (_index == 2 && f2 != null)
{
return f2(_value2);
}
if (_index == 3 && f3 != null)
{
return f3(_value3);
}
if (_index == 4 && f4 != null)
{
return f4(_value4);
}
if (_index == 5 && f5 != null)
{
return f5(_value5);
}
if (_index == 6 && f6 != null)
{
return f6(_value6);
}
if (_index == 7 && f7 != null)
{
return f7(_value7);
}
if (_index == 8 && f8 != null)
{
return f8(_value8);
}
if (_index == 9 && f9 != null)
{
return f9(_value9);
}
if (_index == 10 && f10 != null)
{
return f10(_value10);
}
if (_index == 11 && f11 != null)
{
return f11(_value11);
}
if (_index == 12 && f12 != null)
{
return f12(_value12);
}
throw new InvalidOperationException();
}
#endif

public TResult Match<TResult>(Func<T0, TResult> f0, Func<T1, TResult> f1, Func<T2, TResult> f2, Func<T3, TResult> f3, Func<T4, TResult> f4, Func<T5, TResult> f5, Func<T6, TResult> f6, Func<T7, TResult> f7, Func<T8, TResult> f8, Func<T9, TResult> f9, Func<T10, TResult> f10, Func<T11, TResult> f11, Func<T12, TResult> f12)
{
if (_index == 0 && f0 != null)
Expand Down
67 changes: 67 additions & 0 deletions OneOf.Extended/OneOfBaseT13.generated.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;

#if !NET35
using System.Threading.Tasks;
#endif
using static OneOf.Functions;

namespace OneOf
Expand Down Expand Up @@ -215,6 +219,69 @@ public void Switch(Action<T0> f0, Action<T1> f1, Action<T2> f2, Action<T3> f3, A
throw new InvalidOperationException();
}

#if !NET35
public Task Switch(Func<T0, Task> f0, Func<T1, Task> f1, Func<T2, Task> f2, Func<T3, Task> f3, Func<T4, Task> f4, Func<T5, Task> f5, Func<T6, Task> f6, Func<T7, Task> f7, Func<T8, Task> f8, Func<T9, Task> f9, Func<T10, Task> f10, Func<T11, Task> f11, Func<T12, Task> f12, Func<T13, Task> f13)
{
if (_index == 0 && f0 != null)
{
return f0(_value0);
}
if (_index == 1 && f1 != null)
{
return f1(_value1);
}
if (_index == 2 && f2 != null)
{
return f2(_value2);
}
if (_index == 3 && f3 != null)
{
return f3(_value3);
}
if (_index == 4 && f4 != null)
{
return f4(_value4);
}
if (_index == 5 && f5 != null)
{
return f5(_value5);
}
if (_index == 6 && f6 != null)
{
return f6(_value6);
}
if (_index == 7 && f7 != null)
{
return f7(_value7);
}
if (_index == 8 && f8 != null)
{
return f8(_value8);
}
if (_index == 9 && f9 != null)
{
return f9(_value9);
}
if (_index == 10 && f10 != null)
{
return f10(_value10);
}
if (_index == 11 && f11 != null)
{
return f11(_value11);
}
if (_index == 12 && f12 != null)
{
return f12(_value12);
}
if (_index == 13 && f13 != null)
{
return f13(_value13);
}
throw new InvalidOperationException();
}
#endif

public TResult Match<TResult>(Func<T0, TResult> f0, Func<T1, TResult> f1, Func<T2, TResult> f2, Func<T3, TResult> f3, Func<T4, TResult> f4, Func<T5, TResult> f5, Func<T6, TResult> f6, Func<T7, TResult> f7, Func<T8, TResult> f8, Func<T9, TResult> f9, Func<T10, TResult> f10, Func<T11, TResult> f11, Func<T12, TResult> f12, Func<T13, TResult> f13)
{
if (_index == 0 && f0 != null)
Expand Down
Loading