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

Can we add private properties to modules by using the __ before the variable or function. Bonus adding @ for annotations. #1656

Closed
HarmosCreations opened this issue Feb 12, 2025 · 2 comments
Labels
enhancement New feature or request

Comments

@HarmosCreations
Copy link

HarmosCreations commented Feb 12, 2025

I have seen this in python i think where the private property is recognized by using (__) before the variable or function.

module.__index = module

we already use this often so it makes sense for this to be a official way to hide properties and only make them visible inside the module and hide it from the outside.

im well aware that with the wrong name like __add will also act like a meta method but for that you could reserve those names or just let them be i don't see any problem since most people using modules already aware of metatables and advanced at their job.

Or making static properties like module.new is a static property when __index used it will ignore the static properties and private and only use the normal members.

we could use like @Native @Private or @static or __index for private and @static there are many ways this can be achieved.

Bonus request:

im very greatful for the option to comment out functions and i know it is still a early development thing so i want to suggest to make it like lua annotation since they both are similar and makes great for documenting modules and keeping the clients happy.

link: #https://keplerproject.github.io/luadoc/manual.html
another link: #https://github.com/LuaLS/lua-language-server/wiki/Annotations

@HarmosCreations HarmosCreations added the enhancement New feature or request label Feb 12, 2025
@nothing1649
Copy link

what difference does this make to defining a local variable in the module?

local module = {}

local privateVal = 127

function module.setVal(n:number): number
    privateVal = n
end

function module.getVal(): number
    return privateVal
end

return module

@HarmosCreations
Copy link
Author

local module = {}

local privateVal = 127

function module.setVal(n:number): number
privateVal = n
end

function module.getVal(): number
return privateVal
end

return module -- this is not Object orientated programming so for this case the current luau is sufficient.

look at it in an oop style then you will see many problems real quick like making a whole new table for public methods and static now another one for private and so on.

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

No branches or pull requests

2 participants