Kipper-Lang / Kipper

The Kipper programming language for Browsers and Node.js 🦊✨ Made at HTL Leonding & JKU Linz
https://kipper-lang.org
GNU General Public License v3.0
26 stars 4 forks source link

[Feature] Implement syntax for objects and user-defined interfaces #284

Closed Luna-Klatzer closed 4 months ago

Luna-Klatzer commented 2 years ago

Is there an existing proposal for this?

This feature does not exist in the latest version

Proposal

Implement TypeScript-like objects and interfaces, which should allow the user definition of objects and the creation of custom Kipper object types (interfaces). These types though should unlike in TypeScript be also available at runtime time in form of an object of type type.

This object should store the relevant metadata of the interface type and be defined once during the creation of the interface.

Interfaces though should primarily be used to define blueprints for objects during compile-time type checking, where each field of an interface must be implemented in an object to meet the interface type criteria e.g. be effectively of that type. These interfaces can then be used as types on variables or arguments as the basic blueprint, where only objects matching the interface can be stored.

For example:

interface Blueprint {
  field: num;
}

// Valid
var obj1: Blueprint = { field: 1; };

// Invalid - Does not meet requirements of interface
var obj2: Blueprint = { };
var obj3: Blueprint = { x: "1"; }

// Invalid - Type 'str' does not meet type 'num' of field 'field'
var obj4: Blueprint = { field: "2"; }

Here though interfaces will not be fully strict and use duck-typing like in TypeScript to check whether an object matches an interface. This means additional fields not specified by the interface can be present in the object, while still correctly matching the type criteria of the interface.

For example:

// Valid
var obj5: Blueprint = { field: 1, anotherField: 2, ... };

Exact behaviour / changes you want

Luna-Klatzer commented 4 months ago

This issue is too broad and complex, and as such will be split up into multiple issues to simplify the implementation process.

Superseding issues: