Created
July 18, 2016 23:23
-
-
Save jonahgeorge/a56032c3f35ce328aa446d24994e7353 to your computer and use it in GitHub Desktop.
Example of a type-safe api in Crystal http://blog.tarn-vedra.de/posts/phantom-types-for-safe-apis.html
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
class Connection | |
class Connected | |
def disconnect | |
Disconnected.new | |
end | |
end | |
class Disconnected | |
def connect | |
Connected.new | |
end | |
end | |
def self.new | |
Disconnected.new | |
end | |
end | |
x = Connection.new | |
# x.disconnect # compile-time error | |
x = x.connect | |
# x.connect # compile-time error | |
x = x.disconnect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment