Skip to content

Instantly share code, notes, and snippets.

@PhilipWitte
Last active August 29, 2015 14:00
Show Gist options
  • Save PhilipWitte/11306682 to your computer and use it in GitHub Desktop.
Save PhilipWitte/11306682 to your computer and use it in GitHub Desktop.
Nimrod - @Keyword for OOP
macro class* (head:expr, body:stmt): stmt @keyword =
## builds OOP-style type/procs...
# @keyword makes this behave in the following ways..
# - is compiled (no VM) for compile-time performance
# - is used with '=' instead of ':' (like 'proc' not 'for')
macro init* (head:expr, params:expr{param}, body:stmt): stmt @keyword =
## builds OOP-style alloc/init procs for a type
# NOTE: {param} means 'params' only accepts parameter syntax (a:T, b:U, ...)
# ---
type Person*: ref object @inheritable =
name*: string
age*: int
# ---
class Person* @inheritable =
var
name*: string
age*: int
init new*(name:string, age = 0) =
this.name = name
this.age = age
proc greet* @pure =
echo "Hello, I'm ", name, "."
echo "I am ", age, " years old."
# ---
var philip = Person.new("Philip Witte", 26)
philip.greet()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment