[Feature] Introduce parts of Object Oriented Programming paradigm
- Dominant language
- Rust
- Stars
- 5.2k
- Forks
- 145
- Avg merge
- 5d 3h
- Merged PRs (30d)
- 7
Description
This is a parent issue for the proposals of key OOP language elements:
- **Structures** - Statically typed objects containing a fixed set of values. Can contain other non-primitive values. Structures can contain methods. Does not support inheritance or virtual methods.
- **Dictionaries** - Hash map-like data structures that contain a dynamic set of key-value pairs.
- **Enums** - A Rust-like value enumerator that can hold values just like unions do in C-like languages but also can be a traditional symbolic enumerator representing a value.
# The Community Proposals
Amber's community came up with different ideas on implementing missing pieces in the realm of OOP paradigm. Below are listed some discussions and issues worth considering.
| Feature | Issue | Description |
|--|--|--|
| Structure | #583 | Regular structures |
| Structure | #80 | Class-like syntax approach |
| Dictionaries | #66 | Regular dictionary |
| Dictionaries | #683 | A JSON-like approach |
---
# Proposed Implementation
The following section displays the language syntax proposal for implementing each of the key features based on the community feedback and Discord discussions.
## Structure
- Structure definition
```ts
struct Person {
name: Text
age: Int
fun speak() {
echo("Hello, I'm {this.name}")
}
}
```
- Structure initialization
```ts
const john = Person {
name: "John",
age: 31
}
```
- Structure access (get / set)
```ts
echo(john.name)
john.name = "Johnny"
```
- Structure method call
```ts
john.speak()
```
Keyword `this` represents current structure and cannot be used outside of structure methods.
## Dictionary
- Dictionary initialization
```ts
const color_palette = {
primary: "#1F2A44",
accent: "#C76D45",
background: "#F7F3EA",
"other-color": "#FF4567"
}
```
- Empty Dictionary initialization
```ts
const domain_map = {Text: Text}
```
- Dictionary access (get / set)
```ts
use_color(color_palette.primary)
use_color(color_palette["accent"])
color_palette.primary = "#2F3B55"
```
- Dictionary iteration
```ts
for key, value in color_palette {
echo("The {key} color is {value}")
}
```
## Enum
- Enum definition
```ts
enum Result {
Ok,
Err(Text)
}
```
- Enum initialization
```ts
const result = Result.Err("Cannot move out of map borders")
```
- Enum access (get / set)
```ts
if result is Result.Err {
echo(result) // Shows contained Text message in Result.Err
}
echo(result) // Shows a debug enum variant such as `Result.Err`
result = Result.Ok
```
# Things requiring further discussion
- Type signature for Dictionaries
- Dictionary initialization is very similar to structures.
- Implementation of `match` block although `if` can be enough.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.