Stream: beginners

Topic: Creating new phantom types


view this post on Zulip Henrik Larsson (Mar 14 2025 at 10:44):

I am trying to do some work with phantom types and am struggling to really understand how to think. Given I have something like:

Session state := [Session {stream: Str}]
Offline :  [Offline]
Online : [Online]
new_session : Username, Password -> Session Offline
new_session = |_, _| Session({stream: "test"})
connect!: Session Offline => Session Online

Now new_session will not type check, I need to also include the Offline but there is no way to do this. I must be missing some fundamental mental model about this. How would you impl new_session and connect! here to make it type check?

view this post on Zulip Anton (Mar 14 2025 at 13:06):

This works :)

Session line := {stream: Str}

Offline : [Offline]
Online : [Online]

new_session : Username, Password -> Session Offline
new_session = |_, _| @Session {stream: "test"}

connect!: Session Offline => Session Online

Username : Str
Password : Str

Last updated: Jul 05 2025 at 12:14 UTC