mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-12-23 10:12:21 +03:00
[RFC] Add exmaples to type alias RFCs.
As suggested by Jon.
This commit is contained in:
parent
4919b2cc02
commit
4a8b857b27
@ -42,7 +42,7 @@ With the array types of unspecified size proposed in RFC 006,
|
|||||||
## Syntax
|
## Syntax
|
||||||
|
|
||||||
The ABNF grammar changes as follows:
|
The ABNF grammar changes as follows:
|
||||||
```
|
```ts
|
||||||
; modified rule:
|
; modified rule:
|
||||||
keyword = ...
|
keyword = ...
|
||||||
/ %s"true"
|
/ %s"true"
|
||||||
@ -92,6 +92,35 @@ In any case, it seems beneficial to expand type aliases away
|
|||||||
(whether during canonicalization or as part or preamble to type checking and inference)
|
(whether during canonicalization or as part or preamble to type checking and inference)
|
||||||
prior to performing more processing of the program for eventual compilation to R1CS.
|
prior to performing more processing of the program for eventual compilation to R1CS.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
The aforementioned 3x3 matrix example could be written as follows:
|
||||||
|
```ts
|
||||||
|
type matrix = [u32; (3, 3)];
|
||||||
|
|
||||||
|
function matrix_multiply(x: matrix, y: matrix) -> matrix {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The aforementioned balance example could be written as follows:
|
||||||
|
```ts
|
||||||
|
type balance = u64;
|
||||||
|
|
||||||
|
function f(...) -> (..., balance, ...) {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The aforementioned string example could be written as follows:
|
||||||
|
```ts
|
||||||
|
type string = [char; _];
|
||||||
|
|
||||||
|
function f(str: string) -> ... {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
# Drawbacks
|
# Drawbacks
|
||||||
|
|
||||||
As other extensions of the language, this makes things inherently a bit more complicated.
|
As other extensions of the language, this makes things inherently a bit more complicated.
|
||||||
@ -103,17 +132,17 @@ None; this is just a convenience for the Leo developer.
|
|||||||
# Alternatives
|
# Alternatives
|
||||||
|
|
||||||
An alternative to creating a type alias
|
An alternative to creating a type alias
|
||||||
```
|
```ts
|
||||||
type T = U;
|
type T = U;
|
||||||
```
|
```
|
||||||
is to create a circuit type
|
is to create a circuit type
|
||||||
```
|
```ts
|
||||||
circuit T { get: U }
|
circuit T { get: U }
|
||||||
```
|
```
|
||||||
that contains a single member variable.
|
that contains a single member variable.
|
||||||
|
|
||||||
This is clearly not equivalent to a type alias, because it involves conversions between `T` and `U`
|
This is clearly not equivalent to a type alias, because it involves conversions between `T` and `U`
|
||||||
```
|
```ts
|
||||||
T { get: u } // convert u:U to T
|
T { get: u } // convert u:U to T
|
||||||
t.get // convert t:T to U
|
t.get // convert t:T to U
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user