Skip to content
Merged
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
12 changes: 11 additions & 1 deletion src/Wolfgang.TryPattern/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,20 @@ public static Result Failure(string errorMessage) =>



private static readonly Result _successInstance = new(succeeded: true, string.Empty);



/// <summary>
/// Creates a successful <see cref="Result"/>.
/// </summary>
public static Result Success() => new(succeeded: true, string.Empty);
/// <remarks>
/// Returns a cached singleton instance. <see cref="Result"/> is immutable, so reusing
/// the same instance is safe and avoids per-call allocations on hot paths. Callers must
/// not rely on reference identity to distinguish results: every call to <see cref="Success"/>
/// returns the same object, so two successful results will be reference-equal.
/// </remarks>
public static Result Success() => _successInstance;
Comment thread
Chris-Wolfgang marked this conversation as resolved.



Expand Down
16 changes: 16 additions & 0 deletions tests/Wolfgang.TryPattern.Tests/ResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ public void Success_sets_properties_correctly()



[Fact]
public void Success_returns_the_same_cached_instance_on_every_call()
{
// Locks in the singleton optimization: Result is immutable, so
// Success() intentionally returns a shared instance to avoid
// per-call allocations. If this assertion ever needs to be
// relaxed, it is a deliberate behavioral change that consumers
// relying on reference identity should be notified about.
var first = Result.Success();
var second = Result.Success();

Assert.Same(first, second);
}



[Fact]
public void Failure_sets_properties_correctly()
{
Expand Down
Loading