Created
June 9, 2017 06:55
-
-
Save KatagiriSo/20ae9cf730b6662ff641f289815099a3 to your computer and use it in GitHub Desktop.
phantom type dummy code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ref http://qiita.com/taketo1024/items/71e3272211f08d7e0cde | |
class Status {} | |
class Status_NORMAL : Status {} | |
class Status_HIGH : Status {} | |
class Car<T: Status> { | |
static func createNormalCar() -> Car<Status_NORMAL> { | |
return Car<Status_NORMAL>() | |
} | |
static func createHighCar() -> Car<Status_HIGH> { | |
return Car<Status_HIGH>() | |
} | |
} | |
extension Car where T: Status_HIGH { | |
func setHighSpeedMode() { | |
print("High Speed Mode On") | |
} | |
} | |
/* | |
let high_car = Car.createHighCar() | |
let normal_car = Car.createNormalCar() | |
high_car.setHighSpeedMode() ok | |
normal_car.setHighSpeedMode() compile error | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment