Skip to the page
The language / Traits and enums

Start here01

The language02

Programs and the world03

Kiln04

02 The language / 09

Traits and enums

Sharing between classes without inheritance, and fixed sets of values that are always handled in full.

This page covers traits, which let classes share code, and enums, which give a type a fixed set of values. A trait is a set of fields and methods that several classes share, and it's how classes share code, because there is no inheritance. An enum is a type with a fixed set of named values, and because that set is fixed, every when on an enum must handle all of them.

Traits

A trait holds fields and methods that several classes share. A class takes them on with with lines at its top:

traits.hem
trait Timestamped
  readable var updated_at: Int = 0

  def touch(now: Int)
    self.updated_at = now
  end
end

class Post
  with Timestamped

  readable const title: String
end

var post = new Post(title: "Hello")

post.touch(5)
print(post.updated_at) # prints 5
print(post)            # prints Post(updated_at: 5, title: "Hello")

A class gets a copy of the trait's fields and methods, as if they were written where its with line is. Above, Post has an updated_at field and a touch method exactly as if it had declared them itself. So anything a class has that isn't in its own body comes from a trait named at its top, and that's the only place you need to look for it.

Traits are declared at the top level of a file. Like a class, a trait has fields first, then methods. Write one with line per trait, before the class's fields and methods.

The trait's fields work like the class's own: new Post(updated_at: 3) sets a readable or writable one, and its private fields need a default.

The class's own methods can use the trait's fields and methods through self.. It doesn't work the other way round: a trait can only use its own. It knows nothing about the classes that use it, so it can be read and checked on its own.

A name can come from only one place. A class can't take a name from two traits, or reuse a name one of its traits already has:

trait-name-clash.hem
trait Timestamped
  readable var updated_at: Int = 0
end

class Post
  with Timestamped

  var updated_at: Int = 1 # error: 'updated_at' already comes from the trait Timestamped; choose a different name
end

A trait isn't a type: a value's type is the class that uses it. new Timestamped() and items: [Timestamped] are errors. A trait can't have statics either; put them in the classes that use it.

Not this

class Admin < User: there is no inheritance.

Write this

A trait with what the classes share, and a with line for it in each class.

Enums

An enum is a type with a fixed set of named values:

enums.hem
enum Status
  draft
  published
  archived
end

var status = Status.draft

print(status) # prints Status.draft

status = Status.published

print(status == Status.published) # prints true
print("now #{status}")            # prints now Status.published

Declare enums at the top level of a file. The name starts with an uppercase letter; the values start with a lowercase letter, one per line. Always write a value in full, as Status.draft: there is no shorter form.

The enum's name is a type, like a class name, so it goes wherever a type does: Status, Status?, [Status], a parameter status: Status, a field readable var status: Status = Status.draft.

Enum values do little on their own. They only have == and !=, and only with values of the same enum. They have no methods, no order (<), and can't be hash keys. No variable may have an enum's name. Mistakes are caught when the file is checked:

enum-mistakes.hem
enum Status
  draft
  published
end

enum Color
  red
end

print(Status.drafts)             # error: Status has no value 'drafts'; its values are draft, published
print(Status.draft == Color.red) # error: can't compare a Status with a Color; they're never equal

const s = new Status() # error: 'Status' is an enum; its values already exist, so use one, like Status.draft

Choosing by value: when

when chooses what to do by an enum's value:

choosing-by-value-when.hem
enum Status
  draft
  published
  archived
end

const status = Status.published

when status
  is Status.draft
    print("not yet")
  is Status.published, Status.archived
    print("done") # prints done
end

It reads as "when status is draft, ...". The is line listing the value runs. A line may list several values, separated by commas.

That's why there is no else: it would quietly take in a new value, and the places that need to decide what to do with it would never be pointed out. Here the when handles only one of the two values:

hem
enum Status
  draft
  published
end
const status = Status.draft
when status
  is Status.draft
    print("not yet")
end
message
app.hem:6:1: error: this 'when' doesn't handle Status.published; add an 'is' line for it

when only works on enums. For numbers and strings, use if and else if. A value that might be nil (Status?) must be checked first. After is, write the values out, like Status.draft; a variable there is an error.

Because every value is handled, a when whose is lines all end with return returns on every path, and nothing is needed after it:

when-with-return.hem
enum Size
  small
  large
end

def price(size: Size): Int
  when size
    is Size.small
      return 2
    is Size.large
      return 3
  end
end

print(price(Size.large)) # prints 3

when as a value

A when can also give a value, written after then on each is line:

when-as-a-value.hem
enum Status
  draft
  published
end

const status = Status.draft

const label = when status
  is Status.draft then "not yet"
  is Status.published then "done"
end

print(label) # prints not yet
  • Each is line gives one value after then. All of them must have the same type.
  • It can only come right after = or return. To use it anywhere else, store it in a variable first.
  • A when either runs statements or gives a value: every is line has statements below it, or every one has then and a value, never a mix.
  • The values can't contain another when or an inline if.