Friday, February 20, 2009

Simulate Object with Groovy's Closures and Map

After reading Chapter 18 of Types and Programming Languages by Benjamin C. Pierce, I've got an idea to simulate how OOP is working with closures and map in Groovy. Just for fun anyway :)

def func_01 = { self, props ->
  self.name = props["name"]
}

def func_02 = { self ->
  println "${self.name} is running"
}

def a = [init: func_01, run: func_02]
a.self = a

a.init(a.self, [name:"mr. a"])
a.run(a.self)


1 comment:

Anonymous said...

มี self เหมือน python
แต่ใช้ function(closure) สร้าง object
ยังกับ javascript