Stream: compiler development

Topic: wildcard type error


view this post on Zulip Anton (Dec 11 2023 at 12:11):

Given that we say the wildcard type is compatible with any type, it's kind of strange you get a TYPE MISMATCH error in a case like this:

── TYPE MISMATCH ───────────────────────────────────── examples/helloWorld.roc ─

Something is off with the body of the lst definition:

7│      lst : List *
8│      lst = [1]
              ^^^

The body is a list of type:

    List Num *

But the type annotation on lst says it should be:

    List *

Tip: The type annotation uses the type variable * to say that this
definition can produce any type of value. But in the body I see that
it will only produce a Num value of a single specific type. Maybe
change the type annotation to be more specific? Maybe change the code
to be more general?

Should we make the error something like "type must be specific"?

view this post on Zulip Folkert de Vries (Dec 11 2023 at 12:29):

no, we always believe the type signature over the actual body

view this post on Zulip Richard Feldman (Dec 11 2023 at 12:56):

yeah I think the error message has all the correct information. Maybe the wording could be better? :thinking:

view this post on Zulip Anton (Dec 11 2023 at 13:26):

no, we always believe the type signature over the actual body

Oh yeah, that's no problem. I meant that it's a bit strange that we call it a mismatch. If we say * is compatible with any type, we should not say the types don't match. Because the actual problem is that the type is not specific enough. It seems reasonable to make a separate error category for the "not specific enough" case. It's very different to e.g. a mismatch where you are using aStr type instead of U64.

view this post on Zulip Folkert de Vries (Dec 11 2023 at 16:40):

but the problem here is that the implementation is too specific? it picks a narrower type than the signature says it is

view this post on Zulip Anton (Dec 11 2023 at 16:49):

but the problem here is that the implementation is too specific

Yeah, you can say "the provided implementation is too specific" or "the provided type signature is not specific enough"

view this post on Zulip Richard Feldman (Dec 11 2023 at 17:13):

maybe something like this?

── BODY DOES NOT MATCH ANNOTATION ─────────────────── examples/helloWorld.roc ─

The body of the lst definition does not match its type annotation:

7│      lst : List *
8│      lst = [1]
              ^^^

The body is a list of type:

    List (Num *)

This is a narrower type than what the annotation on lst says it should be:

    List *

Tip: The type annotation uses the type variable * to say that this
definition can produce any type of value. But in the body I see that
it will only produce a Num value of a single specific type. Maybe
change the type annotation to be more specific? Maybe change the code
to be more general?

view this post on Zulip Anton (Dec 11 2023 at 17:18):

Looks good, I would add some color or other emphasis to "narrower type" and "Maybe change the type annotation to be more specific? Maybe change the code to be more general?"

view this post on Zulip Anton (Dec 11 2023 at 17:21):

Perhaps also use "more specific" instead of "narrower" for international English speakers.

view this post on Zulip Brendan Hansknecht (Dec 11 2023 at 17:22):

This feels like a case where giving a counter example would be useful. Though not sure if that can be easily added to the error message rather than as a page on the website with multiple examples

view this post on Zulip Anton (Dec 11 2023 at 17:24):

I think it's a good idea in general to add a link to the website to error messages for an in depth explanation where useful.


Last updated: Jul 06 2025 at 12:14 UTC