Last active
August 29, 2015 14:00
-
-
Save PhilipWitte/11272741 to your computer and use it in GitHub Desktop.
Nimrod syntax suggestions - OOP & @pragma
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 Person: ref object @inheritable = | |
var | |
name*: string | |
age+: int | |
proc new*(name:string, age = 0) @init = | |
this.name = name | |
this.age = age | |
proc greet*() @pure = | |
echo "Hello, I'm ", name, "." | |
echo "I'm ", age, " years old." | |
# --- equivalent flat layout --- # | |
type Person: ref object @inheritable = var | |
name*: string | |
age+: int | |
proc new*(this:var Person, name:string, age = 0) = | |
using this | |
this.name = name | |
this.age = age | |
proc new*(T:typedesc[Person], name:string, age = 0): Person @inline = | |
System.new(result) | |
result.new(name, age) | |
proc greet*(this:Person) = | |
using this | |
echo "Hello, I'm ", name, "." | |
echo "I'm ", age, " years old." | |
# NOTES: | |
# - procs cannot appear in the body of the 'flat' type since 'var' appears inline | |
# - @init builds both procs. One for allocation, and one for initialization only | |
# - @pure makes the 'this' param on 'greet' not 'var' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment