Gauntlet Documentation
  • 🏠Welcome
  • πŸ…Getting Started
    • 🧠Introduction To Gauntlet
  • ‼️Read before Proceeding
  • ⬇️Installation
  • πŸ‘¨β€πŸ’»VSCode Extension
  • πŸ“šBasics
    • πŸ’¨Running Gauntlet
    • πŸ“„File Setup & Hello World
  • πŸ” Scope Variables
  • πŸ–ΌοΈConstants
  • 🧩Functions
  • ↔️If Statements
  • πŸ”‘Ternary Operator
  • πŸ’ Switch-Case
  • πŸ“©Select-Case
  • ➰Loops
  • πŸ“Structs
  • 🧱Interfaces
  • πŸͺͺAliases
  • πŸ“ŽMethods
  • πŸ¦™Lambdas
  • πŸ•ΈοΈMiscellaneous
  • ⚑Advanced Features
    • πŸ”€When-Cases
    • 🚰Pipes
    • ⁉️Try-Statements
    • 🎭Force-Statements
    • 🌯Wrapper Types
Powered by GitBook
On this page
  • Interface Declaration Syntax
  • Working Example
Export as PDF

Interfaces

PreviousStructsNextAliases

Last updated 8 days ago

Interface Declaration Syntax

interface <InterfaceName>[Generics] {
    [export] <MethodReturnType> <methodName>(Arguments) // 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 -

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 String FirstName
  export String LastName
  export Int16 Age
}

interface Greeter {
  Person
  String greet(String punctuation) 
}

interface LoudGreeter {
   Greeter
   String shout()
}

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

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

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

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

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

}
package main

import fmt "fmt"
import strings "strings"

type acceptableAgeType interface {
	int16 | int32
}

type person struct {
	Age       int16
	LastName  string
	FirstName string
}
type greeter interface {
	greet(punctuation string) string

	person
}
type loudGreeter interface {
	shout() string
	greeter
}

func (name person) greet(punctuation string) {
	fmt.Println("Greetings " + name.FirstName + " " + name.LastName + punctuation)
}
func (name person) shout() {
	uppercaseFirstName := strings.ToUpper(name.FirstName)
	uppercaseLastName := strings.ToUpper(name.LastName)
	fmt.Println("GREETINGS " + uppercaseFirstName + " " + uppercaseLastName + "!!!!!!!")
	// Eliminates any 'unused variable' errors
	_, _ = uppercaseFirstName, uppercaseLastName
}

func addOneToAge[T acceptableAgeType](inputAge T) T {

	return inputAge + 1

}

func greetBothWays(name person) {
	fmt.Println("Normal greeting:")
	name.greet(".")
	fmt.Println("Shouting greeting:")
	name.shout()

}

func main() {
	person := person{FirstName: "John", LastName: "Doe", Age: 35}
	greetBothWays(person)
	fmt.Println("Next year, you'll be:")
	fmt.Println(addOneToAge(person.Age))
	// Eliminates any 'unused variable' errors
	_ = person

}
Normal greeting:
Greetings John Doe.
Shouting greeting:
GREETINGS JOHN DOE!!!!!!!
Next year, you'll be:
36
🧱
Common placeholders throughout documentation