tl icon indicating copy to clipboard operation
tl copied to clipboard

`final` as new keyword

Open fperrad opened this issue 1 year ago • 7 comments

Lua 5.4 & Teal support the <const> annotation. That allows immutable variable. But for large use, the annotation syntax is ugly and too long.

My proposal is just a sugar syntax based on a new keyword final: final foo = something --> local foo <const> = something final fn() end --> local fn <const> = function() end

fperrad avatar Sep 20 '23 13:09 fperrad

what would it do if the target isn't lua5.4?

lenscas avatar Sep 20 '23 14:09 lenscas

quoted from https://github.com/teal-language/tl/blob/master/docs/tutorial.md#const-variables

The <const> annotation works in Teal like it does in Lua 5.4 (it works at compile time, even if you're running a different version of Lua).

fperrad avatar Sep 20 '23 18:09 fperrad

I have restrained myself from making purely cosmetic changes/additions to the "Lua parts of the language" so far, and I don't have immediate plans to change this. But if I were to do this, I'd probably just name it const.

hishamhm avatar Sep 20 '23 18:09 hishamhm

const is fine too.

For an example (with a small module) of the tedious syntax of annotation, see https://framagit.org/fperrad/lua-Rotas/-/commit/a2c631309aceb778dde93f044f168e56227d33c2

fperrad avatar Sep 21 '23 19:09 fperrad

I read the previous comments, 5.1 const can just throw an error if you do something dumb, const could be noice for functions too! Like if you want

function(test: const number) 
  test = test + 1
end

would throw an error like that's a const, you're dumb or something more professional during transpile. This would make working with lua 5.1 (like in minetest) soooo much easier

jordan4ibanez avatar Nov 02 '23 21:11 jordan4ibanez