Skip to content

Instantly share code, notes, and snippets.

@sliskiCode
Last active December 2, 2017 08:33
Show Gist options
  • Save sliskiCode/1aa6e774ee1f1a2cbfa18362d0e54ddb to your computer and use it in GitHub Desktop.
Save sliskiCode/1aa6e774ee1f1a2cbfa18362d0e54ddb to your computer and use it in GitHub Desktop.
6 magic sugars that can make your Kotlin codebase happier #10
fun access(contract: Contract,
employee: Employee) = when (Pair(contract, employee)) {
Pair(PROBATION, SENIOR_ENGINEER),
Pair(PROBATION, REGULAR_ENGINEER),
Pair(PROBATION, JUNIOR_ENGINEER) -> NotGranted(AssertionError("Access not allowed on probation contract."))
Pair(PERMANENT, SENIOR_ENGINEER),
Pair(PERMANENT, REGULAR_ENGINEER),
Pair(PERMANENT, JUNIOR_ENGINEER),
Pair(CONTRACTOR, SENIOR_ENGINEER) -> Granted(DateTime(1))
Pair(CONTRACTOR, REGULAR_ENGINEER),
Pair(CONTRACTOR, JUNIOR_ENGINEER) -> Blocked("Access for junior contractors is blocked.")
else -> throw AssertionError("Unsupported case of $employee and $contract")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment