我不知道如何突破這個「無匹配的形狀找到」錯誤,除了寫很多的樣板。在Gist中說明的基本思想是,我有一個非常基本的方法版本(工作,但非常具體),然後一個版本,採用mapper
參數,更通用(也可以工作,但是是特定的到一個特定的類型),然後是第三個版本,它接受一個類型參數並且非常有用,但由於這個錯誤而不能編譯。如何使通用的方法沒有得到「找不到匹配的形狀」
基本方法:
def updatePD_FirstNames(id: ids.PersonalDetailsId, firstNames: StringLtd30): Future[Int] = {
更好的方法:
def updatePD_SL(id: ids.PersonalDetailsId, mapper: tables.PersonalDetails => tables.profile.api.Rep[StringLtd30], sl: StringLtd30): Future[Int] = {
理想的方法(但不編譯):
def updatePD_X[X](id: ids.PersonalDetailsId, mapper: tables.PersonalDetails => tables.profile.api.Rep[X], sl: X): Future[Int] = {
```
[server] $ compile
[info] Compiling 1 Scala source to ... target\scala-2.12\classes...
[error] ...schema\DbProxy.scala:688: No matching Shape found.
[error] Slick does not know how to map the given types.
[error] Possible causes: T in Table[T] does not match your * projection,
[error] you use an unsupported type in a Query (e.g. scala List),
[error] or you forgot to import a driver api into scope.
[error] Required level: slick.lifted.FlatShapeLevel
[error] Source type: slick.lifted.Rep[X]
[error] Unpacked type: T
[error] Packed type: G
[error] val q2: Query[tables.profile.api.Rep[X], X, Seq] = q1.map(mapper)
[error] ^
[error] one error found
[error] (server/compile:compileIncremental) Compilation failed
[error] Total time: 4 s, completed 23-Mar-2017 11:15:47
```
的完整代碼在https://gist.github.com/aholland/0845bf29d836d672d006ab58f5f1c73c
非常感謝你,理查德! –