Layouts and sets

A layout describes an instance's class, attributes and children. A set names a list of members, and the check makes every table, loop and registry keyed by it cover them.

Layouts

A layout is an instance's shape: its class, its typed attributes (@Name : T) and its named children. This one, cut down, is a lighting mode's Configuration, with its attributes and a Sky child named Sky:

system layout LightingModeConfig = Configuration {
    @Ambient                    : Color3
    @Atmosphere_Color           : Color3
    @Atmosphere_Density         : number
    @Bloom_Intensity            : number
    @Brightness                 : number
    @ClockTime                  : number
    @UseWhiteBackground         : boolean

    Sky : Sky
}

A child takes braces of its own for its children, nested as deep as you need. Here a Model holds a Model named Camera, which holds a BasePart named Handle:

layout CameraModel = Model {

    Camera : Model {
        Handle : BasePart {

        }
    }
}

Where a layout goes

A layout goes anywhere a type line does:

  • layout X = ... in a script is that script's own, as a plain type is.
  • system layout X = ... in any script of a system is the whole system's, as a system type is. A system has one layout of each name.

An instance's layout

A use of an instance reads as the one layout in scope that fits it: the layout's class is the instance's own, and the instance has the layout's named children and attributes. With two layouts that fit, or none, it stays its class.

layout Toast = Frame {
    Title : TextLabel
}

layout Banner = Frame {
    Title : TextLabel
}

use script.ToastTemplate

local probe : Toast = ToastTemplate
  • ToastTemplate, a Frame with a TextLabel named Title, fits both layouts, so the use reads it as a Frame.
  • A local whose written type names one of the layouts its value fits takes that layout, as probe does: no error, and the made Luau casts the value to it.
  • A type error that wants one layout offers the cast (:: Toast) as its fix. Any other written type keeps the error on the value, with the cast as its fix where one rewrite is right.

A script's children as a table

from turns a script's children into a table, keyed by their names and typed by the type in front of it:

local configs : {[number] : WorldConfig} = from script.Configs

Here WorldConfig is a system layout in the system script, and Configs holds the worlds' Configurations, named by number. from script takes the script's own children, as Modes does in A WHOLE EXAMPLE below.

use each does the same for scripts: it requires every child the path reaches, keyed by its name, and checks each one against the type in front (Files):

local controllers : { each WorldId : WorldController } = use each script.Controllers

Sets

A set is a named list of members. A written one lists them:

system set SceneLocation = "Outside" | "Studio" | "Furniture" | "Rebirth" | "Tech"
  • A string set is a union of its names, so it works as a type anywhere: a parameter, a field, an attribute in a layout, a match subject (Match).
  • A number set is a number to Luau, and ShotScript checks it.
  • An untyped local keeps a set's type when every side of its or, and or if-else is that set, nil or one of its members: after local mode = picked or self.Mode, self.Mode = mode gives no row.
  • system set gives the set to the whole system.
  • system set S : B = ... makes every member of S a B.
  • A « ... » doc on a system set line shows on the set's row in Docs and when you hover its name.

A set is a value too, and a loop walks its members. This one fills a table with one font for each name in the set FontName:

for _, n in FontName do
    regularRegistry[n] = FontRegular.new(FontInfos.Fonts[n],FontInfos.CoverageMaps[FontInfos.Fonts[n].Coverage])
end

Sets from names

A set can come from names you already have. keys of a table takes its keys, here those of the from table above:

system set WorldId = keys of configs

keys of configs.* takes every inner key instead.

keys of a path takes the names of an instance's children, with no from local needed. Modes, a script whose children are lighting modes, makes a set of their names:

system set LightingModeId = keys of script
  • The path starts at script: keys of script is the script's own children, keys of script.Configs a child folder's, and keys of script.Models.* every grandchild's name, once.
  • When every name is a number it is a number set, else a string set. A mix of the two, an empty folder or no such folder is reported by the check.
  • At run time the set's list is read from those children when the script loads. Use from and keys of its table when you want the instances too.

