Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Friendly error when using multiple spreads #4265

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

carlbordum
Copy link

This was unexpected to me, so figured other new gleamers could trip over it as well

(and an excuse to play with the compiler :))

Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a snapshot test for the error please so we can see what it looks like 🙏

ParseErrorType::ListSpreadWithAnotherSpread => (
"I wasn't expecting a spread here",
vec![
"Hint: use list.append(list1, list2) to join two lists.".into(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we want to recommend this, it's normally the wrong thing to do

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I guess it is likely that the problem is modeled poorly. Perhaps we can add that as a comment, or suggest a common "pattern" to model such problems better.

On the other hand, you could argue that it is still helpful for newcomers. You have to type [..xs, ..ys] to get the recommendation, so I think it is pretty clearly what you are trying to do.

What do you think?

Copy link
Member

@lpil lpil Feb 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We generally don't want to people to append lists, so helping them to do it would be counterproductive.

@carlbordum carlbordum force-pushed the friendly-multi-spread branch from c7898c6 to 1eb6000 Compare February 21, 2025 22:14
@carlbordum
Copy link
Author

Could you add a snapshot test for the error please so we can see what it looks like 🙏

I didn't know about snapshot testing - that's cool! I've added the test.

Comment on lines +16 to +20
┌─ /src/parse/error.gleam:5:13
5 │ [..xs, 4, ..ys]
│ ^^ I wasn't expecting a spread here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit confusing to me, the spread there is technically correct, the problem is there's a prior spread that shouldn't have been there. Maybe it could be pointing to the one that is not last and highlight how there can only be one at the end of the list?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the error at anything that follows the first spread would be fine and possibly easier to implement. The error can say that the spread must be the last thing in the list.

Comment on lines +16 to +20
┌─ /src/parse/error.gleam:5:13
5 │ [..xs, 4, ..ys]
│ ^^ I wasn't expecting a spread here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the error at anything that follows the first spread would be fine and possibly easier to implement. The error can say that the spread must be the last thing in the list.

location: SrcSpan { start: 39, end: 41 },
}
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the two unit tests please 🙏

@lpil
Copy link
Member

lpil commented Feb 22, 2025

Thank you!

@lpil lpil marked this pull request as draft February 22, 2025 21:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants