Last active
August 29, 2015 14:00
-
-
Save PhilipWitte/11306682 to your computer and use it in GitHub Desktop.
Nimrod - @Keyword for OOP
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
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