lua-users home
lua-l archive

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


Luis Carvalho wrote:
> > The problem here is that for the arithmetic operators I need to know
> > if the operands are real or complex number or real or complex
> > matrices. The problem is that if I use the 'type' function I get can
> > obtain just 'number' or 'cdata' and I have no means to discriminate
> > between, for example, a complex number and a matrix because both are
> > cdata.
> 
> I might be missing something, but why don't you just store this information in
> the matrix struct? I mean, what do you need this information through a 'type'
> function?

I guess he wants to e.g. multiply a matrix with either a number, a
complex number or another matrix. And all of that from the same
__mul metamethod. Currently there's no easy way to distinguish the
five cases: mat*mat, mat*n, n*mat, mat*c, c*mat.

> -- note the icomplex field in the struct:
> ffi.cdef [[
> typedef struct { int size, stride, icomplex; double *data; } Vector;
> ...
> ]]

A test based on the contents of a mutable field cannot be
eliminated by the compiler. You could use a 'static const int'
member, though.

Note that
  if type(x) == "number" then ... end
is a zero-cost test with LuaJIT, because the basic type of a Lua
object is a constant.

One would need something similar to check the exact ctype without
necessarily extending it with a field (which is problematic e.g.
for numbers).

--Mike