[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Lua as (Matlab like) calculator
- From: Oscar Chinellato <oscar.chinellato@...>
- Date: Tue, 28 Jun 2011 10:25:01 +0200
Hi.
As part of a larger project I'm currently working on, I need to
implement some sort of calculating engine that allows the user to
handle arrays, vectors, time series’ and related stuff. Hereby,
scriptability is the biggest requirement for the engine. With this I
mean, that for users it must be possible to run scripts but also to
perform (ad-hoc) computations developed interactively – very much like
in Matlab.
Of all the scripting languages I had a look at, Lua seems the most
appropriate one – it’s fast, it has a small footprint, it is garbage
collected and, the most important feature of all, it offers the
possibility to overload standard arithmetic and relational operators.
Unfortunately, I found out that there is no possibility to overload
the assignment operator - a feature that is indispensable for using
"standard math notation”. That is, if I write
x = myObject(…)
y = x
x.value = …
where "myObject" is some userdata defined in C, then (if I’m not
entirely wrong) y will reference to x - a behavior which for most math
inclined users is unexpected. A ”__assign” metamethod, however, would
allow the object to duplicate itself (if required) and the user would
experience the expected behavior. Obviously, this approach would leave
all code written so far unchanged and could therefore seamlessly be
integrated.
After having scanned the mailing-list for similar requests I found
out, that I’m not the only one that could use a overloadable
assignment operator. Interestingly enough, most replies question the
utility of such a metamethod and argue that one could call "copy
operators" by writing some un-mathematical construct - obviously
possible, but not what I want. I then went through the power patch
list and found a patch that adds a mutation operator ":=", which comes
close to what I want, yet will not work with built-in types - the user
would then need to distinguish between, say, an integer and a user
type, which I think is not very elegant. Moreover, the patch is no
longer applicable as is and I had to adapt it. This is not a problem,
yet it shows that “unofficial” add-ons might fall victim to new
releases.
As a consequence, I downloaded the latest release 5.2 (beta) and had a
closer look at the source code. And although I found the spots where
the assignments happen and where the appropriate (byte-) code is
generated, I have to admit that I don't understand the mechanism in
every detail – I’m not a Lua expert. In particular, I'm afraid that I
would modify the source code in an inappropriate way which would
(sooner or later) lead to a strange/wrong behavior, which, in the
worst of the cases, would not be noticed immediately.
For these reasons, I wanted to ask you guys whether you could help me
with this. Of course it would be awesome, if such an add-on would find
its way into the official release and therefore be provided by the
official Lua team (in a robust and proper way), however, any hints on
how to correctly implement this additional metamethod would be greatly
appreciated.
Cheers,
Oscar