lua-users home
lua-l archive

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


You and Eike are right. In my application, the classes I'm dealing with only have a single object, so I mixed the fields in with the class. Of course, if I wanted to apply this to any class with more than a single object, this won't work. I'm surprised I didn't catch that myself -- guess my kids have been waking me up too early, too many days in a row or something. :)

Thanks,
Martin

David Given wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Martin C. Martin wrote:
[...]
My "new" function takes a table to use for the object, so it goes
through it an calls setfenv() on each function[...]

But, assuming I've understood what you're talking about correctly, this
way you'll have to have a *different* function for every instance of the
object. So x.foo and y.foo cannot refer to the same function foo(), even
if x and y are instances of the same class.

[...]
function MyClass.new(obj)

If I do:

o = { foo = function foo() end }
x = MyClass.new(o)
y = MyClass.new(o)

...then when x is created, o.foo's environment will be updated to point
to x; then when y is created, o.foo's environment will be updated to
point to y. Since x.foo == y.foo == o.foo, calling x.foo() will call the
method with y as the environment. I assume this isn't what you want?

(I could be misunderstanding what you mean here; I haven't used function
environments much beyond sandboxing.)

- --
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│
│ "All power corrupts, but we need electricity." --- Diana Wynne Jones,
│ _Archer's Goon_
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFJ38Epf9E0noFvlzgRArsdAJ96CVY3yH/+Hyi0o6xmAPaU3GqTugCgkXWP
nEFwW4kxphE+xtsTTZ/LBfQ=
=w5Wr
-----END PGP SIGNATURE-----