lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I hear the phrase "Duck Typing", in what I guess may be a
dismissive description of Lua's type system and possibly
a mysterious reference to "Duct Tape". But you can always
try to add typing to your own code, even if you cannot
enforce it in other's. For abstract datatypes with
constructors, I suggest

tag = function (t) return function (v)
      return { tag = t, value = v } end end

switch = function (case) return function (obj)
       return case[obj.tag] (obj.value) end end

and dually for structs with destructors, e.g.
for lazy streams

get = \ (stream)
      local v = stream.value
      stream.value = stream.update (v)
      return v, stream
      end

I would be interested to see examples of how to fake
types, type classes, and other such concepts within
Lua. And meanwhile, what about the etymological origins
of "Duck Typing"?
--
Gavin Wraith (gavin@wra1th.plus.com)
Home page: http://www.wra1th.plus.com/