Import std.core.interfaces
std.core.interfaces
The Interfaces that operators and string conversion work through.
The built-in types implement the ones that fit them, and a Struct or Class implements one by listing it.
Examples
// A Struct is Hashable, Equatable, and Formattable through its own Methods.
Struct Money Implements Hashable, Formattable
Field Cents: Int
Public Method Hash(): UInt64 => Cents.Hash()
Operator ==(a: Money, b: Money): Bool => a.Cents == b.Cents
Public Method ToString(): String => "{Cents} cents"
Public Method Format(spec: String): String? => spec == "c" ? "{Cents}c" : Null
End Struct
Local prices = {Money(150): "tea"}
Expect(prices.Contains(Money(150)))
Expect("{Money(150)}" == "150 cents" && "{Money(150):c}" == "150c")std/core/interfaces.fb
Types
Equatable
interface
Public Interface Equatable
What == says, and != is always its opposite. A Struct that lists Equatable without an Operator == of its own compares its fields, and a Class compares whether two are the same object.
std/core/interfaces.fb, line 27
-
Whether
aandbare equal.
Comparable
interface
Public Interface Comparable Extends Equatable
An order: what < says, which the other comparisons follow unless a type gives its own.
Examples
Struct Version Implements Comparable
Field Major: Int
Field Minor: Int
Operator <(a: Version, b: Version): Bool
Return a.Major < b.Major || a.Major == b.Major && a.Minor < b.Minor
End Operator
End Struct
Expect(Version(1, 2) < Version(1, 10))
Expect(Version(2, 0) >= Version(1, 9))
Expect(Max(Version(2, 0), Version(1, 9)).Major == 2)std/core/interfaces.fb, line 50
-
Whether
acomes beforeb. -
Whether
acomes afterb. -
Whether
acomes beforebor is equal to it. -
Whether
acomes afterbor is equal to it.
Hashable
interface
Public Interface Hashable Extends Equatable
A number for Maps and Sets to find a value by, the same for values that are equal. A Struct that lists Hashable without a Hash of its own combines its fields', and a Class's comes from its object.
std/core/interfaces.fb, line 65
-
Its hash, the same for values that are equal.
Printable
interface
Public Interface Printable
What a value shows in a string, like "{point}".
Examples
Struct Point Implements Printable
Field X: Int
Field Y: Int
Public Method ToString(): String => "({X}, {Y})"
End Struct
Expect("{Point(1, 2)}" == "(1, 2)")std/core/interfaces.fb, line 84
-
Its text, as a string interpolation like "{value}" shows it.
Formattable
interface
Public Interface Formattable Extends Printable
A value that takes a format spec, the part after the ':' in "{price:.2}" or Format's "{0:>8}": Format gives its text for the spec, or Null when it doesn't take that spec. The numbers, Bool, String, and Enums are Formattable, by the specs the compiler checks.
std/core/interfaces.fb, line 92
-
Its text for a format
spec, like ".2" or ">8", or Null when it doesn't take thatspec.
Numeric
interface
Public Interface Numeric
What arithmetic needs, so generic code like a Sum works on any number.
std/core/interfaces.fb, line 98
-
The number that adding leaves alone: 0.
-
The number that multiplying leaves alone: 1.
-
aandbadded up. -
btaken away froma. -
aandbmultiplied together. -
adivided byb.
Cloneable
interface
Public Interface Cloneable
What makes a copy of itself, which changing one doesn't change the other: arrays and std's collections are Cloneable.
std/core/interfaces.fb, line 115
-
A new value, equal to this one, that changing this one doesn't change.
Defaultable
interface
Public Interface Defaultable
What has a value to start from, for generic code that needs one: the numbers' is 0, Bool's False, String's "", an Enum's its zero, its first value or a @Flags Enum's no flags, and std's collections' is empty. It's DefaultValue, since Default is Select's keyword.
std/core/interfaces.fb, line 123
-
The value to start from, like 0 for a number, "" for a String, or an empty collection.
Sendable
interface
Public Interface Sendable
What can go to another thread, which the compiler decides: numbers, Bools, Strings, Enums, Callbacks, Channels, Mutexes, Atomics, and a Shared Class's objects and Weaks of them, and the Structs, tuples, and fixed arrays whose fields and elements all are, with a T? of any of them. A type can list it, or an Interface that extends it, to have that checked.
Report an issue with this page or with FutureBlitz on GitHub.