Skip to content

[SUGGESTION] Static: Keyword with many meanings #322

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

Closed
AbhinavK00 opened this issue Apr 5, 2023 · 12 comments
Closed

[SUGGESTION] Static: Keyword with many meanings #322

AbhinavK00 opened this issue Apr 5, 2023 · 12 comments

Comments

@AbhinavK00
Copy link

AbhinavK00 commented Apr 5, 2023

In cpp, the keyword static is currently used at three different places:

  1. To declare objects with static lifetime
  2. To specify global variables with internal linkage
  3. To declare static members (data and functions) in classes

All of these 3 use cases are very different from one-another and therefore, using same keyword for all of them can be confusing, especially to beginners. Consider the following code:

static int a = 9;

auto foo (char t ){
    static float g = 42.0f;

}

The static in above declarations does different things (yes I know the variables behave kinda same but static does different things to them).

Therefore, I propose to divide the static keyword into 3 parts to address each use case differently.

  1. The first use case is the only one with radical change. Instead of using keyword static, we could agree on some different keyword that captures intent clearly in an unambigious way.
//cpp1 code

static int a = 0;

//cpp2 code
SOMETHING a := 0;     //not sure on position of keyword 

I think something like global can be used but put down your suggestions.

  1. This use case is already covered by anonymous namespaces in cpp and I think that's how it should be in cpp2. So no change here. Anonymous namespaces are a bit more verbose so if anyone has better idea, do suggest.

  2. This use case is partially already covered in cpp2. Static member functions are easily and distinguishably declared in cpp2 without the use of static keyword. So I'll be addressing the data member part only. The simple way would be to have a keyword. The real decision to make is whether it would be same as keyword chosen for 1) or a different one. A discussion on this topic would probably be needed so this currently is left unaddressed.

Note: I'd like to expand a bit on 1) to cover global variables too. They also have static lifetime. We can make it mandatory for global variables to be declared with some keyword (same as that would be decided for 1). Herb has plans to make global variables constexpr by default and is not sure if he'll even allow them or not. Having a keyword will capture attention of programmer and also be consistent with other static lifetime variables that would be declared.

These changes will make different things look different and do away with one small source of confusion to new programmers. This is a very small change so shouldn't be hard to implement.

Alternatives
We could also come up with different syntax for case 3) and leave case 1) as it is. That would be one option.

@JohelEGP
Copy link
Contributor

JohelEGP commented Apr 5, 2023

  1. is already
x : type = {
  f: () = { } // No `this` parameter, `static` member function.
};

@AbhinavK00
Copy link
Author

AbhinavK00 commented Apr 5, 2023

Ohh, is there a way for static data members too?

@JohelEGP
Copy link
Contributor

JohelEGP commented Apr 5, 2023

Seems like that's not the case.

@msadeqhe
Copy link

msadeqhe commented Apr 5, 2023

@AbhinavK00, so we have the following cases for static keyword in C++1:

1. Local variables with static life-time

I think this case isn't safe, because the life-time of a local variable will be extended without any obvious tracking.

2. Global variables with internal linkage

Anonymous namespaces are good enough as you have suggested:

_: namespace = {
    variable := value;
}

or simply:

_::variable := value;

3. Static function members

Yes, we already have it. This is the example from @JohelEGP:

X: type = {
    func: () = { }
}

4. Static data members

Using a keyword for this case seems natural as you have suggested:

X: type = {
    KEYWORD variable := value;
}

Alternatively the following syntax can be used to eliminate KEYWORD like other cases don't have any additional keyword:

X: type = {
 // X.variable := value;
    X::variable := value;
}

or this one:

X: type = {
 // type.variable := value;
    type::variable := value;
}

In my opinion, type::variable is better than X::variable, because the type name doesn't have to be replicated multiple times.

@AbhinavK00
Copy link
Author

AbhinavK00 commented Apr 5, 2023

I think this would conflict with how we declare members outside of a class. The type::variable syntax would not, but it's close enough to be confused

@msadeqhe
Copy link

msadeqhe commented Apr 5, 2023

I think this would conflict with how we declare members outside of a class.

Would you provide an example? I don't know how it will conflict.

@AbhinavK00
Copy link
Author

Ah sorry, my bad. You're right, there would be no conflict.

@msadeqhe
Copy link

msadeqhe commented Apr 5, 2023

No problem 😉

@leejy12
Copy link

leejy12 commented Apr 8, 2023

  1. Static data members
    Using a keyword for this case seems natural as you have suggested:
X: type = {
   KEYWORD variable := value;
}

shared could be a possible candidate, as in this variable is "shared" by all instances of this type.

@orent
Copy link

orent commented Apr 11, 2023

So which of these uses do you actually refer to as “static” in everyday speech? I know that I refer to 1 and 3 as “static” and 2 as “non exported symbol” or something like that. Use 2 is also the one that has a good alternative that highlights the fact that it is about symbol visibility rather than lifetime.

No new keyword please. The keyword should match the terminology and the terminology should match that of C++ because Cpp2 shares the same underlying object model. Don’t fork the terminology.

@AbhinavK00
Copy link
Author

Closing now as not high priority, may reopen later.

@AbhinavK00
Copy link
Author

Another suggestion for use-case 2) would be to make global variables have internal linkage by default. As globals constants and globals constant expressions have internal linkage by default, it would be wise to do the same with global variables and have a keyword to specify external linkage instead.

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

No branches or pull requests

5 participants