Skip to content

Instantly share code, notes, and snippets.

@PhilipWitte
Last active August 29, 2015 14:00
Show Gist options
  • Save PhilipWitte/11272741 to your computer and use it in GitHub Desktop.
Save PhilipWitte/11272741 to your computer and use it in GitHub Desktop.
Nimrod syntax suggestions - OOP & @pragma
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