.\" Made by website/plain/build.mjs from website/words/interface.md; change that file. .TH SG-INTERFACE 7 2026-11-28 "SG" "SG reference" .SH NAME interface \- what a type can do; eight are built in and named after the colon, and a program cannot write its own yet .SH SYNOPSIS .TP \fBstruct Name : Copyable, Formattable\fR a struct that takes built-in interfaces .TP \fBenum Name : Formattable, enum Name : byte, Comparable\fR an enum that takes them; with numbers, the integer type comes first .TP \fBCopyable\fR the value is copied where another would be moved .TP \fBClonable\fR \&.clone() gives a copy that owns its own memory .TP \fBFormattable\fR print and $"{value}" write it; your own void format(read this, ref Formatter f) .TP \fBEquatable\fR ==, !=, contains and indexOf; your own bool equals(read this, read T other) .TP \fBHashable\fR a key of a HashMap, an item of a HashSet; the type is Equatable too .TP \fBComparable\fR <, >, sort, binarySearch; always your own int compare(read this, read T other) .TP \fBDisposable\fR your void dispose(ref this) runs where the last owner ends .TP \fBClosable\fR your Result close(this) must be called before the value ends .TP \fBunsafe Sendable\fR the writer's promise that a struct with raw pointers may go to another task: see unsafe .TP \fBinterface Name { ... }\fR refused: interfaces of your own are put off .SH DESCRIPTION An interface says what a type can do. A type takes interfaces by naming them after a colon: \fBstruct Point : Copyable, Formattable\fR\&. Nothing is there unless the type asks for it, so a program never pays for a copy, a comparison or a hash it did not want. A struct that is not \fBFormattable\fR cannot be printed, one that is not \fBEquatable\fR cannot be compared with \fB==\fR, and the error says which interface to add. .PP The language has eight interfaces, and a program cannot add to them: \fBinterface\fR is a word of the language, but an \fBinterface\fR of your own was put off, together with generics and \fBwhere\fR\&. An interface is not a type either: a function cannot take "any \fBFormattable\fR value". Each use is checked where the compiler sees the type, and costs a plain call of that type's method when the program runs. .PP For most interfaces the compiler writes the method from the fields, and each field must be of a type that has the interface too: a \fBFormattable\fR struct is written the way it is built, two \fBEquatable\fR values are equal when their fields are, and \fB\&.clone()\fR clones field by field. A type may write the method itself instead, \fBformat\fR, \fBequals\fR or \fBclone\fR, with the form the table shows. \fBComparable\fR is always written, since an order made from the order of the fields is rarely the one that is meant; an enum with numbers that writes none is ordered by its numbers. A \fBHashable\fR type takes its \fBequals\fR and its hash from its fields for now. The \fBequals\fR the compiler writes is called by name too: \fBa.equals(read b)\fR is \fBa == b\fR\&. The hash it writes serves \fBHashMap\fR and \fBHashSet\fR only and is not called by name, since the number it gives is no part of the language. .PP \fBCopyable\fR has no method: it asks that every field is a number, a \fBbool\fR, a \fBchar\fR or another \fBCopyable\fR type. A \fBstring\fR or a \fBList\fR owns memory on the heap, and copying it means allocating, which is written \fB\&.clone()\fR\&. \fBDisposable\fR and \fBClosable\fR are about the end of a value. \fBdispose\fR is never called by the program; it runs once, where the last owner ends. \fBclose\fR is called by the program, and a local or a parameter left unclosed is a compile error, so the error that closing gives is never lost. Both are for structs for now. .PP A method of the right name does not take the interface: the name after the colon does. A \fBformat\fR in a struct that is not \fBFormattable\fR is a method like any other, and \fBprint\fR does not use it. .SH EXAMPLES .SS "Asking for what a type can do" .RS 4 .nf struct Point : Formattable, Equatable { int x int y } void main() { var a = Point { x = 3, y = 4 } var b = Point { x = 3, y = 4 } print(a) print(a == b) print(a != Point { x = 0, y = 0 }) } .fi .RE .PP .RS 4 .nf Point { x = 3, y = 4 } true true .fi .RE .PP The compiler writes both methods from the fields: \fBprint\fR writes the value the way it is built, and \fB==\fR compares \fBx\fR and \fBy\fR\&. .SS "Copied, or cloned" .RS 4 .nf struct Size : Copyable, Formattable { int width int height } struct Order : Clonable, Formattable { string customer List items } void main() { var small = Size { width = 2, height = 3 } var big = small big.width = 20 print($"{small} {big}") var first = Order { customer = "Ada", items = List { "tea" } } var second = first.clone() second.items.add("cups") print(first) print(second) } .fi .RE .PP .RS 4 .nf Size { width = 2, height = 3 } Size { width = 20, height = 3 } Order { customer = "Ada", items = ["tea"] } Order { customer = "Ada", items = ["tea", "cups"] } .fi .RE .PP \fBvar big = small\fR copies a \fBSize\fR, so both can be used. An \fBOrder\fR holds a text and a list, so it cannot be \fBCopyable\fR; \fB\&.clone()\fR makes a second order with its own text and its own list, and the line shows where the allocation is. .SS "Methods of your own" .RS 4 .nf struct Time : Copyable, Formattable, Equatable, Hashable, Comparable { int hour int minute int compare(read this, read Time other) { if this.hour != other.hour { return this.hour.compare(other.hour) } return this.minute.compare(other.minute) } void format(read this, ref Formatter f) { f.write($"{this.hour:d2}:{this.minute:d2}") } } void main() { var trains = List