-
-
Save wu-lee/f8a1d921efc6d1668ec5d786cb6c8ef0 to your computer and use it in GitHub Desktop.
How to use concept in Nim
This file contains hidden or 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
type | |
CanDance = concept x | |
dance(x) # `x` is anything that has a `dance` procedure | |
proc doBallet(dancer: CanDance) = | |
# `dancer` can be anything that `CanDance` | |
dance(dancer) | |
# --- | |
type | |
Person = object | |
Robot = object | |
proc dance(p: Person) = | |
echo "People can dance, but not Robots!" | |
# --- | |
let p = Person() | |
let r = Robot() | |
doBallet(p) # works. prints: "People can dance, but not Robots!" | |
doBallet(r) # ERROR: (expected a type which CanDance) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment