I’m working on the following code and I couldn’t figure out why Studio was underlining the enum.
function checkHumanoids()
map(game.Workspace, function(i)
if (i.className == "Humanoid") then
installGui(i.Parent)
i.DisplayDistanceType = HumanoidDisplayDistanceType.None
end
end)
end
It turns out that it needs to be Enum.HumanoidDisplayDistanceType.None
Is there a reason Roblox just doesn’t define all enums as globals so that I don’t need “Enum.”?
A problem with defining all enums as globals is that the potential of coming up with a variable name that conflicts with the name of an existing global would increase significantly for anyone who uses PascalCase. Some notable ones are Mode, KeyCode, Axis, RenderPriority, InputType, etc.
Yeah I thought of this but didn’t realize we had enums with such high collision potential.
Maybe this can be revisited when Roblox adds types to Lua. I’d really like to get better code complete on some of these enums.
In C# if I typed i.DisplayDistanceType = code complete would add HumanoidDisplayDistanceType. for me and then pop up a combo box with the three possible options.
In Roblox Lua, I very rarely have a typed Humanoid instance. I’m always manipulating something I found with :FindFirstChild()
In a typed environment, there wouldn’t be any pollution as far as code complete is concerned, since Enums are never on the left hand side of an expression.
A type system doesn’t mean your environment isn’t polluted. I feel like the example might just be pointing out a flaw of the specific C# library/system you were using instead of a Lua necessity. Namespacing is key to having clean and unambiguous code.
Having all enums defined together in a giant bag named Enum isn’t that clean either
Whenever I type “Enum.” I have to wade through them all.
The Humanoid related Enums could at least be defined within Humanoid so that I could type “Humanoid.DisplayDistanceType.None” and some future code complete system could infer the “Humanoid.DisplayDistanceType.” for me.
I prefer the current system over defining it globally. Whenever I use an enum, it ensures I am deliberately using it. Additionally, it makes autocomplete a breeze.
I don’t think typing “Enum.WhateverEnumYouWant” is that big a pain that a solution is needed. Cohesion-wise maybe, but I feel like the pros outweigh the cons.
I don’t think Lua does type coercion from strings to enums. Even if it did, I wouldn’t do that because you lose typing. That’s so bad I don’t even think Javascript does that.