Stream: beginners

Topic: Example of creating an inspect ability?


view this post on Zulip Ian McLerran (May 10 2024 at 15:24):

Can anyone point me to an example of where an inspect ability has been implemented for an opaque type?

view this post on Zulip Luke Boswell (May 10 2024 at 16:04):

https://github.com/roc-lang/roc/blob/main/examples/inspect-logging.roc

https://github.com/roc-lang/roc/blob/main/examples/Community.roc

view this post on Zulip Luke Boswell (May 10 2024 at 16:05):

Or are you looking for an InspectFormatter implementation, like https://github.com/roc-lang/roc/blob/main/examples/GuiFormatter.roc?

view this post on Zulip Ian McLerran (May 10 2024 at 17:10):

I probably should have posted this in the Http.send causes exits program early on 400 code thread for better context.

I don't know enough to ask my question effectively, but basically I want to implement a type which may vary its inspect behavior depending on certain conditions. IE:

ResponseBody := List U8 implements [Inspect]

# made up inspect function - no idea what this might look like in reality:
inspect : ResponseBody -> f where f implements InspectFormatter
inspect = \responseBody ->
    when responseBody |> List.takeFirst 50 |> Str.fromUtf8 is
        Ok str -> # inspect returns a string
        Err _ -> # inspect returns the first 50 bytes

view this post on Zulip Ian McLerran (May 10 2024 at 17:20):

I'm thinking InspectFormatter is what I need to figure out here...

view this post on Zulip Anton (May 10 2024 at 17:23):

This example may help as well.

view this post on Zulip Anton (May 10 2024 at 17:27):

Sidenote; you'll need to wrap what you return in a Tag if you want to return multiple types, e.g. [TagStr Str, TagBytes (List U8)]

view this post on Zulip Ian McLerran (May 12 2024 at 03:58):

So i'm playing around with some code trying to get a custom Inspect ability for my opaque. I've written a toInspector function, but the language server tells me that I have an illegal specialization for a non-opaque/structural type. I'm confused because my type is definitely defined as an opaque:

ResponseBody := List U8 implements [Inspect {toInspector: toInspector}]

The error message says its specialized for a List U8. My toInspector code is as follows:

toInspector = \respBody ->
    when respBody |> List.takeFirst 50 |> Str.fromUtf8 is
        Ok strBody -> Inspect.inspect (String strBody)
        Err _ -> Inspect.inspect (Bytes respBody)

view this post on Zulip Brendan Hansknecht (May 12 2024 at 06:32):

Need to unwrap the opaque

view this post on Zulip Brendan Hansknecht (May 12 2024 at 06:32):

You are using it as a List U8 directly in that function

view this post on Zulip Brendan Hansknecht (May 12 2024 at 06:33):

\@ResponseBody respBody ->

view this post on Zulip Ian McLerran (May 12 2024 at 06:33):

Ahh, of course! 🤦🏻‍♂️

view this post on Zulip Ian McLerran (May 12 2024 at 19:29):

Anyone know what I should make of this error message?

thread '<unnamed>' panicked at crates/compiler/solve/src/specialize.rs:866:25:
lambda set region not resolved: (`42.IdentId(11)`, MemberSpecializationInfo { _phase: PhantomData<roc_can::abilities::Resolved>, symbol: `42.IdentId(11)`, specialization_lambda_sets: VecMap { keys: [1], values: [124] } })
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

crates/compiler/solve/src/specialize.rs:866

Not sure what it means by a lambda set 'region' here. It clearly is an issue with my specialization of Inspect/toInspector, but beyond that, I'm lost.

The code panics in the function find_opaque_specialization_ambient_function, after matching opt_specialization to Some(member_impl) (not None), and matching member_impl to MemberImpl::Impl(spec_symbol)not MemberImpl::Error.

So it seems that there has been some success in matching my specialization to an ability in the ability store, but when trying to get the "specialized lambda set", something goes wrong.

view this post on Zulip Brendan Hansknecht (May 12 2024 at 19:31):

These are the scary errors where I generally comment out code til it works and then slowly work myself back. Maybe try a few variants of similar code.

view this post on Zulip Ian McLerran (May 12 2024 at 19:35):

Oh boy lol. Duly note... :+1:


Last updated: Jul 05 2025 at 12:14 UTC