Derivable
Derivable is a type that combines a molecule:
type MoleculeThatReturnsAString = () -> stringand a constant:
type Constant = stringinto one. This is what a Derivable looks like internally:
type Derivable<T> = T | () -> TUsage
Say you have a table like so:
type Props = {
count: Molecule<number>
}
local props: Props = { count = flec(0) }
print(peek(props.count))And you want your peek to detect constants as well as state objects. You can do so by doing this:
type Props = {
count: Derivable<number>
}
local props: Props = { count = flec(0) }
print(peek(props.count)) --> 0By setting count's value type to Derivable<number>, we can do something like:
props.count = 0
print(peek(props.count)) --> 0and the code will still work as expected. Pretty neat, huh?
Be mindful of Derivable angle brackets
Consider the following type definitions carefully:
Derivable<Vector3>?This type definition means that it will only accept:
Vector3A molecule that returns a
Vector3objectnil(if the user doesn't specify a property value)
This type is best used for optional properties, where you provide a default value if not specified by the user.
Derivable<Vector3?>This type definition means that it will only accept:
Vector3, ornilA molecule that returns a
Vector3object, ornil
This type works best in situations where the property understands nil as a valid value. The user can set it to nil at any time.