unison/unison-src/transcripts/fix2027.md
2024-06-25 11:11:07 -07:00

1.2 KiB

scratch/main> builtins.merge
structural ability Exception where raise : Failure -> x

reraise = cases
  Left e -> raise e
  Right a -> a

structural type Either a b = Left a | Right b

putBytes h bs = reraise (putBytes.impl h bs)

toException : Either Failure a ->{Exception} a
toException = cases
  Left e -> raise e
  Right a -> a

putText : Handle -> Text ->{IO, Exception} ()
putText h t = putBytes h (toUtf8 t)

bugFail = cases
  Failure typ _ _ -> bug (Failure typ "problem" (Any ()))

Exception.unsafeRun! : '{Exception, g} a -> '{g} a
Exception.unsafeRun! e _ =
    h : Request {Exception} a -> a
    h = cases
        {Exception.raise fail -> _ } ->
            bugFail fail
        {a} -> a
    handle !e with h

socketSend s bytes = reraise (socketSend.impl s bytes)
closeSocket s = reraise (closeSocket.impl s)
serverSocket host port = reraise (IO.serverSocket.impl host port)

hello : Text -> Text -> {IO, Exception} ()
hello host port =
    socket = serverSocket (Some host) port
    msg = toUtf8 "Hello there"
    socketSend socket msg
    closeSocket socket

myServer = unsafeRun! '(hello "127.0.0.1" "0")

scratch/main> run myServer