Skip to content

Records

Isaac Shelton edited this page Apr 8, 2022 · 2 revisions

Records

Records are structures that the compiler will automatically generate a constructor for.

Declaration

Records are declared like structs, except they use the record keyword

record Employee (name String, age int, productivity float)

Usage Example

import basics

record Person (name String, age int)

func main {
    people <Person> List
    people.add(Person("Abram", 10))
    people.add(Person("Blake", 11))
    people.add(Person("Candice", 12))

    each person Person in people {
        printf("%S is %d years old\n", person.name, person.age)
    }
}

Generated Constructor

The generated constructor will:

  • Have the same name as the record
  • Take in one argument for every field of the record
  • Return a value with the type defined by the record

Characteristics:

  • Every argument taken in will be taken in as POD
  • The result value will be marked as POD during construction
  • Management procedures like __assign__ still apply despite the overall result being marked as POD

Example of Automatic Generation:

record MyRecord (a int, b String, c uint, d bool)

is equivalent to

struct MyRecord (a int, b String, c uint, d bool)

func MyRecord(a POD int, b POD String, c POD uint, d POD bool) MyRecord {
    result POD MyRecord
    result.a = a
    result.b = b
    result.c = c
    result.d = d
    return result
}

Structure-ness

Records are still structures, so everything that applies to structures also applies to records.

This includes things like having the ability to directly declare methods on them.

record Human (firstname, lastname String) {
    func getFullname() String {
        return firstname + " " + lastname
    }
}
Clone this wiki locally