diff --git a/code-samples/calling-c-addressof.pony b/code-samples/calling-c-addressof.pony new file mode 100644 index 00000000..d7f28aaa --- /dev/null +++ b/code-samples/calling-c-addressof.pony @@ -0,0 +1,4 @@ +use @frexp[F64](value: F64, exponent: Pointer[U32]) +// ... +var exponent: U32 = 0 +var mantissa = @frexp(this, addressof exponent) \ No newline at end of file diff --git a/code-samples/derived-authority-authority-hierarchies.pony b/code-samples/derived-authority-authority-hierarchies.pony new file mode 100644 index 00000000..65975fa4 --- /dev/null +++ b/code-samples/derived-authority-authority-hierarchies.pony @@ -0,0 +1,23 @@ +primitive NetAuth + new create(from: AmbientAuth) => + None + +primitive DNSAuth + new create(from: (AmbientAuth | NetAuth)) => + None + +primitive UDPAuth + new create(from: (AmbientAuth | NetAuth)) => + None + +primitive TCPAuth + new create(from: (AmbientAuth | NetAuth)) => + None + +primitive TCPListenAuth + new create(from: (AmbientAuth | NetAuth | TCPAuth)) => + None + +primitive TCPConnectAuth + new create(from: (AmbientAuth | NetAuth | TCPAuth)) => + None \ No newline at end of file diff --git a/code-samples/recovering-capabilities-format-int.pony b/code-samples/recovering-capabilities-format-int.pony new file mode 100644 index 00000000..8e978a1d --- /dev/null +++ b/code-samples/recovering-capabilities-format-int.pony @@ -0,0 +1,21 @@ +recover + var s = String((prec + 1).max(width.max(31))) + var value = x + + try + if value == 0 then + s.push(table(0)?) + else + while value != 0 do + let index = ((value = value / base) - (value * base)) + s.push(table(index.usize())?) + end + end + end + + _extend_digits(s, prec') + s.append(typestring) + s.append(prestring) + _pad(s, width, align, fill) + s +end \ No newline at end of file diff --git a/code-samples/traits-and-interfaces-structural-subtyping.pony b/code-samples/traits-and-interfaces-structural-subtyping.pony index 51c80835..22b0cb4e 100644 --- a/code-samples/traits-and-interfaces-structural-subtyping.pony +++ b/code-samples/traits-and-interfaces-structural-subtyping.pony @@ -1,19 +1,11 @@ -actor Main - new create(env: Env) => - let divident: U8 = 1 - let divisor: U8 = 0 - let result = div_by_zero(divident, divisor) - match result - | let res: U8 => env.out.print(divident.string() + "/" + divisor.string() + " = " + res.string()) - | let err: ExecveError => env.err.print(divident.string() + " cannot be divided by " + divisor.string()) - end - - fun div_by_zero(divident: U8, divisor: U8): (U8 | ExecveError) => - try divident / divisor - return divident /? divisor - else - return ExecveError - end +interface box Stringable + """ + Things that can be turned into a String. + """ + fun string(): String iso^ + """ + Generate a string representation of this object. + """ primitive ExecveError fun string(): String iso^ => "ExecveError".clone() \ No newline at end of file diff --git a/code-samples/type-aliases-set-is.pony b/code-samples/type-aliases-set-is.pony new file mode 100644 index 00000000..7555b9d2 --- /dev/null +++ b/code-samples/type-aliases-set-is.pony @@ -0,0 +1 @@ +type SetIs[A] is HashSet[A, HashIs[A!]] \ No newline at end of file diff --git a/code-samples/type-expressions-combined.pony b/code-samples/type-expressions-combined.pony new file mode 100644 index 00000000..f700f40d --- /dev/null +++ b/code-samples/type-expressions-combined.pony @@ -0,0 +1 @@ +var _array: Array[((K, V) | _MapEmpty | _MapDeleted)] \ No newline at end of file diff --git a/docs/c-ffi/calling-c.md b/docs/c-ffi/calling-c.md index eccad52f..e2d41f87 100644 --- a/docs/c-ffi/calling-c.md +++ b/docs/c-ffi/calling-c.md @@ -43,11 +43,7 @@ When dealing with `void*` return types from C, it is good practice to try to nar To pass pointers to values to C the `addressof` operator can be used (previously `&`), just like taking an address in C. This is done in the standard library to pass the address of a `U32` to an FFI function that takes a `int*` as an out parameter: ```pony ---8<-- -https://raw.githubusercontent.com/ponylang/ponyc/main/packages/builtin/float.pony:59 -// ... -https://raw.githubusercontent.com/ponylang/ponyc/main/packages/builtin/float.pony:432:424 ---8<-- +--8<-- "calling-c-addressof.pony" ``` ### Get and Pass Pointers to FFI diff --git a/docs/object-capabilities/derived-authority.md b/docs/object-capabilities/derived-authority.md index 1fbad47b..afe2eff4 100644 --- a/docs/object-capabilities/derived-authority.md +++ b/docs/object-capabilities/derived-authority.md @@ -53,13 +53,13 @@ As the package author, it is then our responsibility to realize that the minimal Let's have a look at the authorizations available in the standard library's `net` package. ```pony ---8<-- "https://raw.githubusercontent.com/ponylang/ponyc/main/packages/net/auth.pony" +--8<-- "derived-authority-authority-hierarchies.pony" ``` Look at the constructor for `TCPConnectAuth`: ```pony ---8<-- "https://raw.githubusercontent.com/ponylang/ponyc/main/packages/net/auth.pony:22:22" +--8<-- "derived-authority-authority-hierarchies.pony:22:22" ``` you might notice that this looks like a hierarchy of authorities: diff --git a/docs/reference-capabilities/recovering-capabilities.md b/docs/reference-capabilities/recovering-capabilities.md index 589c24dc..80b09347 100644 --- a/docs/reference-capabilities/recovering-capabilities.md +++ b/docs/reference-capabilities/recovering-capabilities.md @@ -23,7 +23,7 @@ This expression returns an `Array[String] iso`, instead of the usual `Array[Stri Here's a more complicated example from the standard library: ```pony ---8<-- "https://raw.githubusercontent.com/ponylang/ponyc/master/packages/format/_format_int.pony:81:101" +--8<-- "recovering-capabilities-format-int.pony" ``` That's from `format/_FormatInt`. It creates a `String ref`, does a bunch of stuff with it, and finally returns it as a `String iso`. diff --git a/docs/types/traits-and-interfaces.md b/docs/types/traits-and-interfaces.md index 6fdb73ef..26a45f9f 100644 --- a/docs/types/traits-and-interfaces.md +++ b/docs/types/traits-and-interfaces.md @@ -29,11 +29,7 @@ Structural typing is very similar to [duck typing](https://en.wikipedia.org/wiki You do not declare structural relationships ahead of time, instead it is done by checking if a given concrete type can fulfill the required interface. For example, in the code below, we have the interface `Stringable` from the standard library. Anything can be used as a `Stringable` so long as it provides the method `fun string(): String iso^`. In our example below, `ExecveError` provides the `Stringable` interface and can be used anywhere a `Stringable` is needed. Because `Stringable` is a structural type, `ExecveError` doesn't have to declare a relationship to `Stringable`, it simply has that relationship because it has "the same shape". ```pony ---8<-- -https://raw.githubusercontent.com/ponylang/ponyc/main/packages/builtin/stringable.pony - -traits-and-interfaces-structural-subtyping.pony:18:19 ---8<-- +--8<-- "traits-and-interfaces-structural-subtyping.pony" ``` ## Nominal and structural subtyping in Pony diff --git a/docs/types/type-aliases.md b/docs/types/type-aliases.md index 35ee3af1..97bab5a9 100644 --- a/docs/types/type-aliases.md +++ b/docs/types/type-aliases.md @@ -62,7 +62,7 @@ But the use of `type` here is exactly the same as the enumeration example above, Another example, this time from the standard library, is `SetIs`. Here's the actual definition: ```pony ---8<-- "https://raw.githubusercontent.com/ponylang/ponyc/main/packages/collections/set.pony:3:3" +--8<-- "type-aliases-set-is.pony" ``` Again there's something new here. After the name `SetIs` comes the name `A` in square brackets. That's because `SetIs` is a __generic type__. That is, you can give a `SetIs` another type as a parameter, to make specific kinds of set. If you've used Java or C#, this will be pretty familiar. If you've used C++, the equivalent concept is templates, but they work quite differently. diff --git a/docs/types/type-expressions.md b/docs/types/type-expressions.md index 8da2d2fc..39d56dd9 100644 --- a/docs/types/type-expressions.md +++ b/docs/types/type-expressions.md @@ -61,7 +61,7 @@ That's a fairly complex type alias, but let's look at the constraint of `K`. It' Type expressions can be combined into more complex types. Here's another example from the standard library: ```pony ---8<-- "https://raw.githubusercontent.com/ponylang/ponyc/main/packages/collections/map.pony:22" +--8<-- "type-expressions-combined.pony" ``` Here we have an array where each element is either a tuple of `(K, V)` or a `_MapEmpty` or a `_MapDeleted`.