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] 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 -// } -// } - } }