Type-safe variables

113 views
Skip to first unread message

Kai

unread,
Jun 19, 2024, 5:15:02 AM (14 days ago) Jun 19
to lua-l
Hello everyone,

Please excuse me if this suggestion has been made before.

I have a special wish, which I'm sure has been expressed before.
It would be great if Lua would get optional type safety, e.g. in version 5.5 or 6.0.

I imagine it like this:

local var = ""

Is initialised as a string and can still be used e.g. for numbers, tables etc.

local var <string> = ""

Is initialised as a string and can only be used as a string.

var = 1 would generate an error here

For functions, you may be able to solve this in this way:

function test(var <string>, num <number>)

What do you think of this?

Thijs Schreijer

unread,
Jun 19, 2024, 5:21:16 AM (14 days ago) Jun 19
to lu...@googlegroups.com
There already are some stronger typed derivatives, checkout Pallene [1] or Teal [2] for example.

Thijs


Sainan

unread,
Jun 19, 2024, 10:37:40 AM (14 days ago) Jun 19
to lu...@googlegroups.com
Do you actually need type safety or do you just think you need type safety? I think Lua has really robust type semantics, and it already throws errors in all the cases you would want a type error, and it's also helped by having separate operators for 'addition' and 'concatenation', unlike certain other scripted languages (and even there the type-safe variant doesn't solve any of these issues).

bil til

unread,
Jun 19, 2024, 12:35:57 PM (14 days ago) Jun 19
to lu...@googlegroups.com
Am Mi., 19. Juni 2024 um 16:37 Uhr schrieb 'Sainan' via lua-l
<lu...@googlegroups.com>:
>
> Do you actually need type safety or do you just think you need type safety?

You could also consider to edit Lua programs with MS free of charge
"Visual Studio Code" together with free Sumneko add-on "Lua programmed
by Lua" (do NOT mix up with MSVC, which is the non-free Visual
Compiler of MS).

Then you get quite advanced on-site code checking - this has 3 warning
levels red/ yellow/ blue, and the blue or yellow warnings will
includes checking variable usage against the variable definitions -
typically very useful. (just of course there ARE many Lua functions
like e. g. print, string.format etc. returning or using Lua types with
different types intentionally - in such caess Visual Studio type
checking will somehow "instruct the user" not to use such "type
mangling" in a TOO "offensive" way.

Denis Dos Santos Silva

unread,
Jun 21, 2024, 1:25:43 AM (12 days ago) Jun 21
to lua-l

Kai

unread,
Jun 21, 2024, 7:16:03 AM (12 days ago) Jun 21
to lua-l
Thanks for all the feedback and suggestions.

For my part, I would like to see an official implementation for optional type safety.
Additionally, I would like to use as few third party modules as possible.

A stock Lua with this feature would be really nice, but it's not mandatory.
It could make life in Lua programming a little bit easier.

Sainan

unread,
Jun 21, 2024, 7:53:28 AM (12 days ago) Jun 21
to lu...@googlegroups.com
This won't happen as you haven't even provided any compelling arguments for this bloat.

There are plenty of Lua pre-processors or bytecode-compatible Lua forks to choose from if you really need an Enhanced™ Development Experience®.
Reply all
Reply to author
Forward
0 new messages