You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The discussion in #59 got me thinking a lot about improving the readability of our codegen.
null comparisons
One codegen that is extremely non-readable is this:
let x = 1
print(x != null)
This codegens to the following:
var x: int = 1
print(((typeof(x) != typeof(null)) or ((typeof(x) == typeof(null)) and (x != null))))
Ouch! We could just codegen
print(x != null)
⚠️⚠️⚠️ NOTE ⚠️⚠️⚠️
The reason we cant do this all the time because print(self != 5) is a RUNTIME ERROR: you can't compare Node2D to 5. This is why I codegen such disastrous code. However, we can compare null to anything without doing a typeof check, so we could special case that for readability.
Similar type comparisons that aren't unions
Another simple comparison that codegens terribly is this:
let x = 5
print(x == 5)
This codegens to:
var x: int = 5
print(((typeof(x) == typeof(5)) and (x == 5)))
Again, ouch.
The reason we codegen like this is because == has the same issue and will throw a runtime error if we compare two values of the wrong type. I currently just always emit the typeof() checks because it produces correct code 100% of the time. However, if we check the types on both sides of the equals sign, and we see they:
Are the same
They aren't union types
Or they are union types, but the union is only with null/undefined
Then we could emit the simpler case.
I think this would go a long way towards improving readability.
The text was updated successfully, but these errors were encountered:
The discussion in #59 got me thinking a lot about improving the readability of our codegen.
null comparisons
One codegen that is extremely non-readable is this:
This codegens to the following:
Ouch! We could just codegen
The reason we cant do this all the time because
print(self != 5)
is a RUNTIME ERROR: you can't compare Node2D to 5. This is why I codegen such disastrous code. However, we can compare null to anything without doing a typeof check, so we could special case that for readability.Similar type comparisons that aren't unions
Another simple comparison that codegens terribly is this:
This codegens to:
Again, ouch.
The reason we codegen like this is because == has the same issue and will throw a runtime error if we compare two values of the wrong type. I currently just always emit the typeof() checks because it produces correct code 100% of the time. However, if we check the types on both sides of the equals sign, and we see they:
Then we could emit the simpler case.
I think this would go a long way towards improving readability.
The text was updated successfully, but these errors were encountered: