KittyCAD / modeling-app

The KittyCAD modeling app.
https://kittycad.io/modeling-app/download
MIT License
411 stars 35 forks source link

KCL: % is not an operator #4041

Open adamchalmers opened 1 month ago

adamchalmers commented 1 month ago

In many languages % is the modulo operator, but not in KCL! In KCL, % is used as a substitution in a pipeline (|>).

Trying to use % as an operator should be a parse error, or at the very least, a runtime error of some kind.

Unfortunately right now it's silently accepted, and for example this program:

let n = 10
let d = n % 2

evaluates d to 0.0 -- very bad.

I've reproduced this bug in a PR (https://github.com/KittyCAD/modeling-app/pull/4042) but haven't yet fixed it.

jtran commented 4 days ago

I think it's fine to remove % as an operator. #4227 tries to change it to infix mod. Copying what I said from there:

My opinion is that having infix mod is inconsistent with the rest of the language. The way we divide and the way we do modulus should go together. There are different flavors, and as long as they match, it makes sense. Division as we've defined it matches with remainder. We have rem() for that. I think we should make mod() be a stdlib function similar to rem(), but that's implemented with Rust's rem_euclid.