3 ms·
Just tried it online¹ to see how it deals with a long standing problem² I have with Lua and I'm pleased to see actual improvement in the form of a warning:
by srhtftw 3y ago
Just tried it online¹ to see how it deals with a long standing problem² I have with Lua and I'm pleased to see actual improvement in the form of a warning:
script.pluto:1: warning: function was hinted to return string
but actually returns nil [type-mismatch]
1 | function foo(x: number): string
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ here
Unfortunately the underlying table was corrupted when the code ran and only
1 foo
was generated in the output. I would have preferred it to have refused to run at all (will need to look in the docs to see if there's a way to get that behavior with configuration). The purist in me wants more but the practical side of me appreciates that this is definitely an improvement over current Lua.
¹: https://pluto-lang.org/web/#code=function%20foo(x%3A%20number)%3A%20string%20%0A%20%20if%20x%20%3D%3D%202%20then%20return%20nil%20else%20return%20%22foo%22%20end%0Aend%0A%0Ax%20%3D%20%7B%20foo(1)%2C%20foo(2)%2C%20foo(3)%20%7D%0A%0Afor%20i%2Cv%20in%20ipairs(x)%20do%0A%20print(i%2Cv)%0Aend https://pluto-lang.org/web/#code=function%20foo(x%3A%20numbe...
²: https://news.ycombinator.com/item?id=38030778 https://news.ycombinator.com/item?id=38030778
- djur 3y agoIt looks like you can set any warning to cause an error with a pragma: https://pluto-lang.org/docs/New%20Features/Compiler%20Warnings#changing-warning-states https://pluto-lang.org/docs/New%20Features/Compiler%20Warnin...
- naniwaduni 3y agoIt's not "corruption" when you don't understand the semantics of the functions you're calling—ipairs stops at the smallest integer index that doesn't have a value. You can still find that the key 3 exists with pairs; you can't find the key 2, because it's not in the table. The language just doesn't distinguish "key present with no value" in a table, which is frankly a bizarre thing to want with typechecking in the same breath.
- srhtftw 3y agoYou make a good point: since "pairs" returns all the values as it should, calling the table "corrupted" may be too harsh. It's been so long now I can't recall why the original code I found this problem in used "ipairs" instead of "pairs". It's simply not safe to use "ipairs" on a table containing a nil and the code that did that was wrong. I think working with newer languages like Typescript have made me unsatisfied with languages like Lua which provide no clear way to prevent this sort of mistake, so I like that dialects like Pluto are showing ways Lua could be improved.