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

Better return types for lists that aren't really mutable #164

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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static ResultDto ToResultDto(this Result result)
return new ResultDto(false, TransformErrors(result.Errors));
}

private static IEnumerable<ErrorDto> TransformErrors(List<IError> errors)
private static IEnumerable<ErrorDto> TransformErrors(IEnumerable<IError> errors)
{
return errors.Select(TransformError);
}
Expand Down
6 changes: 3 additions & 3 deletions src/FluentResults/Factories/ResultHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static Result<IEnumerable<TValue>> MergeWithValue<TValue>(
}

public static bool HasError<TError>(
List<IError> errors,
IEnumerable<IError> errors,
Func<TError, bool> predicate,
out IEnumerable<TError> result)
where TError : IError
Expand All @@ -53,7 +53,7 @@ public static bool HasError<TError>(
}

public static bool HasException<TException>(
List<IError> errors,
IEnumerable<IError> errors,
Func<TException, bool> predicate,
out IEnumerable<IError> result)
where TException : Exception
Expand Down Expand Up @@ -83,7 +83,7 @@ public static bool HasException<TException>(
}

public static bool HasSuccess<TSuccess>(
List<ISuccess> successes,
IEnumerable<ISuccess> successes,
Func<TSuccess, bool> predicate,
out IEnumerable<TSuccess> result) where TSuccess : ISuccess
{
Expand Down
2 changes: 1 addition & 1 deletion src/FluentResults/Results/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ public void Deconstruct(out bool isSuccess, out bool isFailed, out TValue value,
isSuccess = IsSuccess;
isFailed = IsFailed;
value = IsSuccess ? Value : default;
errors = IsFailed ? Errors : default;
errors = IsFailed ? Errors.ToList() : default;
}

private void ThrowIfFailed()
Expand Down
10 changes: 5 additions & 5 deletions src/FluentResults/Results/ResultBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public interface IResultBase
/// <summary>
/// Get all errors
/// </summary>
List<IError> Errors { get; }
IReadOnlyList<IError> Errors { get; }

/// <summary>
/// Get all successes
/// </summary>
List<ISuccess> Successes { get; }
IReadOnlyList<ISuccess> Successes { get; }
}

public abstract class ResultBase : IResultBase
Expand All @@ -54,12 +54,12 @@ public abstract class ResultBase : IResultBase
/// <summary>
/// <inheritdoc/>
/// </summary>
public List<IError> Errors => Reasons.OfType<IError>().ToList();
public IReadOnlyList<IError> Errors => Reasons.OfType<IError>().ToList().AsReadOnly();

/// <summary>
/// <inheritdoc/>
/// </summary>
public List<ISuccess> Successes => Reasons.OfType<ISuccess>().ToList();
public IReadOnlyList<ISuccess> Successes => Reasons.OfType<ISuccess>().ToList().AsReadOnly();

protected ResultBase()
{
Expand Down Expand Up @@ -226,7 +226,7 @@ public void Deconstruct(out bool isSuccess, out bool isFailed, out List<IError>
{
isSuccess = IsSuccess;
isFailed = IsFailed;
errors = IsFailed ? Errors : default;
errors = IsFailed ? Errors.ToList() : default;
}
}

Expand Down