Confusion when importing type and its constructor #5574
Replies: 6 comments 1 reply
-
|
Can you think of something that the error message could say that would help you understand better in this situation?
Which documentation did you study prior to this point? Our introduction does carefully explain this, so it's useful to understand how you got here.
We definitely do not want to make the syntax ambiguous, it makes it harder to understand. We did previously have a system like this but it was removed, and that change made the language better. |
Beta Was this translation helpful? Give feedback.
-
|
I read the language tour and writing gleam and didn't see examples of importing both a type and its constructor, e.g: There is one example in the tour showing an import of both a type and what I assume are type constructors, but the distinction between type and constructor isn't mentioned in the text. There's also an example of a record type where the constructor has the same name as the type, but the fact that they're in separate namespaces (and therefore must be imported separately) is not mentioned. An example showing how to import a record and its constructor would be helpful.
I think record constructors are ambiguous, because the docs say:
...and yet a record constructor is not the same thing as a type (or at least, it must be imported separately from its type). |
Beta Was this translation helpful? Give feedback.
-
It's challenging to document things not about Gleam, because that list is infinite in size! I'm trying to find examples of languages where types and values are the same, to better understand where that idea would come from, and Idris is the only one I have managed to find. Why is it that you would expect them to be the same? What languages are you most familiar with?
You linked to one example, and the "unqualified imports" and "type imports" pages explain the system.
They are not ambiguous: type imports are prefixed with the keyword |
Beta Was this translation helpful? Give feedback.
-
|
I feel like we're not understanding each other. Here's a made-up example that would have eliminated my confusion:
The last line that imports both a record type AND its constructor with the same name is absent from the docs. I assumed |
Beta Was this translation helpful? Give feedback.
-
|
I definitely ran into this particular mental model confusion when I started Gleam too. I'm trying to figure out where it would feel best to provide guidance: docs or compiler message. Docs are safer b/c they only effect people who specifically seek them out and are not imposed on all users, but compiler messages are more likely to reach a new user encountering this issue. You could imagine the message could be altered to only when the compiler sees that a constructor of the same name was exported from the same file that the type was defined in. I realize that might be a lot of work for a small benefit to only a few users but I could imagine that being a helpful reminder for even a veteran Gleam user. 🤷♂️ |
Beta Was this translation helpful? Give feedback.
-
I understand what you are saying, but without the information I am asking you for it's difficult for me to understand the context. The tour is very carefully constructed, we cannot make changes without understanding the needs, even if you personally are certain it is an improvement.
There is a work in progress PR for this from last year: #4615 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm new to Gleam and had some code like this:
As the file was growing larger I decided to factor the Context type into its own file, so now there's two files:
context.gleam
main.gleam
This fails to compile with this error:
Ok, I guess I can import it without the
typekeyword, likeimport context.{Context}, but that results inerror: Unknown type.Through trial and error I eventually discovered how to resolve the issue via
import context.{Context, type Context}. So this solved the problem for me. But I assumed thatimport context.{type Context}would be sufficient, so the error message was confusing to me.Now I have updated my mental model of how modules work in Gleam to something like "there are separate namespaces for types and type constructors, even though in the source code they are syntactically similar".
Perhaps this needs more explanation in the intro documentation, or maybe the compiler could "do what I mean". I don't feel strongly either way since now I know how to avoid this issue, but I wanted to make an Issue for it since it may trip up other beginners.
Beta Was this translation helpful? Give feedback.
All reactions