if transparency > 1 then
warn("transparency > 1")
transparency = 1
end
But I get a warning here. Type number and number? cannot be compared with < opperator. :
if transparency < 0 then
warn("transparency < 0")
transparency = 0
end
After the first checks, the luau type checker should be smart enough to know that transparency is no longer number?, it is now number. I placed an if with: if not transparency then make it 0, like this:
if not transparency then
transparency = 0
end
if typeof(transparency) ~= "number" then
warn("transparency and typeof(transparency) ~= \"number\"")
return
end
Is this a BUG or it is normal behavior? If it is normal behavior, then type cheking (in my opinion) is unrelaiable and I will stop using --!strict with 100% trust now.
This is thw whole function code:
Shared.DeepSetTransparencyOfInstance = function(instance: Instance, transparency: number?)
if not instance then
warn("not instance")
return
end
if typeof(instance) ~= "Instance" then
warn("typeof(instance) ~= \"Instance\"")
return
end
if not transparency then
transparency = 0
end
if typeof(transparency) ~= "number" then
warn("transparency and typeof(transparency) ~= \"number\"")
return
end
if transparency > 1 then
warn("transparency > 1")
transparency = 1
end
if transparency < 0 then
warn("transparency < 0")
transparency = 0
end
if instance:IsA("BasePart") then
instance.Transparency = transparency
end
local descendants = instance:GetDescendants()
for _, descendant in ipairs(descendants) do
if not descendant:IsA("BasePart") then
continue
end
descendant.Transparency = transparency
end
end
I am using it im my whole code base, sometimes works as expected and I like having it on because we are humans and an extra check dosn’t hurt (we all do misstakes), but with more complicated code, it gets in the way instead of helping.
Your problem appears to be a problem with type narrowing. typeof(transparency) ~= "number"should ensure everything after it treats it like a number since it returns early but the type solver hates us so nope!
Anyway this change seems to fix it:
--!strict
Shared.DeepSetTransparencyOfInstance = function(instance: Instance, transparency: number?)
if not instance then
warn("not instance")
return
end
if typeof(instance) ~= "Instance" then
warn("typeof(instance) ~= \"Instance\"")
return
end
if not transparency then
transparency = 0
end
if typeof(transparency) ~= "number" then
warn("transparency and typeof(transparency) ~= \"number\"")
return
end
local transparency: number = transparency
if transparency > 1 then
warn("transparency > 1")
transparency = 1
end
if transparency < 0 then
warn("transparency < 0")
transparency = 0
end
if instance:IsA("BasePart") then
instance.Transparency = transparency
end
local descendants = instance:GetDescendants()
for _, descendant in ipairs(descendants) do
if not descendant:IsA("BasePart") then
continue
end
descendant.Transparency = transparency
end
end
The change I made is shadowing transparency with local transparency: number = transparency.
This fixed this particular case, but still… The type narrowing should be a thing. And also, if the type narrowing is not working, why is not working only for the second if. That;s wierd and I wanted to post this so the Roblox Team can see that they have bugs in the type checking systems. I hate to write extra code for something “useless” like this. That shadow is a ussles line of code. If the type narrowing works well, you should no longer need that shadow variable. But yes, that’s a cool solution. Thanks!
That’s kinda weird - speaking about yourself in third person?
Anyway, is the caller in the room with us right now?
Jokes aside, you’re literally the one using the function. You’re not “protecting” the code from anyone but yourself. typeof isn’t mandatory here - it’s just bloat for a self-contained function.
Also, maybe send one reply instead of two next time.
If the project is made by one dev you are right, I am the only caller. But otherwise, it needs protection. When you work in a team you make the code as bulletproof as possible with warnings for debugging purposes, that’s why I said it is mandatory. Sry for double reply.
No, it’s literally not. You will not get anywhere with this. If you were to write an app like that in C++, for example (without macros), you would’ve been instantly kicked off the team and fired.
No any kind of runtime validation must exist of any kind unless mandatory.