Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

final as new keyword #700

Open
fperrad opened this issue Sep 20, 2023 · 7 comments
Open

final as new keyword #700

fperrad opened this issue Sep 20, 2023 · 7 comments
Labels
feature request New feature or request

Comments

@fperrad
Copy link
Contributor

fperrad commented Sep 20, 2023

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

@lenscas
Copy link
Contributor

lenscas commented Sep 20, 2023

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

@fperrad
Copy link
Contributor Author

fperrad commented Sep 20, 2023

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).

@hishamhm hishamhm added the feature request New feature or request label Sep 20, 2023
@hishamhm
Copy link
Member

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.

@fperrad
Copy link
Contributor Author

fperrad commented Sep 21, 2023

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

@jordan4ibanez
Copy link

jordan4ibanez commented Nov 2, 2023

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

@hishamhm hishamhm changed the title [PROPOSAL] final as new keyword final as new keyword Nov 13, 2023
@Zipperdoodle
Copy link

For my two cents I think it'd be a great value add if Teal could have some way of enforcing immutability of compound types when so desired, in whatever way seems most appropriate. Even if only by declaring a reference variable as immutable. In other words an object can only be mutated through a mutable reference. And mutable reference can be assigned to an immutable reference, but not the other way around (except perhaps by an explicit cast but then at least you know it's not an accident). Something like that. Simple but effective. It's just nice to know when the compiler can catch unintended mutations when there shouldn't be any.

@Andre-LA
Copy link

Andre-LA commented Sep 2, 2024

Just my feedback:

I don't think adding custom syntax just to be C-like is a good idea, and being honest, I did found the <const> and <close> syntax ugly when I saw on lua-l the first time, but after using it for years on Nelua (which also adds others annotations like <comptime> and multiple annotations like <cimport, nodecl>) I find it natural to use nowadays.

Anyway, I'm just saying this because I don't believe adding syntax sugar to get a C-like syntax worth the complexity and maintenance cost to Teal, in my opinion.

I do however find interesting the idea of adding it's own annotations or expand it's capabilities, like <const> fields or arguments as an example.

For an example (with a small module) of the tedious syntax of annotation, see

I can see fperrad point though, this is currently possible:

local function add(x: integer, y: integer): integer
    return x + y
end

add = assert -- oops!

print(add(5, 10))

And this is only way to avoid such thing I suppose, which doesn't seems elegant:

local add <const> = function(x: integer, y: integer): integer
    return x + y
end

add = assert -- error: test.tl:5:1: cannot assign to <const> variable

print(add(5, 10))

If we re-use Nelua syntax here (which the syntax for annotations is a bit different IIRC), it could be:

local function add(x: integer, y: integer): integer <const>

maybe this could be a possible option?

Another possibility is making functions <const> by default and then introducing a new annotation to allow overwriting them (like <mutable>), anyway, I'm just throwing ideas here, I hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants