Skip to content
FutureBlitz

    Install

    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

    • Operator ==(a: Self, b: Self): Bool operator #

      Whether a and b are 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

    • Operator <(a: Self, b: Self): Bool operator #

      Whether a comes before b.

    • Operator >(a: Self, b: Self): Bool operator #

      Whether a comes after b.

    • Operator <=(a: Self, b: Self): Bool operator #

      Whether a comes before b or is equal to it.

    • Operator >=(a: Self, b: Self): Bool operator #

      Whether a comes after b or 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

    • Method Hash(): UInt64 method #

      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

    • Method ToString(): String method #

      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

    • Method Format(spec: String): String? method #

      Its text for a format spec, like ".2" or ">8", or Null when it doesn't take that spec.

    Numeric interface

    Public Interface Numeric

    What arithmetic needs, so generic code like a Sum works on any number.

    std/core/interfaces.fb, line 98

    • Static Method Zero(): Self static method #

      The number that adding leaves alone: 0.

    • Static Method One(): Self static method #

      The number that multiplying leaves alone: 1.

    • Operator +(a: Self, b: Self): Self operator #

      a and b added up.

    • Operator -(a: Self, b: Self): Self operator #

      b taken away from a.

    • Operator *(a: Self, b: Self): Self operator #

      a and b multiplied together.

    • Operator /(a: Self, b: Self): Self operator #

      a divided by b.

    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

    • Method Clone(): Self method #

      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

    • Static Method DefaultValue(): Self static method #

      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.

    std/core/interfaces.fb, line 132

    Report an issue with this page or with FutureBlitz on GitHub.