~ryanford/blog.ryanford.dev

ef93b742403a48ed9fcacb7bee981bf5fccc8f4d — Ryan Ford 1 year, 10 months ago 4b377fa
add object capability example
1 files changed, 18 insertions(+), 0 deletions(-)

M src/posts/pony-tldr.md
M src/posts/pony-tldr.md => src/posts/pony-tldr.md +18 -0
@@ 94,6 94,24 @@ Basically, at start up, your `Main` program is bestowed certain rights. Like rig

As an additional parallel, you can imagine Android's permission model. Apps need to require specific permission to do certain actions on your device. If a library requests more capabilities than you feel like it needs, you're encouraged to *not use that library*.

Here's an example from an http framework, [Jennet](https://github.com/Theodus/jennet#serving-static-files):

```
actor Main
    new create(env: Env) =>
        let tcplauth: TCPListenAuth = TCPListenAuth(env.root)
        let fileauth: FileAuth = FileAuth(env.root)

        let server =
            Jennet(tcplauth, env.out)
                .> serve_file(fileauth, "/", "index.html")
                .serve(ServerConfig(where port' = "8080"))

        if server is None then env.out.print("bad routes!") end
```

To serve static files, Jennet requests access to the TCPListenAuth and the FileAuth. If it didn't have them, it wouldn't be able to perform those functions.

## More safety

In Pony, arithmetic comes in many flavors. By default, Pony looks out for overflow/underflow and division by zero at a small cost to runtime performance. You can optionally use modified operators i.e. `+~` to do "unsafe arithmetic". You can increase performance at the cost of safety (and you void your crash free warrantee). There are 2 more variants available - checked and partial arithmetic. In the partial variant - written `+?`, Pony raises an error on overflow/underflow and division by zero. Not this is an "error" not a crash/exception. If you're program can raise an error, the compiler will tell you that you **must** handle it or it will fail to compile. The last variant - checked - is used in method for like `addc()` returns a tuple on which the second argument is `true` if there is overflow/underflow or division by zero. On top of this, Pony allows operator overloading by redefining an objects default arithmetic methods.