diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index c94de1c8c..8d39236bd 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -674,18 +674,20 @@ extension Row { dataNoCopy(named: column.name) } - /// Returns the first non-null value, if any. Identical to SQL COALESCE function. + /// Returns the first non-null value, if any. Identical to SQL `COALESCE` function. /// /// For example: /// /// ```swift - /// let myInt: Int? = row.coalesce(["int_A", "int_B"]) + /// let name: String? = row.coalesce(["nickname", "name"]) /// ``` /// - /// Use of `coalesce` is essential, as nil-coalescing row values does not work: + /// Prefer `coalesce` to nil-coalescing row values, which does not + /// return the expected value: /// /// ```swift - /// let myInt: Int? = row["int_A"] ?? row["int_B"] // Won't work + /// // INCORRECT + /// let name: String? = row["nickname"] ?? row["name"] /// ``` public func coalesce( _ columns: some Collection @@ -698,18 +700,20 @@ extension Row { return nil } - /// Returns the first non-null value, if any. Identical to SQL COALESCE function. + /// Returns the first non-null value, if any. Identical to SQL `COALESCE` function. /// /// For example: /// /// ```swift - /// let myInt: Int? = row.coalesce([Column("int_A"), Column("int_B")]) + /// let name: String? = row.coalesce([Column("nickname"), Column("name")]) /// ``` /// - /// Use of `coalesce` is essential, as nil-coalescing row values does not work: + /// Prefer `coalesce` to nil-coalescing row values, which does not + /// return the expected value: /// /// ```swift - /// let myInt: Int? = row[Column("int_A")] ?? row[Column("int_B")] // Won't work + /// // INCORRECT + /// let name: String? = row[Column("nickname")] ?? row[Column("name")] /// ``` public func coalesce( _ columns: some Collection diff --git a/GRDB/QueryInterface/SQL/SQLFunctions.swift b/GRDB/QueryInterface/SQL/SQLFunctions.swift index 44f203aa5..dde4a4e16 100644 --- a/GRDB/QueryInterface/SQL/SQLFunctions.swift +++ b/GRDB/QueryInterface/SQL/SQLFunctions.swift @@ -76,7 +76,7 @@ public func cast(_ expression: some SQLSpecificExpressible, as storageClass: Dat /// For example: /// /// ```swift -/// // COALESCE([value1, value2, ...]) +/// // COALESCE(value1, value2, ...) /// coalesce([Column("value1"), Column("value2"), ...]) /// ``` public func coalesce(_ values: some Collection) -> SQLExpression { diff --git a/README.md b/README.md index 5f338f8e6..e682103ad 100644 --- a/README.md +++ b/README.md @@ -841,11 +841,11 @@ row[...] as Int? > if let int = row[...] as Int? { ... } // GOOD > ``` -> **Warning**: avoid nil-coalescing row values: +> **Warning**: avoid nil-coalescing row values, and prefer the `coalesce` method instead: > > ```swift -> let myInt: Int? = row["int_A"] ?? row["int_B"] // BAD - doesn't work -> let myInt: Int? = row.coalesce(["int_A", "int_B"]) // GOOD +> let name: String? = row["nickname"] ?? row["name"] // BAD - doesn't work +> let name: String? = row.coalesce(["nickname", "name"]) // GOOD > ``` Generally speaking, you can extract the type you need, provided it can be converted from the underlying SQLite value: