lua-users home
lua-l archive

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


1) I'd like to see `a` (note the backticks) be a run-once form of
string.byte('a') -- a single-character transform that happens at
compile-time.  When you use string.byte() in a loop it'll be run every
time, and I argue that creating a local for this purpose is difficult
to name and largely a waste of a local.

Since I've been guilty of using magical Ascii Codes in my source code like 47 to check if a string ends with a '/' I have some sympathy for it. Thanks to this thread I also learned why this is a bad idea.

However, this can easily be worked around with "local slash = string.byte( '/' )" at top context so its computed only once.

The root I see is missing immutables, which is a far bigger scope, the string 'a' is immutable. the function byte() has no side-effects or is side-effect dependent thus we expect the computation to be done only once. Same would be "muh":sub( 2 ) wouldn't it?. However, "string" is a table that is not frozen, so string.byte could be changed (counter intuitively)

In my coding experiences over the years I'm moving more and more to be working immutable objects. While it sounds contra intuitive to make a copy of a whole table, just because you want to change one value, and think you are working effectively, at the end of the day this can actually have lots of value. I'm currently hacking procedural languages to simulate this functional paradigm of deeply frozen objects. I'd suppose this is just some thing thats not in these language designs and thus something we decided to live with.

In my experience it takes experience with bigger projects to learn the value of immutables. Back the day when java first introduced immutable strings to me, I was, WTF is wrong with them? Nowadays I know a bit better.

single-character literals in code, I don't see it worth the hassle. Just make variables at root context to be assigned on startup if you care about that call in every loop iteration.