-
-
Save foxoman/25f791d1ff60b17835ab2b01afb36fdd 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