2021-08-16 21:38:01 +03:00
|
|
|
# Leo RFC 011: Scalar Type Accesses And Methods
|
2021-08-14 04:21:39 +03:00
|
|
|
|
|
|
|
## Authors
|
|
|
|
|
2021-08-31 19:11:26 +03:00
|
|
|
The Aleo Team.
|
2021-08-14 04:21:39 +03:00
|
|
|
|
|
|
|
## Status
|
|
|
|
|
2021-09-22 20:24:37 +03:00
|
|
|
FINAL
|
2021-08-14 04:21:39 +03:00
|
|
|
|
|
|
|
## Summary
|
|
|
|
|
2021-09-22 20:19:23 +03:00
|
|
|
This RFC proposes three things:
|
2021-08-14 04:21:39 +03:00
|
|
|
|
|
|
|
1. The scalar types in Leo (integers, fields, etc.) can have static methods.
|
|
|
|
2. The scalar types in Leo (integers, fields, etc.) can have static constants.
|
2021-09-27 20:47:59 +03:00
|
|
|
3. Expressions of scalar type can have instance methods called on them.
|
2021-08-14 04:21:39 +03:00
|
|
|
|
|
|
|
## Motivation
|
|
|
|
|
|
|
|
This approach allows for a clean interface to provide built-in methods or static members for these basic types.
|
|
|
|
|
|
|
|
## Design
|
|
|
|
|
|
|
|
### Semantics
|
|
|
|
|
|
|
|
Firstly we would have to modify both the ABNF and parsing of Leo to allow static method calls onto a scalar type.
|
|
|
|
|
|
|
|
The ABNF would look as follows:
|
|
|
|
|
|
|
|
```abnf
|
2021-09-22 20:24:07 +03:00
|
|
|
; This is an existing old rule.
|
2021-08-14 04:21:39 +03:00
|
|
|
scalar-type = boolean-type / arithmetic-type / address-type / character-type
|
|
|
|
|
|
|
|
; Add this rule.
|
2021-09-22 20:24:07 +03:00
|
|
|
named-type = identifier / self-type / scalar-type
|
2021-08-14 04:21:39 +03:00
|
|
|
|
2021-09-22 20:24:07 +03:00
|
|
|
; Modify this rule.
|
2021-08-14 04:21:39 +03:00
|
|
|
postfix-expression = primary-expression
|
|
|
|
/ postfix-expression "." natural
|
|
|
|
/ postfix-expression "." identifier
|
|
|
|
/ identifier function-arguments
|
|
|
|
/ postfix-expression "." identifier function-arguments
|
2021-09-22 20:24:07 +03:00
|
|
|
/ named-type "::" identifier function-arguments ; this used to be identifier-or-self-type
|
|
|
|
/ named-type "::" identifier ; this is new to allow member constants
|
2021-08-14 04:21:39 +03:00
|
|
|
/ postfix-expression "[" expression "]"
|
|
|
|
/ postfix-expression "[" [expression] ".." [expression] "]"
|
|
|
|
```
|
|
|
|
|
2021-08-16 21:38:01 +03:00
|
|
|
Now methods and static members would be first-class citizens of scalar types and their values. For example, the following could be done:
|
2021-08-14 04:21:39 +03:00
|
|
|
|
|
|
|
```ts
|
|
|
|
let x = 1u8.to_bits(); // A method call on on a scalar value itself
|
|
|
|
let x = u8::MAX; // A constant value on the scalar type
|
|
|
|
let y = u8::to_bits(1u8); // A static method on the scalar type
|
|
|
|
```
|
|
|
|
|
|
|
|
## Drawbacks
|
|
|
|
|
|
|
|
This change adds more complexity to the language.
|
|
|
|
|
|
|
|
## Effect on Ecosystem
|
|
|
|
|
|
|
|
None. The new parsing changes would not break any older programs.
|
|
|
|
|
|
|
|
## Alternatives
|
|
|
|
|
|
|
|
None.
|