mirror of
https://github.com/unisonweb/unison.git
synced 2024-09-11 02:25:39 +03:00
56 lines
1.2 KiB
Markdown
56 lines
1.2 KiB
Markdown
|
|
|
|
```ucm:hide
|
|
scratch/main> builtins.merge
|
|
```
|
|
|
|
```unison
|
|
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")
|
|
|
|
```
|
|
|
|
```ucm:error
|
|
scratch/main> run myServer
|
|
```
|