Skip to content

Commit 6ab967e

Browse files
committed
Option<T> refactoring
1 parent e0f5493 commit 6ab967e

File tree

4 files changed

+92
-84
lines changed

4 files changed

+92
-84
lines changed

Main/src/CodeJam.Main.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
<Compile Include="Collections\EnumerableExtensions.Slice.cs" />
107107
<Compile Include="Collections\QueryableExtensions.ApplyOrder.cs" />
108108
<Compile Include="Collections\QueryableExtensions.cs" />
109+
<Compile Include="Option.cs" />
109110
<Compile Include="Reflection\InfoOf`1.cs" />
110111
<Compile Include="Reflection\ParamInfo.cs" />
111112
<Compile Include="Services\IServicePublisher.cs" />
@@ -164,8 +165,7 @@
164165
<Compile Include="Structures\ObjectPools\SharedPoolExtensions.cs" />
165166
<Compile Include="Structures\ObjectPools\PooledObject.cs" />
166167
<Compile Include="Structures\ObjectPools\ObjectPool.cs" />
167-
<Compile Include="Option.cs" />
168-
<Compile Include="OptionExtensions.cs" />
168+
<Compile Include="Option`1.cs" />
169169
<Compile Include="Properties\AssemblyInfo.cs" />
170170
<Compile Include="Reflection\EnumHelper.cs" />
171171
<Compile Include="Reflection\ExpressionHelper.cs" />

Main/src/Option.cs

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,76 @@
1-
using System;
1+
using System;
22

33
using JetBrains.Annotations;
44

55
namespace CodeJam
66
{
77
/// <summary>
8-
/// Represents a value type that can be assigned null.
8+
/// Methods to work with <see cref="Option{T}"/>
99
/// </summary>
10-
/// <typeparam name="T"></typeparam>
1110
[PublicAPI]
12-
public struct Option<T>
11+
public static class Option
1312
{
1413
/// <summary>
15-
/// Initializes a new instance to the specified value.
14+
/// Create instance of <see cref="Option{T}"/>
1615
/// </summary>
17-
public Option(T value)
16+
/// <typeparam name="T">Type of value</typeparam>
17+
/// <param name="value">Value</param>
18+
/// <returns>New instance of <see cref="Option{T}"/>.</returns>
19+
public static Option<T> Create<T>(T value) => new Option<T>(value);
20+
21+
/// <summary>
22+
/// Calls <paramref name="someAction"/> if <paramref name="option"/> has value,
23+
/// and <paramref name="noneAction"/> otherwise.
24+
/// </summary>
25+
public static void Match<T>(
26+
this Option<T> option,
27+
[NotNull, InstantHandle]Action<Option<T>> someAction,
28+
[NotNull, InstantHandle]Action noneAction)
1829
{
19-
HasValue = true;
20-
Value = value;
30+
Code.NotNull(someAction, nameof(someAction));
31+
Code.NotNull(noneAction, nameof(noneAction));
32+
33+
if (option.HasValue)
34+
someAction(option);
35+
else
36+
noneAction();
2137
}
2238

2339
/// <summary>
24-
/// Gets a value indicating whether the current object has a value.
40+
/// Calls <paramref name="someFunc"/> if <paramref name="option"/> has value,
41+
/// and <paramref name="noneFunc"/> otherwise.
2542
/// </summary>
26-
public bool HasValue { get; }
43+
[Pure]
44+
public static TResult Match<T, TResult>(
45+
this Option<T> option,
46+
[NotNull, InstantHandle] Func<Option<T>, TResult> someFunc,
47+
[NotNull, InstantHandle] Func<TResult> noneFunc)
48+
{
49+
Code.NotNull(someFunc, nameof(someFunc));
50+
Code.NotNull(noneFunc, nameof(noneFunc));
51+
52+
return option.HasValue ? someFunc(option) : noneFunc();
53+
}
2754

2855
/// <summary>
29-
/// Gets the value of the current object.
56+
/// Returns value of <paramref name="option"/>, or <paramref name="defaultValue"/> if <paramref name="option"/>
57+
/// hasn't it.
3058
/// </summary>
31-
public T Value { get; }
59+
[Pure]
60+
public static T GetValueOrDefault<T>(this Option<T> option, T defaultValue = default(T)) =>
61+
option.HasValue ? option.Value : defaultValue;
3262

3363
/// <summary>
34-
/// Creates a new object initialized to a specified value.
64+
/// Converts <paramref name="option"/> value to another option with <paramref name="selectFunc"/>.
3565
/// </summary>
36-
public static implicit operator Option<T>(T value) => new Option<T>(value);
66+
[Pure]
67+
public static Option<TResult> Map<T, TResult>(
68+
this Option<T> option,
69+
[InstantHandle] Func<T, TResult> selectFunc)
70+
{
71+
Code.NotNull(selectFunc, nameof(selectFunc));
72+
73+
return option.HasValue ? new Option<TResult>(selectFunc(option.Value)) : new Option<TResult>();
74+
}
3775
}
3876
}

Main/src/OptionExtensions.cs

Lines changed: 0 additions & 68 deletions
This file was deleted.

Main/src/Option`1.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
3+
using JetBrains.Annotations;
4+
5+
namespace CodeJam
6+
{
7+
/// <summary>
8+
/// Represents a value type that can be assigned null.
9+
/// </summary>
10+
/// <typeparam name="T"></typeparam>
11+
[PublicAPI]
12+
public struct Option<T>
13+
{
14+
/// <summary>
15+
/// Initializes a new instance to the specified value.
16+
/// </summary>
17+
public Option(T value)
18+
{
19+
HasValue = true;
20+
Value = value;
21+
}
22+
23+
/// <summary>
24+
/// Gets a value indicating whether the current object has a value.
25+
/// </summary>
26+
public bool HasValue { get; }
27+
28+
/// <summary>
29+
/// Gets the value of the current object.
30+
/// </summary>
31+
public T Value { get; }
32+
33+
/// <summary>
34+
/// Creates a new object initialized to a specified value.
35+
/// </summary>
36+
public static implicit operator Option<T>(T value) => new Option<T>(value);
37+
}
38+
}

0 commit comments

Comments
 (0)