From 6bdf433e9520654e6d6823bb8448f50cea3daf97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 20 May 2016 08:19:39 +0200 Subject: [PATCH 1/4] Issue #53: add a FAQ entry for the `Generic parameter 'T' could not be inferred` error --- README.md | 25 ++++++ .../DatabaseQueue/DatabaseQueueTests.swift | 80 ------------------- 2 files changed, 25 insertions(+), 80 deletions(-) diff --git a/README.md b/README.md index ea97c2989..151235a12 100644 --- a/README.md +++ b/README.md @@ -2935,6 +2935,31 @@ See also [Transactions](#transactions) for more precise handling of transactions FAQ === +- **Generic parameter 'T' could not be inferred** + + You may get this error when using DatabaseQueue.inDatabase, DatabasePool.read, or DatabasePool.write: + + ```swift + // Generic parameter 'T' could not be inferred + let x = dbQueue.inDatabase { db in + let result = String.fetchOne(...) + return result + } + ``` + + This is a Swift compiler bug (see [SR-1570](https://bugs.swift.org/browse/SR-1570)). + + The workaround is to explicitly declare the type of the closure result: + + ```swift + // Workaround + let x = dbQueue.inDatabase { db -> String? in + let result = String.fetchOne(...) + return result + } + ``` + + - **How do I close a database connection?** The short answer is: diff --git a/Tests/Public/Core/DatabaseQueue/DatabaseQueueTests.swift b/Tests/Public/Core/DatabaseQueue/DatabaseQueueTests.swift index e3df2cb61..9c23b2c6d 100644 --- a/Tests/Public/Core/DatabaseQueue/DatabaseQueueTests.swift +++ b/Tests/Public/Core/DatabaseQueue/DatabaseQueueTests.swift @@ -26,84 +26,4 @@ class DatabaseQueueTests: GRDBTestCase { } } } - - func testSwiftCompiler() { - // Here we test that Swift compiler compiles some various usages of - // DatabaseQueue.inDatabase { ... } - // - // Goal: fix https://github.com/groue/GRDB.swift/issues/54 - -// assertNoError { -// let dbQueue = DatabaseQueue() -// try dbQueue.inDatabase { db in -// try db.execute("CREATE TABLE foo (id INTEGER PRIMARY KEY") -// } -// } -// -// assertNoError { -// let dbQueue = DatabaseQueue() -// try dbQueue.inDatabase { db in -// try db.execute("CREATE TABLE foo (id INTEGER PRIMARY KEY") -// try db.execute("CREATE TABLE bar (id INTEGER PRIMARY KEY") -// } -// } -// -// assertNoError { -// let dbQueue = DatabaseQueue() -// // TODO: make it compile -// try dbQueue.inDatabase { db in -// try db.execute("CREATE TABLE foo (id INTEGER PRIMARY KEY") -// return 1 -// } -// } -// -// assertNoError { -// let dbQueue = DatabaseQueue() -// // TODO: make it compile -// let x = try dbQueue.inDatabase { db in -// try db.execute("CREATE TABLE foo (id INTEGER PRIMARY KEY") -// return 1 -// } -// } -// -// do { -// let dbQueue = DatabaseQueue() -// dbQueue.inDatabase { db in -// let x = 1 -// } -// } -// -// do { -// let dbQueue = DatabaseQueue() -// let x = dbQueue.inDatabase { db in -// 1 -// } -// } -// -// do { -// let dbQueue = DatabaseQueue() -// dbQueue.inDatabase { db in -// 1 -// } -// } -// -// do { -// let dbQueue = DatabaseQueue() -// // TODO: make it compile -// let x = dbQueue.inDatabase { db in -// let a = 1 -// let b = 2 -// return a + b -// } -// } -// -// do { -// // TODO: make it compile -// dbQueue.inDatabase { db in -// let a = 1 -// let b = 2 -// return a + b -// } -// } - } } From ca81422c4bb0a06d78f4ac9ad5b6228bbf921b13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 20 May 2016 08:23:24 +0200 Subject: [PATCH 2/4] Issue #53: update FAQ entry for the `Generic parameter 'T' could not be inferred` error --- README.md | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 151235a12..3272bfc75 100644 --- a/README.md +++ b/README.md @@ -2942,23 +2942,32 @@ FAQ ```swift // Generic parameter 'T' could not be inferred let x = dbQueue.inDatabase { db in - let result = String.fetchOne(...) + let result = String.fetchOne(db, ...) return result } ``` This is a Swift compiler bug (see [SR-1570](https://bugs.swift.org/browse/SR-1570)). - The workaround is to explicitly declare the type of the closure result: + The general workaround is to explicitly declare the type of the closure result: ```swift - // Workaround + // General Workaround let x = dbQueue.inDatabase { db -> String? in - let result = String.fetchOne(...) + let result = String.fetchOne(db, ...) return result } ``` + You can also, when possible, write a single-line closure: + + ```swift + // Single-line closure workaround: + let x = dbQueue.inDatabase { db in + String.fetchOne(db, ...) + } + ``` + - **How do I close a database connection?** From b2e0874efa0b25bca00a00aeecb2e848a2cb249b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 20 May 2016 10:17:12 +0200 Subject: [PATCH 3/4] Configuration.maximumReaderCount documentation --- CHANGELOG.md | 4 ++++ TODO.md | 1 + 2 files changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index aab6e07fe..55753545a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ Release Notes final class DatabasePool { - init(path: String, configuration: Configuration = default) throws + init(path: String, configuration: Configuration = default, maximumReaderCount: Int = default) throws + } + struct Configuration { + + var maximumReaderCount: Int = default + } ``` diff --git a/TODO.md b/TODO.md index 49aa56935..464e90703 100644 --- a/TODO.md +++ b/TODO.md @@ -1,3 +1,4 @@ +- [ ] maximumReaderCount in Configuration: update README - [ ] FetchedRecordsController: see if we can replace identity comparison function with a function that returns an Equatable value (beware the generics trouble). See if this can help optimize memory use, and use a sortedMerge() algorithm. - [ ] GRDBCipher: remove limitations on iOS or OS X versions - [ ] FetchedRecordsController: take inspiration from https://github.com/jflinter/Dwifft From 19fbce4479f727caf21b5e56f7a4ad2b7777f328 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 20 May 2016 10:17:53 +0200 Subject: [PATCH 4/4] Configuration.maximumReaderCount documentation --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55753545a..92261d2eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,8 @@ Release Notes ```diff final class DatabasePool { - - init(path: String, configuration: Configuration = default) throws - + init(path: String, configuration: Configuration = default, maximumReaderCount: Int = default) throws + - init(path: String, configuration: Configuration = default, maximumReaderCount: Int = default) throws + + init(path: String, configuration: Configuration = default) throws } struct Configuration { + var maximumReaderCount: Int = default