KittyCAD/modeling-app

Remove declaration keywords

Closed this issue · 3 comments

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.

Replaces #439

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.

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.