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
Add a subsection under the top level definition in style guides to specify constant declaration best practices #8608
Comments
Now that we support the type descriptor in the
Consider the below code. const string REPO_NAME = "balleirna-lang";
const REPO_NAME = "balleirna-lang";
var REPO_NAME = "balleirna-lang";
string REPO_NAME = "balleirna-lang"; // We prefer this becuase of point 2.
In the following case based on 1 and 2 points, I think it is better to explicitly declare the type. // Having the explicit type here would make sure
// the user can use this constant in a place where a byte is expected
// without having much thought.
const byte FOO_VAL = 7 * 25 + 79; |
According to the spec, Hence type descriptor is only useful for cases such as
when we have to decide the type of literals. Also spec says, IMO, expected type is only required for when we have to modify the default behaviour such as mentioned before. It is better to not use the expected type and it will reduce the unnecessary type checking as well. |
Even const decimal VAL_1 = 1; we can write as const VAL_1 = 1d; Given that using the type-descriptor isn't used as the inherent type, I also prefer omitting the type, unless it's actually required for construction.
What the type-descriptor would say is that the constant's type is a subtype of that type, rather than the exact type. So, I'm not sure these make a significant difference. In fact, it may even make it clearer to the user that the type of the constant is a singleton.
IMO, using var result = getResult(); where it's not obvious without looking at Unless we need something from the type-descriptor (and maybe some complicated expressions), I think we can recommend defining the constant without the type. |
Thanks for the input. |
// don't
const map<int> MAX_DIMENSION_SIZES = {width: 1920, height: 1080};
const string[] DAYS_OF_WEEK = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
// do
const MAX_DIMENSION_SIZES = {width: 1920, height: 1080};
const DAYS_OF_WEEK = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; I was writing the above samples. However, for the first one, if we use a record say, record {| int width; int height;|} Then it would be easy to access the width and height from the constant neh.
In that case, I prefer to have the record as the type descriptor depends on the usecase. WDYT? @MaryamZi @prakanth97 // don't
const map<int> MAX_DIMENSION_SIZES = {width: 1920, height: 1080};
// do
const MAX_DIMENSION_SIZES = {width: 1920, height: 1080};
// or
const record {| int width; int height;|} MAX_DIMENSION_SIZES = {width: 1920, height: 1080}; |
Description
In the style guides page under the top level definitions section we don't have a section cover the best practice when declaring constant values. Even in the general practices subsection we have constant declaration without using the best practice.
In the spec it is optional to add the type-descriptor when declaring constant values. When the type-descriptor is not present the type checker will resolve the expressions singleton type and add it as the constant type. Also when we don't have the type descriptor present in the constant declaration we don't have to do a additional type checking.
The only place we need to add the type-descriptor is the cases where we need to explicitly cast as a literal value. I think we can mention that.
eg:
Related website/documentation area
Describe the problem(s)
Describe your solution(s)
Related issue(s) (optional)
Suggested label(s) (optional)
Suggested assignee(s) (optional)
The text was updated successfully, but these errors were encountered: