重载“apply"方法时:错误消息“值元组不是对象的成员" [英] When overloading `apply` method: Slick error message 'value tupled is not a member of object'
问题描述
我需要能够通过在某些情况下提供除 id
之外的所有值来创建 User
对象,以便 User
对象采用注意为自己分配一个自动生成的值.
I need an ability to create a User
object by providing all the values except id
in certain cases, such that the User
object takes care of assigning itself an auto-generated value.
为此,我重载了伴生对象中的 apply 方法,如下所示.但这会导致编译时错误:value tupled is not a member of object
.
For this I have overloaded the apply method in the companion object, like shown below. But this is causing the compile time error: value tupled is not a member of object
.
StackOverflow 和其他博客上提到的解决方案不起作用,例如:http://queirozf.com/entries/slick-error-message-value-tupled-is-not-a-member-of-object
Solutions mentioned on StackOverflow and other blogs aren't working, such as: http://queirozf.com/entries/slick-error-message-value-tupled-is-not-a-member-of-object
case class User(id: Long, firstName: String, lastName: String, mobile: Long, email: String)
object User {
private val seq = new AtomicLong
def apply(firstName: String, lastName: String, mobile: Long, email: String): User = {
User(seq.incrementAndGet(), firstName, lastName, mobile, email)
}
}
class UserTableDef(tag: Tag) extends Table[User](tag, "user") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def firstName = column[String]("first_name")
def lastName = column[String]("last_name")
def mobile = column[Long]("mobile")
def email = column[String]("email")
override def * =
(id, firstName, lastName, mobile, email) <> (User.tupled, User.unapply)
}
推荐答案
问题的根源在于重载了 apply
def.
The source of your problem is that overloaded apply
def.
tupled
不适用于 case class
的 少于 2 个参数
或 overloaded apply
.
tupled
does not work with case class
's with less than 2 parameters
or overloaded apply
.
就 slick 的 *
(或全部)映射和 <>
而言,它应该是这样的,
As far as slick's *
(or all) mapping and <>
is concerned, it is supposed to be like,
def * = (tupleMember1, tupleMember2, ...) <> (func1, func2)
这样,
func1
将该元组(tupleMember1, tupleMember2, ...)
作为输入并返回映射类/案例类的实例.func1
获取映射类/案例类的实例并返回该元组(tupleMember1, tupleMember2, ...)
.
func1
takes that tuple(tupleMember1, tupleMember2, ...)
as input and returns an instance of mapped class/case class.func1
takes an instance of mapped class/case class and returns that tuple(tupleMember1, tupleMember2, ...)
.
因此您可以提供任何功能...满足这些要求.
So you can provide any function... which meets these requirements.
case class User(id: Long, firstName: String, lastName: String, mobile: Long, email: String)
object User {
private val seq = new AtomicLong
def apply(firstName: String, lastName: String, mobile: Long, email: String): User = {
User(seq.incrementAndGet(), firstName, lastName, mobile, email)
}
def mapperTo(
id: Long, firstName: String,
lastName: String, mobile: Long, email: String
) = apply(id, firstName, lastName, mobile, email)
}
class UserTableDef(tag: Tag) extends Table[User](tag, "user") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def firstName = column[String]("first_name")
def lastName = column[String]("last_name")
def mobile = column[Long]("mobile")
def email = column[String]("email")
override def * =
(id, firstName, lastName, mobile, email) <> ((User.mapperTo _).tupled, User.unapply)
}
这篇关于重载“apply"方法时:错误消息“值元组不是对象的成员"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!