我在我的应用程序中使用Kotlin和Springboot。我想要设置病人problem.problemId =。patientId在一侧是lambda函数,但是Kotlin给出错误作为响应。我得到以下错误类型
Assignments are not expressions, and only expressions are allowed in this context Spring Boot Kotllin
patientservice.kt
package com.nillmani.hospitalmanagement.service
import com.nillmani.hospitalmanagement.entity.Patient
import com.nillmani.hospitalmanagement.exception.PatientNotFoundException
import com.nillmani.hospitalmanagement.model.ReqPatient
import com.nillmani.hospitalmanagement.repository.PatientRepository
import org.modelmapper.ModelMapper
import org.slf4j.Logger
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*
import java.util.function.Consumer
@Service
class PatientService {
@Autowired
private lateinit var patientRepository: PatientRepository
private lateinit var modelMapper : ModelMapper
private lateinit var logger: Logger
fun PatientService(patientRepository: PatientRepository, modelMapper: ModelMapper, logger: Logger) {
this.patientRepository = patientRepository
this.modelMapper = modelMapper
this.logger = logger
}
@Throws(Exception::class)
fun findAll():List<ReqPatient>{
try {
var patient : List<Patient?>? = patientRepository.findAllByStatusEquelsOne()
if (patient != null) {
if (patient.size < 1){
logger.error("Here No patient Record available")
throw PatientNotFoundException()
}
}
val dto : ReqPatient = modelMapper.map(patient,ReqPatient::class.java)
val patientDto : List<ReqPatient> = listOf(dto)
patientDto.forEach{patient ->(patient.problems).forEach { problem ->(problem.problemId=patient.patientId ) }}
return listOf(dto)
}catch (e:Exception){
throw Exception(e)
}
}
}
我在这行得到错误
val patientDto : List<ReqPatient> = listOf(dto)
patientDto.forEach{patient ->(patient.problems).forEach { problem ->(problem.problemId=patient.patientId ) }}
如果我使用赋值操作符,我如何将PatientId分配给ProblemIDKotlin给出异常
你需要去掉赋值的括号
val patientDto : List<ReqPatient> = listOf(dto)
patientDto.forEach{patient ->
(patient.problems).forEach { problem -> problem.problemId = patient.patientId }
}
在kotlin中,您可以将表达式括在括号中以创建parenthesizeexpression。自赋值不是表达式,你会得到错误。