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

Remove declaration keywords #3985

Closed
adamchalmers opened this issue Sep 25, 2024 · 3 comments · Fixed by #4063
Closed

Remove declaration keywords #3985

adamchalmers opened this issue Sep 25, 2024 · 3 comments · Fixed by #4063

Comments

@adamchalmers
Copy link
Collaborator

adamchalmers commented Sep 25, 2024

Many languages, like Rust, JavaScript, Java, etc have a variable declaration keyword, maybe let or const or var or something else (like the variable's type).

Not all languages have a declaration keyword! Python and Matlab both declare variables via x = 3 with no preceding let or var or const keywords.

Currently KCL supports let, const and var (they all do the same thing for various historical reasons). According to @jgomez720 the Python-style x = 3 is a more natural syntax for him, as a mechanical engineer. He prefers it to let x = 3 or const x = 3.

So, KCL should probably imitate Python, and remove the variable declaration keywords.

Justification

If KCL had mutation, it'd be important to have an explicit declaration keyword. That way users could tell when a variable comes into being and when it is/is not in scope or is redefined. Without mutation, that isn't as big of a problem for KCL.

Implementation

We want to avoid breaking existing code that uses let or const if possible. Here's a plan for that:

  • Update the AST node for VariableDeclaration to make the keyword (let/const/var) optional.
  • Update the parser to allow users to skip the variable declaration keyword
  • Update the recaster (which is also used as the code formatter) to remove the keyword.

This way, old code (let x = 3) will be accepted and run just fine, but be converted to x = 3 when executed.

@adamchalmers
Copy link
Collaborator Author

Replaces #439

@yeroca
Copy link

yeroca commented Sep 30, 2024

Some folks may wonder why they keyword disappears when auto-formatting their code. Do you have a method of highlighting code to show a warning? If so you can call the keywords out as deprecated.

@jtran
Copy link
Collaborator

jtran commented Oct 3, 2024

Sorry for the trouble this may have caused. In the future, we'll try to do things like have auto-migration and lint auto-fix. But it's a lot of work. We're still in the pre-1.0 phase where we're making breaking changes.

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

Successfully merging a pull request may close this issue.

3 participants