Skip to content
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 @@ -40,6 +40,17 @@ extension PredicateExpressions {
}
}

@available(FoundationPreview 6.4, *)
extension PredicateExpressions {
public static func build_Equal<LHS, Wrapped>(lhs: LHS, rhs: NilLiteral<Wrapped>) -> Equal<OptionalFlatMap<LHS, Wrapped, Value<Bool>, Bool>, Value<Bool?>> {
Equal(lhs: OptionalFlatMap(lhs) { _ in Value(true) }, rhs: Value(nil))
}

public static func build_Equal<Wrapped, RHS>(lhs: NilLiteral<Wrapped>, rhs: RHS) -> Equal<Value<Bool?>, OptionalFlatMap<RHS, Wrapped, Value<Bool>, Bool>> {
Equal(lhs: Value(nil), rhs: OptionalFlatMap(rhs) { _ in Value(true) })
}
}

@available(macOS 14.4, iOS 17.4, tvOS 17.4, watchOS 10.4, *)
extension PredicateExpressions.Equal : CustomStringConvertible {
public var description: String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ extension PredicateExpressions {
}
}

@available(FoundationPreview 6.4, *)
extension PredicateExpressions {
public static func build_NotEqual<LHS, Wrapped>(lhs: LHS, rhs: NilLiteral<Wrapped>) -> NotEqual<OptionalFlatMap<LHS, Wrapped, Value<Bool>, Bool>, Value<Bool?>> {
NotEqual(lhs: OptionalFlatMap(lhs) { _ in Value(true) }, rhs: Value(nil))
}

public static func build_NotEqual<Wrapped, RHS>(lhs: NilLiteral<Wrapped>, rhs: RHS) -> NotEqual<Value<Bool?>, OptionalFlatMap<RHS, Wrapped, Value<Bool>, Bool>> {
NotEqual(lhs: Value(nil), rhs: OptionalFlatMap(rhs) { _ in Value(true) })
}
}

@available(macOS 14.4, iOS 17.4, tvOS 17.4, watchOS 10.4, *)
extension PredicateExpressions.NotEqual : CustomStringConvertible {
public var description: String {
Expand Down
12 changes: 12 additions & 0 deletions Tests/FoundationEssentialsTests/PredicateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,16 @@ private struct PredicateTests {
#expect(try expression.evaluate(i) == i + 1)
}
}

@Test(arguments: [#Predicate<Object?> { $0 == nil }, #Predicate<Object?> { nil == $0 }])
func nilLiteral(equality: Predicate<Object?>) throws {
#expect(try equality.evaluate(nil))
#expect(try !equality.evaluate(Object(a: 9, b: "abc", c: 0.72, d: 64, e: "c", f: true, g: [5, 7, 5, 1])))
}

@Test(arguments: [#Predicate<Object?> { $0 != nil }, #Predicate<Object?> { nil != $0 }])
func nilLiteral(inequality: Predicate<Object?>) throws {
#expect(try !inequality.evaluate(nil))
#expect(try inequality.evaluate(Object(a: 9, b: "abc", c: 0.72, d: 64, e: "c", f: true, g: [5, 7, 5, 1])))
}
}