Mudlet: Complement 0-indexed functions with 1-indexed

Created on 16 Apr 2020  ·  8Comments  ·  Source: Mudlet/Mudlet

Brief summary of issue / Description of requested feature:

We've got a few functions in TLuaInterpreter.cpp from an early design which index starting from 0 - which isn't idiomatic Lua and unintuitive. Need to add 1-indexed complements to them, like https://wiki.mudlet.org/w/Manual:Mapper_Functions#getExitStubs1.

Steps to reproduce the issue / Reasons for adding feature:

More intuitive API.

Error output / Expected result of feature

Any function that returns an indexed table starting at 0 should also have a complementary function that starts indexing at 1.

Extra information, such as Mudlet version, operating system and ideas for how to solve / implement:

4.6.2

enhancement help wanted

Most helpful comment

Well, how about we announce that eventually we will have a getLuaApiVersion() number that will return a version number string of the form 1.x.y, and promise that when we go to version 2 all the broken/defunct functions will be fixed/removed and all the remainder will have completely uniform argument handling and return values (including nil + error messages for every run-time problem).

Of course it will then provide an opportunity to smash the backwards compatibility so we need to get it into the Lua API A.S.A.P. so that packages and scripts can start to defend themselves against such a breaking change.

We might even consider doing that concurrent with a change to (float) mudlet::mMudletXmlDefaultFormat to a value >= 2.0 - which would be appropriate as any XML save file or package bearing such a number will be rejected completely (by design) by any Mudlet version since 3.0.1 and will prompt the following:

[ ALERT ] - Sorry, the file being read:
            XXXXX.xml
            reports it has a version (2.0.0) it must have come from a later Mudlet version,
            and this one cannot read it, you need a newer Mudlet!

Needless to say such a change will be massive and would need to also be concurant with a Major Mudlet version release but I would not expect that to be before Mudlet 6.0 at the earliest. OTOH It would then be possible to switch to a later Lua version.

All 8 comments

How about adding both getExitStubs1 and getExitStubs0 so we can give fair warning and make getExitStubs work as expected at some point? That way Mudlet could become more idiomatic and intuitive in the end.

I get what you're saying, but that would break existing scripts for what is relatively speaking a trivial issue, not a good tradeoff. It's nice that someone can pick up some code from a forum from a couple of years ago, drop it in, and have it work.

If we could only magically influence all the code that's been put out there... :)

I remember something like rust-lang's fix command... (or was golang fix) where it would magically fix things.

--the following possible functions have 0-indexed
function getRoomsByPosition()
function getExitStubs() --Obviously..
function getAreaRooms()

edit:
Look like that would be just those functions that needed to be changed to return a proper 1-indexed reply.

Well, how about we announce that eventually we will have a getLuaApiVersion() number that will return a version number string of the form 1.x.y, and promise that when we go to version 2 all the broken/defunct functions will be fixed/removed and all the remainder will have completely uniform argument handling and return values (including nil + error messages for every run-time problem).

Of course it will then provide an opportunity to smash the backwards compatibility so we need to get it into the Lua API A.S.A.P. so that packages and scripts can start to defend themselves against such a breaking change.

We might even consider doing that concurrent with a change to (float) mudlet::mMudletXmlDefaultFormat to a value >= 2.0 - which would be appropriate as any XML save file or package bearing such a number will be rejected completely (by design) by any Mudlet version since 3.0.1 and will prompt the following:

[ ALERT ] - Sorry, the file being read:
            XXXXX.xml
            reports it has a version (2.0.0) it must have come from a later Mudlet version,
            and this one cannot read it, you need a newer Mudlet!

Needless to say such a change will be massive and would need to also be concurant with a Major Mudlet version release but I would not expect that to be before Mudlet 6.0 at the earliest. OTOH It would then be possible to switch to a later Lua version.

Is it possible to resolve it along the lines of making table[0] and table[#table] the same reference? So code that used table[0] and ipairs works without issue?
I don't think it is the fix as it causes a duplicate event for code that uses for i=0, #table do
It seems like an issue that has a clever workaround.

Could there be a method using metatable _index? I don't fully comprehend metatables.

Was this page helpful?
0 / 5 - 0 ratings