Skip to content

[Feature] Introduce parts of Object Oriented Programming paradigm #1135

Description

@Ph0enixKM

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
    struct Person {
      name: Text
      age: Int
    
      fun speak() {
        echo("Hello, I'm {this.name}")
      }
    }
  • Structure initialization
    const john = Person {
      name: "John",
      age: 31
    }
  • Structure access (get / set)
    echo(john.name)
    john.name = "Johnny"
  • Structure method call
    john.speak()

Keyword this represents current structure and cannot be used outside of structure methods.

Dictionary

  • Dictionary initialization
    const color_palette = {
      primary: "#1F2A44",
      accent: "#C76D45",
      background: "#F7F3EA",
      "other-color": "#FF4567"
    }
  • Empty Dictionary initialization
    const domain_map = {Text: Text}
  • Dictionary access (get / set)
    use_color(color_palette.primary)
    use_color(color_palette["accent"])
    color_palette.primary = "#2F3B55"
  • Dictionary iteration
    for key, value in color_palette {
      echo("The {key} color is {value}")
    }

Enum

  • Enum definition
    enum Result {
      Ok,
      Err(Text)
    }
  • Enum initialization
    const result = Result.Err("Cannot move out of map borders")
  • Enum access (get / set)
    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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions