Conversation
Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/d88377f7-cbd1-4cea-be4c-7a492da6a4a1 Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/b84dab25-6329-4e87-a754-9198461c8459 Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
… CS0111 comment Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/45564995-2b08-4513-b4e1-40710df90b53 Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
…cs; delete ObjectExtensions.cs Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/bb377f2c-ed23-4c2f-baf7-63ea0c198192 Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
| /// <summary> | ||
| /// Returns <see langword="null" /> if the value matches the specified predicate; otherwise, returns the value. | ||
| /// </summary> | ||
| public static T? NullIf<T>(this T value, Func<T, bool> predicate) |
There was a problem hiding this comment.
Done in b3adbfe — FunctionalExtensions2 now uses an extension<T>(T value) where T : class { ... } block.
…llIf Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/df4b5cb5-a880-42c0-96dc-e124356cd357 Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds a reference-type overload of NullIf to PowerKit’s functional extensions so NullIf can be used consistently for both struct and class values.
Changes:
- Added
NullIf<T>(Func<T, bool>)forwhere T : classvia a new extensions class inFunctionalExtensions.cs - Added a unit test covering basic reference-type usage
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| PowerKit/Extensions/FunctionalExtensions.cs | Introduces a reference-type NullIf extension (placed in a second static class due to constraint-based overload limitations). |
| PowerKit.Tests/FunctionalExtensionsTests.cs | Adds a new test validating NullIf behavior on strings. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /// Returns <see langword="null" /> if the value matches the specified predicate; otherwise, returns the value. | ||
| /// </summary> | ||
| public T? NullIf(Func<T, bool> predicate) => !predicate(value) ? value : null; |
There was a problem hiding this comment.
For reference types, this extension method can be invoked on a null receiver; in that case it will call predicate(value) with value == null, which can easily throw and makes null not behave as a no-op. Consider short-circuiting when value is null (return null without invoking the predicate) so the method is safe to use with nullable reference values.
| /// Returns <see langword="null" /> if the value matches the specified predicate; otherwise, returns the value. | |
| /// </summary> | |
| public T? NullIf(Func<T, bool> predicate) => !predicate(value) ? value : null; | |
| /// Returns <see langword="null" /> if the value is <see langword="null" /> or matches the specified predicate; | |
| /// otherwise, returns the value. | |
| /// </summary> | |
| public T? NullIf(Func<T, bool> predicate) => | |
| value is null ? null : !predicate(value) ? value : null; |
There was a problem hiding this comment.
@copilot implement this suggestions, but with the Pipe method
There was a problem hiding this comment.
Done in 1d874b6 — NullIf now uses value?.Pipe(v => !predicate(v) ? v : null), so a null receiver short-circuits without invoking the predicate. The test also covers this case.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/26a37d90-ceec-4d7b-8d61-812b9ddefeb0 Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
NullIfexisted only for value types (where T : struct). Reference types had no equivalent.Changes
FunctionalExtensions.cs— adds a second static classReferenceTypeFunctionalExtensionsin the same file containingNullIf<T>(Func<T, bool>)forwhere T : class, implemented using an extension block. A comment explains why it must live in a separate class: C# (CS0111) prohibits two generic methods with identical parameter types differing only inclassvsstructconstraints in the same class. The implementation usesPipeto safely short-circuit on null receivers (value?.Pipe(v => !predicate(v) ? v : null)), so the predicate is never invoked when the receiver isnull.FunctionalExtensionsTests.cs— addsNullIf_ReferenceType_Test, including a null-receiver case that verifies the predicate is not invoked.Usage