Stream: beginners

Topic: ✔ How to unwrap opaque type


view this post on Zulip Jonas Schell (Mar 03 2024 at 20:46):

I've been playing around with opaque types and was wondering how to unwrap them properly if I don't do it at the beginning of the function as in the tutorial example?

toStr : Username -> Str
toStr = \@Username str ->
    str

I realised that I could unpack it with when ... is like this:

toStr : Username -> Str
toStr = \username ->
  when username is
    @Username str -> str

But intuitively I was trying to unwrap it the same way as in the tutorial example also for non argument unwarpps. So my question is, am I doing it right or is there another way to get the value inside an opaque type?

view this post on Zulip Richard Feldman (Mar 03 2024 at 23:37):

that's the way to do it! Basically it's always through destructuring like that

view this post on Zulip Richard Feldman (Mar 03 2024 at 23:38):

either is fine, and you can also do:

@Username str = username

view this post on Zulip Jonas Schell (Mar 04 2024 at 22:06):

Thanks for your answer! Your example only works if you unwrap the argument directly, right?
I'm not sure if my example was clear, but from reading the docs I thought this would also be possible.

unwrappLater : Username -> Str
unwrappLater = \username ->
    @Username username

But it does not work. It makes sense that it doesn't work, but I just wanted to share my experience and my intuition that my brain thought it could work.

view this post on Zulip Richard Feldman (Mar 04 2024 at 22:32):

ah gotcha! Yeah that's trying to wrap it a second time :big_smile:

view this post on Zulip Brendan Hansknecht (Mar 04 2024 at 22:33):

Interesting. That would actually be the reverse wrap function

view this post on Zulip Notification Bot (Mar 05 2024 at 22:34):

Jonas Schell has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC