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

Add more context to wildcard type (*) explanation #6805

Closed
wants to merge 6 commits into from

Conversation

ElrohirGT
Copy link

Fixes #5038

This PR adds a little bit more context to the wildcard type as discussed in the zullip chat.

It also adds the roc-cli package to the devShell in the flake.nix. This was done because the www/build-dev-local.sh bash script needs roc to run.

@ElrohirGT ElrohirGT changed the title Add more context to wildcard type explanation * Add more context to wildcard type (*) explanation Jun 12, 2024
Comment on lines 41 to 46
{
name = "roc-lang-unofficial";
publisher = "IvanDemchenko";
version = "1.2.0";
sha256 = "sha256-lMN6GlUM20ptg1c6fNp8jwSzlCzE1U0ugRyhRLYGPGE=";
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Were these spaces removed by a formatter?

Copy link
Author

Choose a reason for hiding this comment

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

Yep! I ran:

nix fmt

And it formatted flake.nix and a file under de tools directory.

flake.nix Outdated
Comment on lines 134 to 136
packages = [
(self.outputs.packages.${system}.cli)
];
Copy link
Collaborator

Choose a reason for hiding this comment

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

@Anton-4 can you please advise on this, I"m not a nix user.

@lukewilliamboswell
Copy link
Collaborator

I think the wording addition looks good. Just not 100% on the nix addition.

@ElrohirGT
Copy link
Author

This PR no longer modifies the flake.nix file, only the tutorial.md file

@Anton-4 Anton-4 self-assigned this Jun 12, 2024
Signed-off-by: Anton-4 <[email protected]>
@Anton-4 Anton-4 requested a review from rtfeldman June 12, 2024 18:25
@Anton-4
Copy link
Collaborator

Anton-4 commented Jun 12, 2024

@ElrohirGT it"s hard to explain this really well, so I tried to make some improvements and asked Richard for help to perfect this.


- The function could only return `[]`. Remember, the wildcard type means **any** type, so our function `List * -> List *` **must** return a list that can contain **any** type. The only specific list that would fit the description is the empty list.
- Note the function would not only accept `[]` as argument, but any list!
Function arguments are allowed to be broad/general but the values you return must be specific.
Copy link
Collaborator

Choose a reason for hiding this comment

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

@rtfeldman I feel like this last sentence is important but I"m not sure this is the best way to put it. I"m also not sure why precisely this is the case. It seems like we could arbitrarily allow (in type checking) to return e.g ["abc"] here as well, and it"s not obvious to me what problems that would create.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems * has caused a good bit of confusion throughout the years. Are we sure we want to keep it? Especially because it is just syntactic suger.

Copy link
Author

Choose a reason for hiding this comment

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

From what I can understand returning [ "abc" ] would make the "real" function signature List * -> List Str.

A List * as a return type would mean a List that can contain any type, since the moment you add an element you make that List a List (type of element you added) the only List capable of holding * typed items would be an empty one.

For the input type the interpretation changes to a List of elements of some type. So for the input, you don"t really care which type of elements you have on the List.

Maybe it"s a little confusing having the wording of "any type" and suddenly you have flashbacks as to how generics work in other languages.

Copy link

@lishaduck lishaduck Jun 12, 2024

Choose a reason for hiding this comment

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

I"m also not sure why precisely this is the case. It seems like we could arbitrarily allow (in type checking) to return e.g ["abc"] here as well, and it"s not obvious to me what problems that would create.

I recall reading about this, I think it was in C# In Depth. Here"s Wikipedia on invariance instead, given that I don"t expect anyone to having a copy sitting around: [[Covariance and contravariance (computer science)]]1

If you read it: good for for you. If not: the list might be appended to, but if both the caller and receiver can choose what it is, they might choose differently and that"s a runtime error becuase lists can only contain one type (otherwise, you"d need unsoundness in the form of dynamic + casting or type unions. Either way is bad for performance, becuase you can"t allocate it up front if I understand correctly). Additionally, roc monomorphizes, so it picks the type at compile time, but it can"t do that if you"ve picked a type for it. The star is saying that it can fulfill whatever the caller wants, not the receiver.

Footnotes

  1. Yes, that was for OOP languages, but it still applies. There"s no subtyping, but imagine a type of Object (*) with a "subtype" of Str (Do abilities allow emulating hierarchies? You might be able to get into this situation.). Additionally, while this is an issue for "mutable state," Lists can "change," it"s just hidden from the user, particularly with opportunistic mutation.

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems * has caused a good bit of confusion throughout the years. Are we sure we want to keep it? Especially because it is just syntactic suger.

It"s a reasonable question. Can you start a thread in #ideas? I"d like to hear what different people think!

Copy link

@lishaduck lishaduck Jun 13, 2024

Choose a reason for hiding this comment

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

It seems * has caused a good bit of confusion throughout the years. Are we sure we want to keep it? Especially because it is just syntactic suger.

It"s a reasonable question. Can you start a thread in #ideas? I"d like to hear what different people think!

As someone not on Zulip, I"ll just give my 2¢: It"s confusing. It makes sense, but it takes time to figure out. The second-to-last time I tried to use Roc, I gave up because the interplay between * and type aliases were annoying. It makes sense, just annoying. The learning curve (and the prohibition on them in type aliases) wouldn"t be there if they were gone 🤷‍♂️

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for sharing!

I know it"s a separate topic, but what was annoying about type aliases?

This comment was marked as off-topic.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link

Thank you for your contribution! Sometimes PRs end up staying open for a long time without activity, which can make the list of open PRs get long and time-consuming to review. To keep things manageable for reviewers, this bot automatically closes PRs that haven’t had activity in 60 days. This PR hasn’t had activity in 30 days, so it will be automatically closed if there is no more activity in the next 30 days. Keep in mind that PRs marked Closed are not deleted, so no matter what, the PR will still be right here in the repo. You can always access it and reopen it anytime you like!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add syntax sugar for * to Tutorial
5 participants