[M3devel] object oriented programming in Quake

Jay K jay.krell at cornell.edu
Fri Jul 2 12:35:09 CEST 2010


next in the series of advanced programming in quake...



Perl/Python-esque "objects" (aka hash tables)
 - Quake doesn't have lexical scoping. Or therefore closures.
      So you have to pass the this pointer manually.
 
- Quake does allow naming a procedure to act like taking its address; it can be stored and called later.
       This is really the only thing that helps the situation.
 - I wasn't able to write "void member functions" aka "methods that don't return anything", thus the "a = "
 - There is no private/protected.
 - Similar reduction in making typos static errors: ie: a{"foo"} = 1 vs. a{"food"} = 1, whichever you meant, no matter,
   you get what you typed, no errors.
  - No "inheritance" demonstrated but er you could probably say there is JavaScript-esque prototyping -- just assign from another object (hash table)


proc NewCounter() is
  local proc inc(this) is
    this{"c"} = Add(this{"c"}, 1)
    return this
  end
  local proc get(this) is
    return this{"c"}
  end
  local this = { }
  this{"c"} = 0
  this{"inc"} = inc
  this{"get"} = get
  return this
end


local c1 = NewCounter()
local c2 = NewCounter()


write("c1 is " & c1{"get"}(c1), CR)
a = c1{"inc"}(c1)
write("c1 is " & c1{"get"}(c1), CR)
a = c1{"inc"}(c1)
a = c1{"inc"}(c1)
write("c1 is " & c1{"get"}(c1), CR)
write("c2 is " & c2{"get"}(c2), CR)
a = c2{"inc"}(c2)
a = c2{"inc"}(c2)
a = c2{"inc"}(c2)
write("c2 is " & c2{"get"}(c2), CR)



 - Jay




 		 	   		  


More information about the M3devel mailing list