by IshKebab · 1y ago · 0 comments
2. Doesn't make the mistake of 1-based indexing.
3. Generally much more modern and nicer language than Lua.
The reasons I've found not to use it:
1. Very slow - even slower than Python! Of course often this won't matter but it would be nice if it was fast.
2. No support for type annotations.
I've used it before for a config file, and it worked well there. I think if I wanted anything much more complex though I would just embed V8. Then you get a language that everyone already knows and very good type annotations.
I don't think "ecosystem" really matters much for embeddable languages like this. You app is the ecosystem.
Genuine question, as I don't have any prior experience embedding any scripting language into a Rust project.
The fact that Rhai builds with just 'cargo build' shouldn't be underestimated - a Rust project with all pure-Rust dependencies is much easier to maintain / support / distribute across a wide variety of hosts!
$ cargo add mlua --features lua54,vendored
Then mlua will statically build with the Lua sources it bundles itself and no need to link the system Lua or do anything other than "cargo build" like normal.`cargo cross` exists, which can help but it's really a kludge.
Even something like a `Vec<u64>` is likely to be a right pain with Lua.
If you are thinking in rust already, why would you go to an FFI c solution?
(Source: have been prototyping DSLs in rust with most of my life force for about a year)
I don't mean to be overly harsh, but this is just not a valid/serious answer. The other reasons are fine. This is pure bike-shedding.
This doesn't make one better than the other, but precedent/familiarity does matter and represents a valid decision weight.
(I don't have a dog in this fight; I'm not a user of either Lua or Rhai.)
No one can convince me thst 0-based indexing is worth more than widespread editor syntax -highlighting support, working LSPs, lots of blog posts, books and more vs a new niche language whose marquee feature seems to be it is easier to embed in rust.
Again, I don't want to put down the authors, making languages and tooling is fun and awesome, but I wouldn't pretend this is a rstional replacement for Lua.
Lua is my first 1-indexed language, i have yet to have a single bug over this. It's a irrational fear.
It absolutely is. I gave you concrete reasons. (Edit: actually in a sibling comment)
> i have yet to have a single bug over this.
I never said anything about bugs.
Also I don't think Lua's LSP/type annotations are anything to boast about. They exist, but they aren't good. If you care about that there are way better choices - Typescript, or maybe Dart (not sure how embeddable that is though).
A lot of people would call this a feature, not a mistake. 0-based indexing came from programming languages typically calculating addresses in the memory layout of an array. Whereas in mathematics (the background of the Lua inventor) sequence positions typically start at 1. Also, humans generally start counting from 1. This is intuitive in languages like SQL which also use 1-based indexing.
It could be argued that 0-based indexing was the mistake since it actually conflates two concepts, the memory layout in the machine, and the actual sequence you want to index.
Fundamentally either work, but 1-based indexing is a mistake mainly because it leads to much less elegant code.
A simple example is accessing rows in a flattened matrix (or 2D array).
With 0-based intervals and right-open intervals it is
array[stride*y .. stride*y + width]
With 1-based it is array[stride*(y-1)+1 .. stride*(y-1)+1+width]
Ouch. Code dealing with intervals is simpler with 0-based indexing & right-open intervals 99.9% of the time.Take a look at the example code here:
https://www.lua.org/pil/11.2.html
What index does the flattened `mt` start at?
> What index does the flattened `mt` start at?
I'm not sure if I missed a point you were getting at with this? Lua array table indexes start at 1.
The code example is
mt = {} -- create the matrix
for i=1,N do
for j=1,M do
mt[i*M + j] = 0
end
end
What index do I use to access the first element in mt? It isn't 1!If this was written using 0-based indices then the answer would be zero.
This could be argued to be a feature. That it isn't conflating the concepts but communicating both.
Why is this a mistake? I've used 0-based languages primarily, but ergonomically, 1-based languages like Awk and Smalltalk are fine too, making some code slightly harder to write and other code slightly easier. Overall, I've found it to be a wash. If anything, in a pointer-less language, pedagogically, 1-based is more intuitive for novices.
P.S. Classic VB and VBA had an odd feature where one could choose the base for themselves, using the Option Base feature.