keys of and tags of a type read its fields:

  • system set PlayerStat = keys of PlayerData, where PlayerData is a type: the string set of its field names (a record's in the order written, then its parents'; a class's public fields shared by both sides; an interface's).
  • system set CauseKind = tags of Cause: the string set of a union of records' tags. The tag is the field every shape writes as one string of its own: Type, else the only such field, else the one you name with by (tags of U by Kind). tags of subclasses of C reads a class family.
  • In ShotScript Editor, Go to Declaration on C in tags of subclasses of C reaches class C.
type PlayerData = { Coins : number, Gems : number }
type Cause = { Type : "Untradable" } | { Type : "World", World : string }

system set PlayerStat = keys of PlayerData -- "Coins" | "Gems"
system set CauseKind = tags of Cause       -- "Untradable" | "World"

A type whose fields can't be listed is one error at the type, saying why:

keys of Token: Token is a union, and each of its types has its own fields: write keys of one of them

For tags of, a shape with no tag, two shapes with one tag, or two fields that could each be the tag is one error at the union.

A template set builds one name for each member of another set:

system set WorldName = `World{World}`

Set arithmetic

A set line's right side may be an expression over sets, strings and numbers:

system set World = "Grass" | "Sand" | "Lake" | "Sea"
system set Water = "Lake" | "Sea"
system set Land = World - Water -- "Grass" | "Sand"
  • A | B is every member of either, A & B the members of both, A - B the members of A that B lacks. Brackets ( ) group.
  • & binds before |, and - takes away everything after it up to the next - or ).
  • An unknown set, a mix of number and string sets, a ring of sets that name each other, or a taken-away name the left side lacks is an error. An empty result is a warning.

Open sets

An open set takes its members from any script that reaches it:

system set CheckName = open

set CheckName += "MapEnlarged" -- in any script that reaches CheckName
  • One name added twice is an error on each add.
  • A match over an open set with no else gets a note: a member can turn up from anywhere.
  • A written set and a keys of set take no adds.

Each tables

{ each S : T } is a table with exactly one entry for each member of the set S. A missing one is an error. These two hold a lighting mode for each world in WorldId, the set from SETS FROM NAMES above:

local outsideModes  : {each WorldId : SunLightingMode}  = {
    [1] = Modes.World1Outside,
    [2] = Modes.World2Outside
}

local rebirthModes  : {each WorldId : SunLightingMode}  = {
    [1] = Modes.World1Cave,
    [2] = Modes.World2Temple
}

Add a world under Configs and both tables are errors until each has an entry for it.

A type can name an each table too. Here a table of number suffixes needs one entry for each suffix in its set:

system set TrillSuffix = "K" | "M" | "B" | "T" | "X" | "Z"


system type TrillSuffixInfo = {
    Power   : number,
    Suffix  : TrillSuffix,
    Color   : Color3,
}

system type TrillRegistry = {each TrillSuffix : TrillSuffixInfo}
  • { [WorldId] : SunLightingMode } keeps Luau's meaning: any of the members, or none.
  • A table keyed by a narrower set, one whose members are all in WorldId, fits where a { [WorldId] : SunLightingMode } is wanted. A { each WorldId : SunLightingMode } still needs every member.
  • { each U : T } over a union of records is keyed by its tags (tags of U): local words : { each Cause : string } = { Untradable = "Can't trade", World = "Wrong world" }.
  • { each C in U : T<C> } holds one entry per tag, each typed with that tag's shape.
  • In { each k in S : T }, T may read the key k, so each entry gets its own type. { each p in Property : Props[p] }, over system set Property = keys of Props, is one entry per field of Props, each with that field's type.
  • use script[k] in a type is what child k of the script returns, for the script's use each script. With type WorldControllerMap = { each id in WorldId : use script[id] } and local controllers : WorldControllerMap = use each script, controllers[1] is the class child 1 returns. instance of X is the type of an object made from the class X: a local typed instance of WorldControllerMap[1] takes controllers[1].new().
  • A bound checks each child's class: with local controllers : { each id in WorldId : use script[id] extends BaseController } = use each script, a child whose class doesn't extend BaseController gets a row. controllers[id].new() for any id : WorldId is then a BaseController, and inside if id == 1 then it is child 1's class, so its own members read with no row.
  • A key that is no tag is an error, "Lava is not a tag of SomeCause (its tags are Untradable | World)", and so is a read through a key that may name a tag with no entry: narrow the key first.
  • An each table filled over several lines is checked complete where it leaves your hands: returned, passed on or stored (Classes).

