Stream: compiler development

Topic: auto inspect on type variables


view this post on Zulip Brendan Hansknecht (Jan 21 2024 at 18:15):

From #beginners > Type variables implement two abilities?, can we make it so that you never have to specify where a implements Inspect.Inspect.

Make this no longer an error:

» x : a -> Str
… x = \v -> Inspect.toStr v

── TYPE MISMATCH ───────────────────────────────────────────────────────────────

This 1st argument to toStr has an unexpected type:

5│      x = \v -> Inspect.toStr v
                                ^

This v value is a:

    a

But toStr needs its 1st argument to be:

    val where val implements Inspect

All types always implement Inspect, so it should never need to be specified.

view this post on Zulip Brendan Hansknecht (Jan 21 2024 at 18:16):

I was thinking this probably could be special handling for Inspect.Inspect.

view this post on Zulip Brendan Hansknecht (Jan 21 2024 at 18:16):

Richard commented that there might be a smarter way to do this:

yeah I think if we expose Inspect.inspect as * -> f where f implements InspectFormatter then Inspect.toStr can be implemented in terms of that and we're good

view this post on Zulip Brendan Hansknecht (Jan 21 2024 at 18:18):

@Ayaz Hafiz any thoughts on this?

view this post on Zulip Ayaz Hafiz (Jan 21 2024 at 18:19):

Tbh I wouldn't do this

view this post on Zulip Ayaz Hafiz (Jan 21 2024 at 18:20):

you could probably special case it but I'm pretty sure it would complicate the implementation

view this post on Zulip Ayaz Hafiz (Jan 21 2024 at 18:20):

how often is this case going to come up? I think it's likely not that frequent. Adding a suggestion to the error message to add the ability constraint is probably enough.

view this post on Zulip Ayaz Hafiz (Jan 21 2024 at 18:21):

* -> f where f implements InspectFormatter would not work without punching a hole in the type system

view this post on Zulip Brendan Hansknecht (Jan 21 2024 at 18:23):

you could probably special case it but I'm pretty sure it would complicate the implementation

Wouldn't be as simple as saying all type variable always implement inspect? Like just appending it to a list?

view this post on Zulip Ayaz Hafiz (Jan 21 2024 at 19:39):

yes, but that's not a trivial thing. Every type starts off as a variable, so you would have to have the list for every variable.

view this post on Zulip Brendan Hansknecht (Jan 21 2024 at 20:12):

Make sense. Then nvm.

view this post on Zulip Brendan Hansknecht (Jan 21 2024 at 20:13):

A nice to have, but definitely not worth any sort of complexity cause it is trivial for an end user to fix and is only hit with type variables


Last updated: Jul 06 2025 at 12:14 UTC