[英]Scala Type Mismatch For Subclasses of The Same Parent
UserGetResponse and GeneralResponse are sublclasses of BaseResponse, which is as follows: UserGetResponse和GeneralResponse是BaseResponse的子类,如下所示:
abstract class BaseResponse()
And the function I use to retrieve users is as follows: 我用来检索用户的功能如下:
def userGet(userId: Int)(implicit ec: ExecutionContext): Future[BaseResponse] = Future {
val response = users.get(userId) map { user =>
val userRes = new UserResponse(user.id, user.firstname, user.lastname, user.organisationid, user.email, user.password, user.usertype)
new UserGetResponse(1, "Successful retrieved the user.", userRes)
} getOrElse {
GeneralResponse(0, s"Error retrieving user. User does not exist.")
}
}
where users is another class with methods get, insert etc. defined. 用户是定义了获取,插入等方法的另一个类。 I am getting the following compilation error:
我收到以下编译错误:
type mismatch;
[error] found : Unit
[error] required: package.name.BaseResponse
[error] }
What am I doing wrong? 我究竟做错了什么?
The closure inside the Future
is not returning anything, so the compiler infers that it's return type is Unit
, and complains because it should be BaseResponse
. Future
内部的闭包不返回任何内容,因此编译器推断其返回类型为Unit
,并且抱怨,因为它应该为BaseResponse
。
Removing val response =
from the beginning of the function (or adding response
before the end) should fix it. 从函数的开头删除
val response =
(或在结尾之前添加response
)应该可以解决该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.