shortercode/Radiance

Feature: interface

Opened this issue · 0 comments

Description

An interface describes an abstruct set of functions that a struct can implement. This allows implementations of that interface to be used in many places. With the restriction that only those functions described in the interface can be used.

struct Message {
  text: string
}

interface Display {
  print -> string
}

impl Display for Message {
  print -> string {
    self.text
  }
}

import fn log_string (txt: string)

fn print (obj: Display) {
  log_string(obj.print())
}

fn main {
  let msg = Message {
    text: "hello world"
  }
  print (msg)
}

When used as that interface the struct reference is converted to an object that represents the implementation of the interface. This conversation can only occur 1 way, as the new object does not contain the type information required to reverse the operation.

It is not possible to define members for an interface, only methods. This circumvents the requirement for virtual member lookups. Nor is it possible to directly access the members of the struct, as it is not possible to ensure this is type safe.

With the addition of interfaces we should also define some common interface types; such as the "Display" interface describes above which allows for the conversion of the struct into a string. Another key one would be "Iterable" which would enable the addition of 'for...of' loop syntax.

There are several additions that could be added on top of this.

  • default function implementations which can be used by all implementors.
  • extension of other interfaces.