Skip to content

Commit

Permalink
Revert "Use stdlib examples directly from ponylang/ponyc repository"
Browse files Browse the repository at this point in the history
This reverts commit 0f8e225.
  • Loading branch information
shaedrich committed Jun 4, 2024
1 parent 51dca91 commit f3c5552
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 31 deletions.
4 changes: 4 additions & 0 deletions code-samples/calling-c-addressof.pony
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use @frexp[F64](value: F64, exponent: Pointer[U32])
// ...
var exponent: U32 = 0
var mantissa = @frexp(this, addressof exponent)
23 changes: 23 additions & 0 deletions code-samples/derived-authority-authority-hierarchies.pony
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions code-samples/recovering-capabilities-format-int.pony
Original file line number Diff line number Diff line change
@@ -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
24 changes: 8 additions & 16 deletions code-samples/traits-and-interfaces-structural-subtyping.pony
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions code-samples/type-aliases-set-is.pony
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type SetIs[A] is HashSet[A, HashIs[A!]]
1 change: 1 addition & 0 deletions code-samples/type-expressions-combined.pony
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var _array: Array[((K, V) | _MapEmpty | _MapDeleted)]
6 changes: 1 addition & 5 deletions docs/c-ffi/calling-c.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docs/object-capabilities/derived-authority.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion docs/reference-capabilities/recovering-capabilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
6 changes: 1 addition & 5 deletions docs/types/traits-and-interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/types/type-aliases.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/types/type-expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down

0 comments on commit f3c5552

Please sign in to comment.