【发布时间】:2019-08-10 09:33:01
【问题描述】:
我做了一些scala 练习。我有一个方法:
def getDepartment: (Either[String, Employee]) => Either[String, String] = ???
我需要实现主体。方法参数数据示例:
- Right(Employee("Joe", "Finances", Some("Julie")))
- Right(Employee("Mary", "IT", None))
- Left("找不到员工")
方法应该返回下一个:
- 右(“财务”)
- 对(“IT”)
- Left("找不到员工")
所以我要添加正文:
def getDepartment: (Either[String, Employee]) => Either[String, String] = _ match {
case _: Left[String, Employee] =>
println(s"Left: " + _)
_ // unbound placeholder parameter - compilation error
case _: Right[String, Employee] =>
println(s"Right: " + _)
_ // unbound placeholder parameter - compilation error
case _ =>
println(s" " + _)
_ // unbound placeholder parameter - compilation error
}
我知道我的实现是不正确的,因为编译错误一直存在。看来我没有经验来实施所需的解决方案。
关于方法声明,我发现信息是scala tutorial。但是我没有任何有用的想法如何将_ 映射到所需的类型。可能有人可以帮助修复我的编译错误,提出更好的正文实现方式。
附言
解决方案应在Optional 上实施,不进行错误处理。
附言2
任务取自online resource。练习 4.6 中的第一个任务。
【问题讨论】:
标签: scala