/
Result.RationalResult.cs
74 lines (64 loc) · 2.53 KB
/
Result.RationalResult.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) Dmytro Kyshchenko. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Diagnostics.CodeAnalysis;
namespace xFunc.Maths.Results;
public abstract partial class Result
{
/// <summary>
/// Converts <see cref="RationalValue"/> to <see cref="RationalResult"/>.
/// </summary>
/// <param name="rational">The rational value.</param>
/// <returns>The rational result.</returns>
[ExcludeFromCodeCoverage]
public static implicit operator Result(RationalValue rational)
=> new RationalResult(rational);
/// <summary>
/// Gets the rational value.
/// </summary>
/// <param name="rationalValue">The rational value.</param>
/// <returns><c>true</c> if the current instance contains <see cref="RationalValue"/>, otherwise <c>false</c>.</returns>
public bool TryGetRational([NotNullWhen(true)] out RationalValue? rationalValue)
{
if (this is RationalResult rationalResult)
{
rationalValue = rationalResult.Rational;
return true;
}
rationalValue = null;
return false;
}
/// <summary>
/// Gets the rational value.
/// </summary>
/// <exception cref="InvalidCastException">Thrown when the current instance is not <see cref="RationalResult"/>.</exception>
[ExcludeFromCodeCoverage]
public virtual RationalValue Rational
=> ((RationalResult)this).Rational;
/// <summary>
/// Represents the rational result.
/// </summary>
public sealed class RationalResult : Result
{
/// <summary>
/// Initializes a new instance of the <see cref="Result.RationalResult"/> class.
/// </summary>
/// <param name="rational">The rational value.</param>
public RationalResult(RationalValue rational)
=> Rational = rational;
/// <summary>
/// Converts <see cref="Result.RationalResult"/> to <see cref="RationalValue"/>.
/// </summary>
/// <param name="rationalResult">The rational result.</param>
/// <returns>The rational value.</returns>
[ExcludeFromCodeCoverage]
public static implicit operator RationalValue(RationalResult rationalResult)
=> rationalResult.Rational;
/// <inheritdoc />
public override string ToString()
=> Rational.ToString();
/// <summary>
/// Gets the rational value.
/// </summary>
public override RationalValue Rational { get; }
}
}