Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Child helper function #352

Merged
merged 5 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api-reference/roblox/members/attribute.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<nav class="fusiondoc-api-breadcrumbs">
<span>Memory</span>
<span>Roblox</span>
<span>Members</span>
<span>Attribute</span>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion docs/api-reference/roblox/members/attributechange.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<nav class="fusiondoc-api-breadcrumbs">
<span>Memory</span>
<span>Roblox</span>
<span>Members</span>
<span>AttributeChange</span>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion docs/api-reference/roblox/members/attributeout.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<nav class="fusiondoc-api-breadcrumbs">
<span>Memory</span>
<span>Roblox</span>
<span>Members</span>
<span>AttributeOut</span>
</nav>
Expand Down
54 changes: 54 additions & 0 deletions docs/api-reference/roblox/members/child.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<nav class="fusiondoc-api-breadcrumbs">
<span>Roblox</span>
<span>Members</span>
<span>Child</span>
</nav>

<h1 class="fusiondoc-api-header" markdown>
<span class="fusiondoc-api-icon" markdown>:octicons-workflow-24:</span>
<span class="fusiondoc-api-name">Child</span>
<span class="fusiondoc-api-type">
-> <a href="../../types/child">Child</a>
</span>
</h1>

```Lua
function Fusion.Child(
child: Child
): Child
```

Returns the [child](../../types/child) passed into it.

This function does no processing. It only serves as a hint to the Luau type
system, constraining the type of the argument.

-----

## Parameters

<h3 markdown>
child
<span class="fusiondoc-api-type">
: <a href="../../types/child">Child</a>
</span>
</h3>

The argument whose type should be constrained.

-----

<h2 markdown>
Returns
<span class="fusiondoc-api-type">
-> <a href="../../types/child">Child</a>
</span>
</h2>

The argument with the newly cast static type.

-----

## Learn More

- [Parenting tutorial](../../../../tutorials/roblox/parenting)
2 changes: 1 addition & 1 deletion docs/api-reference/roblox/members/children.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<nav class="fusiondoc-api-breadcrumbs">
<span>Memory</span>
<span>Roblox</span>
<span>Members</span>
<span>Children</span>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion docs/api-reference/roblox/members/hydrate.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<nav class="fusiondoc-api-breadcrumbs">
<span>Memory</span>
<span>Roblox</span>
<span>Members</span>
<span>Hydrate</span>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion docs/api-reference/roblox/members/new.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<nav class="fusiondoc-api-breadcrumbs">
<span>Memory</span>
<span>Roblox</span>
<span>Members</span>
<span>New</span>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion docs/api-reference/roblox/members/onchange.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<nav class="fusiondoc-api-breadcrumbs">
<span>Memory</span>
<span>Roblox</span>
<span>Members</span>
<span>OnChange</span>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion docs/api-reference/roblox/members/onevent.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<nav class="fusiondoc-api-breadcrumbs">
<span>Memory</span>
<span>Roblox</span>
<span>Members</span>
<span>OnEvent</span>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion docs/api-reference/roblox/members/out.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<nav class="fusiondoc-api-breadcrumbs">
<span>Memory</span>
<span>Roblox</span>
<span>Members</span>
<span>Out</span>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion docs/api-reference/roblox/members/ref.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<nav class="fusiondoc-api-breadcrumbs">
<span>Memory</span>
<span>Roblox</span>
<span>Members</span>
<span>Ref</span>
</nav>
Expand Down
35 changes: 34 additions & 1 deletion docs/tutorials/roblox/parenting.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,37 @@ local folder = scope:New "Folder" {
end)
}
}
```
```
!!! tip

If you're using strictly typed Luau, you might incorrectly see type errors
when mixing different kinds of value in arrays. This commonly causes
problems when listing out children.

If you're seeing type errors, try importing the
[`Child` function](../../../api-reference/roblox/members/child)
and using it when listing out children:

```Lua hl_lines="1 6"
local Child = Fusion.Child

-- ... later ...

local folder = scope:New "Folder" {
[Children] = Child {
scope:New "Part" {
Name = "Gregory",
Color = Color3.new(1, 0, 0)
},
scope:Computed(function(use)
return if use(includeModel)
then modelChildren:GetChildren() -- array of children
else nil
end)
}
}
```

The `Child` function doesn't do any processing, but instead encourages the
Luau type system to infer a more useful type and avoid the problem.

21 changes: 21 additions & 0 deletions src/Instances/Child.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--!strict
--!nolint LocalUnused
--!nolint LocalShadow
local task = nil -- Disable usage of Roblox's task scheduler

--[[
Helper function for type checking purposes. Casts the input to a `Child`
type, while constraining the input to be an array of `Child` - this prevents
Luau from erroneously inferring a different array type for the input.
]]

local Package = script.Parent.Parent
local Types = require(Package.Types)

local function Child(
x: {Types.Child}
): Types.Child
return x
end

return Child
1 change: 1 addition & 0 deletions src/Types.luau
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ export type Fusion = {
Hydrate: HydrateConstructor,

Ref: SpecialKey,
Child: ({Child}) -> Child,
Children: SpecialKey,
Out: (propertyName: string) -> SpecialKey,
OnEvent: (eventName: string) -> SpecialKey,
Expand Down
1 change: 1 addition & 0 deletions src/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ local Fusion: Types.Fusion = table.freeze {
Attribute = require(script.Instances.Attribute),
AttributeChange = require(script.Instances.AttributeChange),
AttributeOut = require(script.Instances.AttributeOut),
Child = require(script.Instances.Child),
Children = require(script.Instances.Children),
Hydrate = require(script.Instances.Hydrate),
New = require(script.Instances.New),
Expand Down