- sealed class SafariBookAccess
- data class Granted(val expirationDate: DateTime) : SafariBookAccess()
- data class NotGranted(val error: AssertionError) : SafariBookAccess()
- data class Blocked(val message: String) : SafariBookAccess()
那么隐蔽在 access() 函数后面的重要意图是什么?全分列!让我们列举下。
- fun access(contract: Contract,
- employee: Employee) = when (contract to employee) {
- PROBATION to SENIOR_ENGINEER,
- PROBATION to REGULAR_ENGINEER -> NotGranted(AssertionError("Access not allowed on probation contract."))
- PERMANENT to SENIOR_ENGINEER,
- PERMANENT to REGULAR_ENGINEER,
- PERMANENT to JUNIOR_ENGINEER,
- CONTRACTOR to SENIOR_ENGINEER -> Granted(DateTime(1))
- CONTRACTOR to REGULAR_ENGINEER,
- PROBATION to JUNIOR_ENGINEER,
- CONTRACTOR to JUNIOR_ENGINEER -> Blocked("Access for junior contractors is blocked.")
- else -> throw AssertionError("Unsupported case of $employee and $contract")
- }
- fun access(employee: Employee,
- contract: Contract): SafariBookAccess {
- return when (employee) {
- SENIOR_ENGINEER -> when (contract) {
- PROBATION -> NotGranted(AssertionError("Access not allowed on probation contract."))
- PERMANENT -> Granted(DateTime())
- CONTRACTOR -> Granted(DateTime())
- }
- REGULAR_ENGINEER -> when (contract) {
- PROBATION -> NotGranted(AssertionError("Access not allowed on probation contract."))
- PERMANENT -> Granted(DateTime())
- CONTRACTOR -> Blocked(
推荐阅读
开辟者大年夜赛路演 | 12月16日,技巧立异,北京不见不散 登录网站时必须输入的图片验证码可以用来辨认拜访者到底是人照样机械——这同时也是某种程度上的「图灵测试」,人工智能>>>详细阅读
本文标题:6个能让你的Kotlin代码库更有意思的“魔法糖”
地址:http://www.17bianji.com/lsqh/39802.html
1/2 1