MajeedKazemi / blocks-to-code-old

Other
1 stars 0 forks source link

removing type restrictions #1

Closed MajeedKazemi closed 2 years ago

MajeedKazemi commented 2 years ago

I don't think there is any other rule. Booleans are pretty much the same.

user-74 commented 2 years ago

For some blocks, removing type checking results in code generation issues.

For example: image Results in the error "abc".length.charAt is not a function.

However, the equivalent statement runs in Scratch. The solution they use is to add a type conversion to string if it is expecting a string.

Preferably we handle this kind of issue globally, such as using a try-catch for every variable to convert to int and if not, then convert to string.

But if it is not possible, we will have to manually include type checking/conversion in each code generator code that we remove type checking from.

MajeedKazemi commented 2 years ago

Let's do whatever Scratch does (e.g. using a try-catch for every variable).

Though I think it's more like if the expression/statement requires a string -> then it tries to convert it into a string. But if the expression/statement requires an integer -> then it tries to convert it to an integer.

It's good to start thinking about detecting these issues as they occur (even before the code is executed) so that we could display warning messages and suggestions for fix.

user-74 commented 2 years ago

Solution to the blocks' type checking: Remove 'check' or 'setCheck' from the init.

Solution to maintaining generator type conversion: Attempt to inject a parseFloat/parseInt when necessary. Otherwise keep the error.

E.g. Adding these two functions for math blocks

Blockly.JavaScript.math.convertFloat_ = (value) => { 
  return isNaN(parseFloat(eval(value))) ? value : `parseFloat(${value})`;
};

Blockly.JavaScript.math.convertInt_ = (value) => { 
  return isNaN(parseInt(eval(value))) ? value : `parseInt(${value})`;
};