Last active
March 8, 2023 00:46
-
-
Save PhilipWitte/33819b40112a18c30b43 to your computer and use it in GitHub Desktop.
How to use concept in Nim
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
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
A small correction, I had to put the
proc doBallet
after the declaration ofproc dance
only after that it works. Else it gives an errorError: undeclared identifier: 'dance'