Loops and comprehensions

A loop over a record, a { each C in U : T<C> } table or a table keyed by a set ties each value to its key, so a compare or a match on the key narrows the value:

local data : PlayerData = { Coins = 0, Gems = 0 }
for key, value in data do
    if key == "Coins" then print(value + 1) end
end
  • Comparing the key with a name no key has is an error: "key is never "Coin": PlayerData has no field Coin". Assigning the key is an error too.
  • Past 64 keys the loop is checked untied, with a warning.

A loop over a set is checked once for each name, with the loop's name as that one name. A loop over a set or a record that writes x[n] on every path, with no break or continue before the write, sets every field it names, so the record it fills is complete:

local properties : Properties = {}
for _, propertyName in PropertyName do
    properties[propertyName] = self:_resolveProperty(propertyName, token, defaults)
end
return properties -- complete: the loop sets every PropertyName
  • Writing the set on the loop's name, for _, propertyName : PropertyName in PropertyName do, reads the same. Any other type there, such as string or another set, makes it a plain loop that fills nothing.
  • A loop that names only some fields gets a row saying which.
  • A copy loop over a union into a typed local, for k, v in token do ft[k] = v end, can't be followed key by key. It gets one warning, whose fix writes return with table.clone(token) { ... } (Match, WITH), and Luau's own type errors on the loop fold into it.

A table comprehension builds a table from a loop: { [k] = v for k, v in t }. One over a set is typed as an each table, complete by construction, with no cast. Modes builds one mode for each name in its set:

local modes     : {each LightingModeId : SunLightingMode}       = {[id] = SunLightingMode.new(configs[id]) for _, id in LightingModeId}

Only a comprehension with one for, over the set itself and nothing else, gets that type. One with several fors, or over anything else, is a plain table, and writing it into a { each S : T } table is reported, since it may miss members.

Set tests

x in S is true when x is a member of the set S, and x not in S when it isn't:

system set FilledTokenTag = "Text" | "Icon"
system set StopWord = "a" | "the"

local function fill(token : Token) : FilledToken
    assert(token.Type in FilledTokenTag) -- token is a Text or an Icon from here on
    return token
end

local function say(word : string)
    if word not in StopWord then
        print(word)
    end
end
  • S is a set: a set name, T.X or Scope.X. The test sits at =='s priority.
  • On a name or a path, if, assert, and, or and guard clauses narrow on it as on x == "A" or x == "B".
  • A copy of the tag (local tag = token.Type) tested the same way never narrows token: a warning says so, and its fix tests token.Type itself.
  • Errors: not x in S (it reads as (not x) in S, so write x not in S), a right side that is no set (for a {string}, write table.find(t, x) ~= nil), and a left side that can never be in S.
  • Inside a class that takes <T : Scope>, S may be a bare set of T, as it may be T.X: the test reads T's set when it runs and narrows by the bound's members (Types, SCOPES THAT EXTEND, AND SCOPE PARAMETERS).
  • A for loop's own in is unchanged.

The value type of a key

In a function's types and body, R[k] is the type of R's field named k, where k is a parameter typed by a set of R's keys:

system set PropertyName = keys of Properties

