【问题标题】:How is this a "type mismatch"?这是如何“类型不匹配”?
【发布时间】:2012-12-02 20:27:45
【问题描述】:
found   : (Int, String, Option[java.lang.String])
required: (Int, String, Option[java.lang.String])

相关代码:

object M extends Table[(Int, String, Option[String])]("table") {

  def msaid = column[Int]("msaid", O NotNull)
  def name = column[String]("name", O DBType "varchar(255)")
  def shape = column[Option[String]]("shape")
  def * = msaid ~ name ~ shape

  type T = (Int, String, Option[java.lang.String])

  def apply(msa: T) = 1

  def q() = db withSession { s: Session => (for (r <- M) yield M(*)).list()(s) }
                                                                 ^
                                                                 ^
...

我也试过

  type T = (Int, String, Option[String])

最终目标是我希望所有选定的列都转换为具有命名访问器的对象,而不是元组。

Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_07).

更新:

这是问题的Gist(从上面的代码略微简化,并通过仅使用 Int 消除了任何 String/java.lang.String “混淆”)

【问题讨论】:

    标签: scala scalaquery


    【解决方案1】:

    错误消息并没有用来告诉您哪个是 TupleN,尽管我认为这在某些时候得到了改进。不匹配发生在元组和 n 个参数之间。或者不。

    fix 在 2.9.2 中。我注意到您的 .sbt 使用 2.9.1 scalaquery,以防万一。 scala-tools.org 不是已经过时了吗?抱歉帮了一半。

    作为一个非用户,看起来 Projection2 不是您寻找的元组,尽管它是一个产品:

    class Projection2 [T1, T2] extends (Column[T1], Column[T2]) with Projection[(T1, T2)] 
    

    回复:

    scala> M.column[Int]("id") ~ M.column[Int]("n")
    res1: (Int, Int) = Projection2
    
    scala> M(res1)
    <console>:23: error: type mismatch;
     found   : (Int, Int)
     required: (Int, Int)
                  M(res1)
                    ^
    
    scala> M.apply
                                    def apply(v: (Int, Int)): Int   
    
    scala> M.apply((1,2))
    res3: Int = 1
    

    【讨论】:

    • 谢谢!确实这是我需要的投影。我被错误的困惑所困扰,以至于认为它甚至不是元组而只使用 REPL。也感谢您发现我的其他不一致之处。
    猜你喜欢
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 2011-01-14
    • 1970-01-01
    • 2021-09-15
    • 2022-08-06
    相关资源
    最近更新 更多