Skip to content

Commit 026b058

Browse files
authored
Merge pull request #2 from mrxrsd/feature/implicit-conversions
feat: implicit conversions
2 parents 2d4af56 + a806bdc commit 026b058

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,27 @@ Bespoke errors provide several benefits:
344344

345345
## Implicit Conversions
346346

347-
ARPL supports implicit conversions between `Either<Error,R>` and `SResult<R>`, making it seamless to work with both types:
347+
ARPL supports several implicit conversions to make your code more concise and readable.
348+
349+
### Either from Base Types
350+
351+
You can create an `Either<L, R>` instance directly from values of type `L` or `R`:
352+
353+
```csharp
354+
// Implicitly create a Right-sided Either
355+
Either<string, int> success = 42;
356+
357+
// Implicitly create a Left-sided Either
358+
Either<string, int> error = "Something went wrong";
359+
360+
// This is equivalent to:
361+
var successExplicit = Either<string, int>.Right(42);
362+
var errorExplicit = Either<string, int>.Left("Something went wrong");
363+
```
364+
365+
### Between `Either<Error,R>` and `SResult<R>`
366+
367+
ARPL also supports seamless conversion between `Either<Error,R>` and `SResult<R>`:
348368

349369
```csharp
350370
// Convert from Either to SResult
@@ -356,7 +376,7 @@ SResult<int> sresult = SResult<int>.Success(42);
356376
Either<Error, int> converted = sresult; // Implicit conversion
357377
```
358378

359-
> **Note:** The implicit conversion only works for `Either<Error, R>` and `SResult<R>`. Attempting to convert other types will throw an exception.
379+
> **Note:** The implicit conversion between `Either` and `SResult` only works for `Either<Error, R>`. Attempting to convert other `Either` types will result in a compile-time error or throw an exception.
360380
361381
## StaticFactory Helpers
362382

arpl.tests/Core/EitherTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,34 @@ public void ImplicitConversion_ToSResult_PreservesLeft()
6666
Assert.True(result.IsFail);
6767
Assert.Equal(error, result.ErrorValue);
6868
}
69+
70+
71+
[Fact(DisplayName = "Implicit Conversion - To Either preserves Left value")]
72+
public void ImplicitConversion_ToEither_PreservesLeft()
73+
{
74+
// Arrange
75+
var error = Errors.New("Test error");
76+
77+
// Act
78+
Either<Error,string> result = error;
79+
80+
// Assert
81+
Assert.True(result.IsLeft);
82+
Assert.Equal(error, result.LeftValue);
83+
}
84+
85+
[Fact(DisplayName = "Implicit Conversion - To Either preserves Right value")]
86+
public void ImplicitConversion_ToEither_PreservesRight()
87+
{
88+
// Arrange
89+
var value = "abc";
90+
91+
// Act
92+
Either<Error, string> result = value;
93+
94+
// Assert
95+
Assert.True(result.IsRight);
96+
Assert.Equal(value, result.RightValue);
97+
}
6998
}
7099
}

arpl/core/Either.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ public EitherRight(R value)
8585
/// <returns>A new Either instance representing a right value.</returns>
8686
public static Either<L, R> Right(R value) => new EitherRight<L, R>(value);
8787

88+
/// <summary>
89+
/// Implicitly converts a Left value to an Either
90+
/// </summary>
91+
/// <param name="left"></param>
92+
public static implicit operator Either<L,R>(L left) => Either<L, R>.Left(left);
93+
94+
/// <summary>
95+
/// Implicitly converts a Left value to an Either
96+
/// </summary>
97+
/// <param name="left"></param>
98+
public static implicit operator Either<L, R>(R right) => Either<L, R>.Right(right);
99+
100+
88101
/// <summary>
89102
/// Implicitly converts an Either type to an SResult when the left type is Error.
90103
/// </summary>

0 commit comments

Comments
 (0)