Skip to content
FutureBlitz

    Install

    Import std.core.load

    std.core.load

    The attributes and Interfaces that change how Load and Save convert a type.

    What Load<T>(value) and Save(x) read: the attributes on a Struct's or Class's Fields that change how they're loaded and saved, and the Interfaces of a type that converts itself. The compiler builds each type's Load and Save, Field by Field, over the Package Functions below. One that can fail puts its ParseError in its last parameter, as a Function that throws does, but without a Throw, so it gets its Place where Load<T> is called.

    std/core/load.fb

    Types

    Rename attribute

    Public Attribute Rename(Name: String)

    The key a Field has in a Value's Object, in place of its name.

    Examples

    Struct Player
        @Rename("player_name")
        Field Name: String
        Field Level: Int
    End Struct
    
    Local saved = Save(Player("Ada", 3))
    Expect(saved.Keys() == ["player_name", "Level"])
    Local loaded = Try Load<Player>(saved)
    Expect(loaded.Name == "Ada" && loaded.Level == 3)

    std/core/load.fb, line 37

    Skip attribute

    Public Attribute Skip

    A Field that Load leaves at its default, and Save leaves out.

    std/core/load.fb, line 39

    Flatten attribute

    Public Attribute Flatten

    A Struct's or Class's Field whose own Fields go in the Object with the others, not in an Object of their own.

    std/core/load.fb, line 42

    Renamed attribute

    Public Attribute Renamed(From: String)

    The key a Field had before, which Load reads when the Object doesn't have its key, so what an older version saved still loads.

    std/core/load.fb, line 45

    Loadable interface

    Public Interface Loadable

    A type that makes itself from a Value, which Load<T> calls in place of filling its Fields.

    Examples

    // A temperature that loads from a number, and saves as one, rather than as an Object.
    Struct Celsius Implements Loadable, Saveable
        Field Degrees: Double
    
        Public Static Method FromValue(value: Value): Celsius Throws ParseError
            Local degrees = value.AsDouble()
            If degrees == Null
                Throw ParseError(ParseErrorKind.WRONG_TYPE, "a temperature is a number")
            End If
    
            Return Celsius(degrees)
        End Method
    
        Public Method ToValue(): Value => Value(Degrees)
    End Struct
    
    Expect(Save(Celsius(21.5)) == Value(21.5))
    Local loaded = Try Load<Celsius>(Value(30))
    Expect(loaded.Degrees == 30.0)

    std/core/load.fb, line 72

    • Static Method FromValue(value: Value): Self Throws ParseError static method #

      Makes one from a Value, as Load does.

    Saveable interface

    Public Interface Saveable

    A type that gives its own Value, which Save calls in place of writing its Fields.

    Examples

    Struct Celsius Implements Saveable
        Field Degrees: Double
    
        Public Method ToValue(): Value => Value(Degrees)
    End Struct
    
    Expect(Save(Celsius(21.5)) == Value(21.5))

    std/core/load.fb, line 90

    • Method ToValue(): Value method #

      Its Value, as Save writes it.

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