internal _resolveProperty(propertyName : PropertyName, base : Unfilled.Base) : Properties[propertyName]
    local prop : Properties[propertyName]? = base.Properties[propertyName]
    ...
  • A call gets that key's field type: self:_resolveProperty("Scale", base) is a number, and a union key gets the union. The body is checked once for each key, and inside for _, n in PropertyName each n gets its own field's type.
  • Each key's check sees only the code that key can reach. A match case on the key, an if branch on it, and the rest of a block after an if on it that returns are each checked for their own keys alone. So in if propertyName == "Scale" then return 1 end the 1 is checked as Scale's number, and a wrong return is an error for that key only.
  • k must be a parameter typed by such a set. A key typed string, a set name the record lacks, a map in place of a record and the form outside a function are errors, and so is a call with a string that is no member: "Spin" isn't a PropertyName (Scale, AlignY).
  • A write through the same key checks against that key's own value type, with no row about the other keys' types: properties[propertyName] = defaults[propertyName], or, with existing read through the same key, properties[propertyName] = if existing == nil then base.Properties[propertyName] else existing.
  • (value of Properties[propertyName]) is one row: "write the value type of a key as Properties[propertyName]". Its fix writes Properties[propertyName], and the line then checks as above.
  • The made Luau writes any in its place. The check for each key is ShotScript's.

Registries

A registry is a table keyed by a set that ShotScript makes for you, and any script that sees it fills it:

system type ProductCallback = (player : Player) -> boolean
system set ProductName = "MasterBall" | "RareEgg"
system registry Purchases : { each ProductName : ProductCallback }

The system registry line goes at the top of a script of the system. Each script that sells a product registers it:

register Purchases.MasterBall = function(player : Player) : boolean
    return true
end
  • An each registry needs exactly one entry per member and side: a missing or doubled one is an error. Here RareEgg still needs its own register line.
  • { [S] : T } instead of { each S : T } makes a registry where an entry may be missing.
  • An entry is written when its script loads, so a register line adds no require.

A whole example

Modes, a ModuleScript in a lighting system, with one Configuration child for each lighting mode:

system script.Parent.Parent

system set LightingModeId = keys of script

system layout LightingModeConfig = Configuration {
    @Ambient                    : Color3
    @Atmosphere_Color           : Color3
    @Atmosphere_Decay           : Color3
    @Atmosphere_Density         : number
    @Atmosphere_Glare           : number
    @Atmosphere_Haze            : number
    @Atmosphere_Offset          : number
    @Bloom_Intensity            : number
    @Bloom_Size                 : number
    @Bloom_Threshold            : number
    @Brightness                 : number
    @CC_Brightness              : number
    @CC_Contrast                : number
    @CC_Saturation              : number
    @CC_TintColor               : Color3
    @ClockTime                  : number
    @ColorShift_Bottom          : Color3
    @ColorShift_Top             : Color3
    @EnvironmentDiffuseScale    : number
    @EnvironmentSpecularScale   : number
    @ExposureCompensation       : number
    @GeographicLatitude         : number
    @OutdoorAmbient             : Color3
    @UseWhiteBackground         : boolean

    Sky : Sky
}
────────────────────────────────────────────────────────────────────────────────────────────────────────────

use SunLightingMode

────────────────────────────────────────────────────────────────────────────────────────────────────────────

local configs   : {each LightingModeId : LightingModeConfig}    = from script
local modes     : {each LightingModeId : SunLightingMode}       = {[id] = SunLightingMode.new(configs[id]) for _, id in LightingModeId}

────────────────────────────────────────────────────────────────────────────────────────────────────────────

return modes
  • LightingModeId is the set of the children's names, and configs is the children themselves, one for each name.
  • modes builds one SunLightingMode from each config.
  • Add a Configuration under Modes and its name joins the set. modes covers it with no change, since the comprehension walks the set itself.

Listing them

shot sets lists the sets (The shot command).