🧱Interfaces

Interface Declaration Syntax

interface <InterfaceName>[Generics] {
    [export] <methodName>(Arguments): <MethodReturnType> // Syntax of methods
    <EmbeddedInterface> // Syntax of embedded interfaces
    <EmbeddedStructs> // Syntax of embedded structs
    <| Type1 | Type2 | etc> // Syntax of type sets
    // NOTE: Type sets MUST start with `|` 
}

For syntax of Generics and Arguments - Common placeholders throughout documentation

Working Example

The def syntax creates a method. It is covered in a later section. In this context, it is used to implement the interface.

package main

import "fmt" as fmt
import "strings" as strings

interface AcceptableAgeType {
  | Int16 | Int32 | 
}

struct Person {
  export FirstName: String
  export LastName: String 
  export Age: Int16
}

interface Greeter {
  Person
  greet(punctuation: String): String  
}

interface LoudGreeter {
   Greeter
   shout(): String
}

def greet(name: Person, punctuation: String): Unit {
  fmt.println("Greetings " + name.FirstName + " " + name.LastName + punctuation)
}

def shout(name: Person): Unit {
  let uppercaseFirstName = strings.toUpper(name.FirstName)
  let uppercaseLastName = strings.toUpper(name.LastName)
  fmt.println("GREETINGS " + uppercaseFirstName + " " + uppercaseLastName + "!!!!!!!")
}

fun addOneToAge[T: AcceptableAgeType](inputAge: T): T {
  return inputAge + 1 
}

fun greetBothWays(name: Person): Unit {
  fmt.println("Normal greeting:")
  name.greet(".")
  fmt.println("Shouting greeting:")
  name.shout()
}

fun main(): Unit {
  let person = Person{FirstName = "John", LastName = "Doe", Age = 35}
  greetBothWays(person)
  fmt.println("Next year, you'll be:")
  fmt.println(addOneToAge(person.Age))

}

Last updated