2021-05-27 23:41:14 +03:00
|
|
|
|
|
|
|
|
|
|
|
```ucm:hide
|
2024-06-12 01:22:09 +03:00
|
|
|
scratch/main> builtins.merge
|
2021-05-27 23:41:14 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
```unison
|
2021-08-24 21:33:27 +03:00
|
|
|
structural ability Exception where raise : Failure -> x
|
2021-06-08 07:01:06 +03:00
|
|
|
|
|
|
|
reraise = cases
|
|
|
|
Left e -> raise e
|
|
|
|
Right a -> a
|
|
|
|
|
2021-08-24 21:33:27 +03:00
|
|
|
structural type Either a b = Left a | Right b
|
2021-06-08 07:01:06 +03:00
|
|
|
|
|
|
|
putBytes h bs = reraise (putBytes.impl h bs)
|
2021-05-27 23:41:14 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-05-28 01:37:55 +03:00
|
|
|
bugFail = cases
|
|
|
|
Failure typ _ _ -> bug (Failure typ "problem" (Any ()))
|
|
|
|
|
2021-05-27 23:41:14 +03:00
|
|
|
Exception.unsafeRun! : '{Exception, g} a -> '{g} a
|
2021-06-08 07:01:06 +03:00
|
|
|
Exception.unsafeRun! e _ =
|
|
|
|
h : Request {Exception} a -> a
|
|
|
|
h = cases
|
|
|
|
{Exception.raise fail -> _ } ->
|
2021-05-28 01:37:55 +03:00
|
|
|
bugFail fail
|
2021-06-08 07:01:06 +03:00
|
|
|
{a} -> a
|
|
|
|
handle !e with h
|
|
|
|
|
|
|
|
socketSend s bytes = reraise (socketSend.impl s bytes)
|
|
|
|
closeSocket s = reraise (closeSocket.impl s)
|
2024-04-02 09:11:34 +03:00
|
|
|
serverSocket host port = reraise (IO.serverSocket.impl host port)
|
2021-05-27 23:41:14 +03:00
|
|
|
|
|
|
|
hello : Text -> Text -> {IO, Exception} ()
|
2021-06-08 07:01:06 +03:00
|
|
|
hello host port =
|
2021-05-27 23:41:14 +03:00
|
|
|
socket = serverSocket (Some host) port
|
2021-06-08 07:01:06 +03:00
|
|
|
msg = toUtf8 "Hello there"
|
2021-05-27 23:41:14 +03:00
|
|
|
socketSend socket msg
|
2021-06-08 07:01:06 +03:00
|
|
|
closeSocket socket
|
2021-05-27 23:41:14 +03:00
|
|
|
|
|
|
|
myServer = unsafeRun! '(hello "127.0.0.1" "0")
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
```ucm:error
|
2024-06-12 01:22:09 +03:00
|
|
|
scratch/main> run myServer
|
2021-05-27 23:41:14 +03:00
|
|
|
```
|