Warn if someone attempts to the the array index syntax of C family languages #5123
Replies: 2 comments 2 replies
-
|
Hello! Thanks for the report.
You have! Gleam doesn't have semicolons or significant whitespace, so there is no difference between the two code examples you have given there. In whitespace insensitive languages (which is most languages!) you can place any amount of whitespace anywhere in the program and there will be no difference in behaviour. This makes adding a warning very challenging. For example, this is a valid program that return import gleam/io
pub fn run() -> List(Int) {
some_function()[0]
}The standard way that the non-FP programmer is alerted to this problem in their code is through use of the auto-formatter, which will format the code in a way that makes it clear. This is built into the language server so will typically run when files are saved. Are you looking to not use the formatter? Do you have other ideas for how we could help people understand that indexing isn't a thing in FP? |
Beta Was this translation helpful? Give feedback.
-
|
Hi! Thanks for the explanation, that makes sense. I understand now that adding a compiler warning for I think a good way to help newcomers would be to add a note in the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The standard operator for retrieving an element from a list
(list[index])is not supported in Gleam. Using it produces confusing behavior for new developers:Output:
Effectively, the code behaves as if it were written like this:
Instead of giving a clear warning about unsupported list indexing, Gleam generates a warning about an "unused literal," which is misleading. I believe there should be a more informative warning or hint indicating that list indexing with [] is not supported.
Beta Was this translation helpful? Give feedback.
All reactions