Fusion icon indicating copy to clipboard operation
Fusion copied to clipboard

Add a conditional state object

Open dphfox opened this issue 1 year ago • 3 comments

Partial alternative to #377.

A more specific state object may be introduced for simple conditions:

local animShow = scope:If(show, 0.0, 1.0)

This would allow lazy evaluation of arguments.

dphfox avatar Aug 31 '24 04:08 dphfox

My I recommend the name Switch or Toggle to match other libraries (e.g. Vide has the switch utility)

znotfireman avatar Sep 09 '24 07:09 znotfireman

We generally look to align with Luau, not outside libraries.

As a case study of why we don't do this, we originally aligned Value's naming with React - "State" - and it brought no material benefit. Instead, it just confused everyone, even though ostensibly React was serving UI developers all the same.

So we name things based on Luau as a general universal baseline, rather than imposing knowledge requirements on our users to understand what names mean.

In this case, both "Switch" and "Toggle" sound more like actions or stateful objects. Instead, this object represents a control flow expression. So the name instead tries to mirror Luau's if expression.

dphfox avatar Sep 11 '24 19:09 dphfox

Perhaps this could be used in conjunction with the chaining behaviors described in #414 ?

local animShow = show:If(0.0, 1.0)

It could potentially support other common shorthand things that are in the same vein of conditional and relational operators:

local isEqual = a:Eq(b)
local isLessThan = a:LessThan(b)
etc...

The one major issue I see with potentially doing this is that people might accidentally try and create these inside of computeds like so:

scope:Computed(function(use)
    return if use(a:Eq(b)) then 1 else 0
end) 

Another alternative might be to have an If object allow for chaining specifically to itself in these instances. Perhaps some like:

local result = scope:If(age):LessThan(18):Then("Is Child"):Else("Is Adult")

Raild3x avatar Feb 13 '25 16:02 Raild3x