How to make method generic without getting “No matching Shape found”

前提是你 提交于 2021-01-27 21:11:21

问题


I am not sure how to get past this "No matching Shape found" error, apart from writing lots of boilerplate.

The basic idea illustrated in the Gist is that I have a very basic version of a method (works, but is very specific), then a version that takes the mapper parameter and is more generic (works too, but is specific to one particular type), and then a third version which takes a type parameter and would be very useful, but doesn't compile because of this error.

Basic method:

def updatePD_FirstNames(id: ids.PersonalDetailsId, firstNames: StringLtd30): Future[Int] = {

Better method:

def updatePD_SL(id: ids.PersonalDetailsId, mapper: tables.PersonalDetails => tables.profile.api.Rep[StringLtd30], sl: StringLtd30): Future[Int] = {

Ideal method (but doesn't compile):

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

```

Full code at https://gist.github.com/aholland/0845bf29d836d672d006ab58f5f1c73c


回答1:


The only obvious problem I can see in the code you've posted is that X is unconstrained. It could be any type, includes ones that Slick doesn't know how to process.

What you can do is add a context bound on X. The bound you probably want is BaseTypedType, which is a "typed type" Slick uses to identify types it can work with. It's described from 11:30 in https://www.youtube.com/watch?v=tS6N5AaZTLA

You'd use it like this:

import slick.ast.BaseTypedType

def updatePD[X : BaseTypedType](
  id: Long, 
  selector: PersonTable => Rep[X],
  newValue: X
): DBIO[Int] =
  people.filter(_.id === id).map(selector).update(newValue)

What that means is that when you use the method...

updatePD(anId, _.name, "Alice")

...the compiler has to prove to itself that whatever X you use, there is an approproate type representation in Slick.




回答2:


This is also from Richard, but the exchange took place on gitter.

The only trouble with the first answer is that by demanding an implicit of type BaseTypedType[X] the context bound forces client code for optional columns to provide an implicit of type BaseTypedType[Option[X]] even when BaseTypedType[X] is already available.

This is unnecessary. Slick handles optional columns for you and if you provide an implicit for BaseTypedType[X] you are providing enough for it to handle columns of type Option[X].

So the context bound, while it works, is more demanding than necessary and results in having to write implicits in the client-code that involve directly referencing null and replicating logic already built into Slick. Not good.

The answer is to declare the implicit parameter as a named implicit parameter (called shape below) in its own parameter list, i.e. in long-form, not using the context bound short-hand :BaseTypedType. Then you can specify the more complicated but less demanding constraint used below.

So the solution is:

def updatePD[X] (id: Long, selector: PersonTable => Rep[X], newValue: X)
                (implicit shape: Shape[_ <: FlatShapeLevel, Rep[X], X, _]): DBIO[Int] = {
        people.filter(_.id === id).map(selector).update(newValue)
}

Understanding why shape has the exact type Shape[_ <: FlatShapeLevel, Rep[X], X, _] depends on an intimate understanding of Slick's types and implicit mechanisms. Richard may yet write a blog post on that!



来源:https://stackoverflow.com/questions/42973999/how-to-make-method-generic-without-getting-no-matching-shape-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!