无法解析 Option 类型上的 Scala flatMap 方法。这样做的正确方法是什么?
Scala flatMap method on Option type cannot be resolved. What is the correct way to do this?
def jobEventToJobEventTimeLine(jobEvent: JobEvent): JobEventTimeline = {
val OpFields(stepId, jobBaseStepId, fieldId) = extractFromOp(jobEvent.getOp)
JobEventTimeline(
jobEvent.getJobId,
if (jobEvent.isSetTyp) Some(jobEvent.getTyp.toString) else None,
if (jobEvent.isSetInPlanning) Some(jobEvent.inPlanning) else None,
jobEvent.getPedigree.getTeamId,
jobEvent.getPedigree.getActorId,
if (jobEvent.getPedigree.isSetAdminActorId) Some(jobEvent.getPedigree.getAdminActorId) else None,
jobEvent.getPedigree.getSessionId,
if (jobEvent.getPedigree.isSetClientSessionId) Some(jobEvent.getPedigree.getClientSessionId) else None,
jobEvent.getPedigree.getCreatedAt,
jobEvent.getPedigree.getSeqId,
if (jobEvent.getPedigree.isSetIsSideEffect) Some(jobEvent.getPedigree.isSideEffect) else None,
jobEvent.getOp.getSetField.toString,
stepId,
jobBaseStepId,
fieldId,
if (jobEvent.isSetReceivedAt) Some(jobEvent.getReceivedAt) else None
)
}
def ToJobEvent(binData: Array[Byte]): Option[JobEventTimeline] = FuncUtils.deserializeJobEvent(binData) flatMap Some(jobEventToJobEventTimeLine(_))
我正在尝试将 flatMap 用作绑定运算符以将 monadic Value 应用于 monad,但是我的 IDE 说 flatMap 无法解析。这样做的正确方法是什么?
Some(jobEventToJobEventTimeLine(_))
表示 Some(x => jobEventToJobEventTimeLine(x))
,而不是 x => Some(jobEventToJobEventTimeLine(x))
,因此类型不匹配。写
FuncUtils.deserializeJobEvent(binData).flatMap { x => Some(jobEventToJobEventTimeLine(x)) }
明确地。
另外,if (jobEvent.isSetTyp) Some(jobEvent.getTyp.toString) else None
可以写成Option.when(jobEvent.isSetTyp)(jobEvent.getTyp.toString)
。
def jobEventToJobEventTimeLine(jobEvent: JobEvent): JobEventTimeline = {
val OpFields(stepId, jobBaseStepId, fieldId) = extractFromOp(jobEvent.getOp)
JobEventTimeline(
jobEvent.getJobId,
if (jobEvent.isSetTyp) Some(jobEvent.getTyp.toString) else None,
if (jobEvent.isSetInPlanning) Some(jobEvent.inPlanning) else None,
jobEvent.getPedigree.getTeamId,
jobEvent.getPedigree.getActorId,
if (jobEvent.getPedigree.isSetAdminActorId) Some(jobEvent.getPedigree.getAdminActorId) else None,
jobEvent.getPedigree.getSessionId,
if (jobEvent.getPedigree.isSetClientSessionId) Some(jobEvent.getPedigree.getClientSessionId) else None,
jobEvent.getPedigree.getCreatedAt,
jobEvent.getPedigree.getSeqId,
if (jobEvent.getPedigree.isSetIsSideEffect) Some(jobEvent.getPedigree.isSideEffect) else None,
jobEvent.getOp.getSetField.toString,
stepId,
jobBaseStepId,
fieldId,
if (jobEvent.isSetReceivedAt) Some(jobEvent.getReceivedAt) else None
)
}
def ToJobEvent(binData: Array[Byte]): Option[JobEventTimeline] = FuncUtils.deserializeJobEvent(binData) flatMap Some(jobEventToJobEventTimeLine(_))
我正在尝试将 flatMap 用作绑定运算符以将 monadic Value 应用于 monad,但是我的 IDE 说 flatMap 无法解析。这样做的正确方法是什么?
Some(jobEventToJobEventTimeLine(_))
表示 Some(x => jobEventToJobEventTimeLine(x))
,而不是 x => Some(jobEventToJobEventTimeLine(x))
,因此类型不匹配。写
FuncUtils.deserializeJobEvent(binData).flatMap { x => Some(jobEventToJobEventTimeLine(x)) }
明确地。
另外,if (jobEvent.isSetTyp) Some(jobEvent.getTyp.toString) else None
可以写成Option.when(jobEvent.isSetTyp)(jobEvent.getTyp.toString)
。