Skip to content

Commit

Permalink
Some updates from review
Browse files Browse the repository at this point in the history
  • Loading branch information
chalcolith committed Apr 25, 2023
1 parent 54a5a49 commit 99fe597
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions text/0000-static-tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The Pony compiler already does this for string literals; they are deduplicated a

# Detailed design

A static table can be included in a Pony program by prefixing its opening square bracket with a `#` charater. This usage is analogous to the use of `#` to denote compile-time expressions in [Luke Cheeseman's dependent type proposal](https://github.com/lukecheeseman/ponyta). For example, in the following code:
A static table can be included in a Pony program by prefixing its opening square bracket with a `#` charater. This borrows from `#` to denote compile-time expressions (e.g. `#(1+2)`) in [Luke Cheeseman's dependent type proposal](https://github.com/lukecheeseman/ponyta), which we intend to implement someday. For example, in the following code:

```pony
let table: Array[U32] val =
Expand All @@ -25,9 +25,13 @@ A static table can be included in a Pony program by prefixing its opening square
]
```

The array's internal pointer will point to a static table. The value of a static array literal can only be `Array[T] val`, where `T` is a floating-point or integral numeric type.
The array's internal pointer will point to a static table.

Ideally, the `Array` object itself will also be a static global object, so as to avoid constructing a new object every time we need to access the table, for instance if we're trying to use a table via a method on a primitive.
The value of a static array literal can only be `Array[T] val`, where `T` is a floating-point or integral numeric type (that is, `T` must be one of { `F32`, `F64`, `ISize`, `ILong`, `I8`, `I16`, `I32`, `I64`, `I128`, `USize`, `ULong`, `U8`, `U16`, `U32`, `U64`, `U128` }).

If you tried to do `let foo: Array[U32] ref = #[1;2]` the compiler would say "right side must be a subtype of left side; `Array[U32 val] val` is not a subtype of `Array[U32 val] ref^`".

The `Array` object itself will also be a static global object, so as to avoid constructing a new object every time we need to access the table, for instance if we're trying to use a table via a method on a primitive.

# How We Teach This

Expand All @@ -50,11 +54,11 @@ This change introduces a bit of new syntax which might be confusing to existing

# Alternatives

Instead of introducing new syntax, we could introduce a new type name, e.g. `StaticTable[T]` (with only `val` constructors) to which an array literal could be assigned; such array literals would be stored as static data, e.g. `let table: StaticTable[U32] = [ 123; 456 ]`.
- Instead of introducing new syntax, we could introduce a new type name, e.g. `StaticTable[T]` (with only `val` constructors) to which an array literal could be assigned; such array literals would be stored as static data, e.g. `let table: StaticTable[U32] = [ 123; 456 ]`.

We could modify the compiler to automatically store array literals assigned to `Array[T] val` variables as static data. However this might be confusing to programmers as to what circumstances would trigger this magical behaviour.
- We could modify the compiler to automatically store array literals assigned to `Array[T] val` variables as static data. However this might be confusing to programmers as to what circumstances would result in a static array (stored at compile-time) vs the current array literal behaviour which constructs arrays at runtime by calling `add()` repeatedly.

If this RFC is not implemented, code that needs fast access to static data can currently use C FFI.
- If this RFC is not implemented, code that needs fast access to static data can currently use C FFI.

# Unresolved questions

Expand Down

0 comments on commit 99fe597

Please sign in to comment.