-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: Go 2: introduce "grant" keyword to set access to variables and functions #31632
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
Comments
The grant keyword can PRECEDE the definition of the items (both who gets the rights and to what) but granted items that are not declared at the end of the same source code should be seen and faulty grant statements. an idea (but this might be harder to implement)
|
I am not the person qualified to know if this is a good idea or not. But once we would go this route we can't go back if it turns out to be to verbose or more chore for other users to work on other projects. One of the strong points of Go is it allows you to quickly poke around a code base. Somehow I believe this proposal sounds good on paper but reality will shoot us in the foot because it prevent us to quickly print or modify some internals for debugging we are not suppose to have access on. At the moment I think Go has a few more important problems like generics that we need to iron out first before considering this. |
The grant statement was part of a proprietary RAD scripting language called Formula for several years.
Agreed but postponing this due to low priority tells me that you are not grasping the current lack of functionality. GOlang is steadily evolving from a RAD to full-fledged all purpose commercial application language. While you have now the luxure to play around with these features, that luxure will be GONE in a few years from now! Once that everybody started deploying commercial applications you will not be so keen as to change anything at all (you'll see). The grant statement is also not adding something that is not needed because it does not only tell the compiler what the restrictions are but it also works as DOCUMENTATION in telling the reader of the program source what the intended usage of the data structure or function or package is. |
But isn't it going to be confusing if you use capital letters to export it and then use grant to not allow it usage? So for me it sounds that for grant to be consistent then the current capital export rules need to go out of the language. |
How is that? |
If it can't be used on function parameters, this proposal doesn't solve one of the most important issues that immutability or read-only types fix, which is guaranteeing at compile-time that a function does not modify the contents of a map or slice it's passed (or, with immutability, that they're also not changed under its feet by another goroutine). And if it can, how would it work syntactically? Presumably it would have to go inside the function body, since the parameters are only in scope there. But then you can't see if a parameter is read-only or immutable by just looking at a signature (in godoc for example). Also, since a |
Correct, I did not originally opt for restricting access to function arguments as it is not clear WHO you give the rights to. However, considering your argument, I conclude that indeed this is not an issue as the WHO is in this case always the function itself. But there is no use case for this as function arguments are either already a local copy (e.g. int,string,bool) or a data structure (in which case you should have done the grant behind the "type MyType struct {...}"
Correct, it is not an alternative for #22048 because I did not take into account what happens if you make a map or list with restricted access public. However making a data item public makes it public (point final, I would not change that behavior). |
There might be no such type declaration. Consider a typical Write method: Which, in turn, would make it impossible to create a version of |
This reminds me of "programming by contract" as available in ADA . I think that in stead of having a specific |
Sorry but there is no correlation to the use case you describe and my GRANT proposal. |
However there is NOTHING that prevents a programmer from directly doing
except when you could do:
I admit that it adds more labor but that is also why it is optional and you would only grant access to variables of which wrong usage would cause havoc (e.g. increment/decrement counters via functions with locking) |
by my opinion, it looks too complex and not match with the benefits it can bring. if changing a variable is a must, and it is not granted with access, then I will choose to change the grant definition, instead of follow the grant restriction. developers in dedicated project are smart enough to know how and when to access things, so no need to put a stone there. |
One of the main features that people want from this kind of access controls is to be able to say "this function does not modify any memory using this pointer." This proposal doesn't seem to address that use case. In general access to a package scope variable can be controlled by making it unexported and providing exported accessor functions, or, perhaps, use an internal package. It's sometimes useful to be able to restrict access to variables across packages. It's rarely useful to restrict access within a single package. If package gets so large that access is unclear, it should likely be broken up anyhow. As you say |
About: read-only, private, public, protected, const
Proposal: introduce Grant keyword
This would fixes all these issues #27975 #20443 #22876 #22048 without introducing compatibility issues of any kind. These issues are currently all open and requiring decision that alter the behavior of existing code while my proposal solves all these issues without any impact on existing code.
Introduction to "grant".
the keyword "grant" comes from the SQL language where it is used to assign privilege to use a particular item in a particular way to a particular user or group of users.
In established programming languages (C++, C#, JAVA) this functionality is accomplished by using keywords like:
However these keywords introduce complexity that we do NOT WANT in GO.
So my proposal is to introduce an optional keyword that does the same but BETTER, FASTER, STRICTER and more ACCURATE and more READABLE.
The new grant keyword is only used during compile time and does not generate any run-time code.
In SQL we have privileges like INSERT, UPDATE, DELETE, CREATE, DROP, ...
In GO we have privileges GET, SET, USE
In SQL we have users and groups of users (because SQL database requires login-to-database with user credentials) so grants are made to a user.
In GO we do not know who the programmer or user is so the grant is made BETWEEN items (where an item is a type or method or variable or package)
Because "grant" is optional the default is that all privileges are granted between all items (not changing the local/public flag set by a name starting with lower-case or upper-case rune).
The effect of the grant keyword is restricted to the package in which it occurs.
Multiple grants (e.g. in different sources about the same item) are cumulative.
There is no "revoke" (like in SQL) that could undo a grant. To revoke a right you simply change the source code line that does the Grant.
Examples
situation 1: I have a structure with a counter that everybody may read but only a single function should be allowed to increment or decrement.
situation 2: I want to limit all access to function Aaa() to only function Bbbb() (undoing the default grant all to all)
situation 3: I have a variable that needs initialization but only the function that initializes it may write to it (everywhere else all members of the struct are immutable)
situation 4: I have dot-imported a package but want to restrict the use of this "type wildcard" to a few functions
The text was updated successfully, but these errors were encountered: