The language / Traits and enums
Start here01
The language02
- 01The basics
- 02Values and numbers
- 03Variables
- 04Calls and expressions
- 05Conditions and loops
- 06Strings
- 07Arrays and hashes
- 08Classes and records
- 09Traits and enums
- 10Blocks
- 11Methods
- 12Imports and exports
- 13Foreign files
- 14Failures
Programs and the world03
- 01Files, input and output
- 02JSON, time, URLs and random numbers
- 03Html and forms
- 04Templates: Ochre
- 05Built-in methods
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:
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 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
endA 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:
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.publishedDeclare 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 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.draftChoosing by value: when
when chooses what to do by an enum's value:
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
endIt 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:
enum Status
draft
published
end
const status = Status.draft
when status
is Status.draft
print("not yet")
endapp.hem:6:1: error: this 'when' doesn't handle Status.published; add an 'is' line for itwhen 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:
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 3when as a value
A when can also give a value, written after then on each is line:
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
isline gives one value afterthen. All of them must have the same type. - It can only come right after
=orreturn. To use it anywhere else, store it in a variable first. - A
wheneither runs statements or gives a value: everyisline has statements below it, or every one hasthenand a value, never a mix. - The values can't contain another
whenor an inlineif.