lua-users home
lua-l archive

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


Suppose I have a user-defined type implemented in C.

  x = mytype.new()

Suppose my type has both data members (ie. passive) and methods
which actually perform an action.  Is it preferred to use method
calls for both?

  x:do_something(5)  -- Clearly should be a function.
  x:name()  -- Called as a function, even though it's just data?
  x.name    -- Returned as a string -- is this better?

Aesthetically the last one seems more pleasing, since it makes clear
that it's just returning some data.  But what do you all as Lua
programmers expect?  I guess one downside of the second is that
it might suggest that you can set it also, which is not allowed
for my type:

  -- Not allowed, but I can prevent this by implementing __newindex
  x.name = "Haha!"

As a second question, most user-defined C types seem to be
constructed with "new()" functions (eg. mytype.new().  Is there
any precedent for using the __call metamethod to make construction
look like a function call?

  x = mytype()

I want to make whatever choices are most idiomatic, but if there
are any noticeable efficiency differences that is a factor too.

Josh