-
Notifications
You must be signed in to change notification settings - Fork 259
[SUGGESTION] Static members in types #522
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
Sorry, but I fail to understand the above example. Shouldn't the member be inaccessible as it is declared private? |
Thanks for the hint. I wanted to spell it public. |
Adapted the initial example. |
This issue is related to the topic.
The current plan for
Also syntactically it would be possible to declare static data members without any new keyword: tt: type = {
public type::foo: int = 0;
// or
public tt::foo: int = 0;
// or
public _::foo: int = 0;
// or
public ::foo: int = 0;
} By the way, a keyword may improve its readability. |
Cpp1 already let's you access static members with I'm more concerned with the Cpp2 spelling of static data members. Static data members is something that will be needed by the |
I also think, that a keyword is more readable. If I had to choose from one of the four options. I think the fourth one would be my choice. But I would always prefer a keyword here.
That is correct but it is not possible without an actual instantiation of the type. https://compiler-explorer.com/z/Ks7eY1qKo |
That's OK. |
That is what I also understood. I would propose to make the distinction in cpp2 the following:
|
|
At namespace scope, static global variables are inside unnamed namespace with internal linkage: _::variable: i32;
main: () = {
/// statements
} At local scope, static local variables are devil. But if the plan is to support them, it's better for them to be visible in function declarations (no hidden stuff): /// function: (_::variable: i32 = 10) = {
function: (static variable: i32 = 10) = {
/// statements
}
main: () = {
function();
} They can be declared at the end of function parameter list, and they must have default value, and we cannot pass static arguments to the function. |
Sounds brilliant if that statement is always true.
I don't disagree with "it's better for them to be visible in function declarations". Regardless, if they're to be declared upfront, FWIW, this formulation prohibits local statics of local type. |
An alternative for locals is to overload
|
I should reply to this: Cpp2 does already support 'static member functions' as type-scope functions that don't have But for static member data, that's global data, and I've been deliberately dragging my heels on all of:
because they're globals, and mutable globals are something worth discouraging -- but they are sometimes useful, so my thought had been to require them to be hidden behind functions (e.g., Meyers singletons). For now I'm allowing global data, but may require it to be End of quick brain dump! |
Ok, so I will close this issue. |
Then I wonder if we can get Cpp1 |
Interesting idea, thanks. |
This is more of a question.
static members are currently not supported in cpp2. Is this due to the lack of time or some other considerations?
I would try to implement it as another modifier like e.g.
public
so thatshould work. I think
t.foo += 1;
would be better but this would be a quite large syntactic change in contrast to cpp1.The text was updated successfully, but these errors were encountered: