Skip to content

Instantly share code, notes, and snippets.

@wu-lee
Forked from PhilipWitte/concepts.nim
Created March 24, 2018 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wu-lee/f8a1d921efc6d1668ec5d786cb6c8ef0 to your computer and use it in GitHub Desktop.
Save wu-lee/f8a1d921efc6d1668ec5d786cb6c8ef0 to your computer and use it in GitHub Desktop.
How to use concept in Nim
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