Constructors should throw exceptions on invalid arguments given

I remember reading a post that addressed exactly this, however I was unable to find it, so if someone knows which post it was, please do link it. The issue still persists so I decided to bring it up again.

Currently, when you provide invalid arguments to a class constructor, they return “blank identity” instances of that class.

For example, Vector3.new("hi") returns an instance of Vector3 at the origin. An exception should have been thrown because no overload exists that takes a string. It is a common mistake to call it with another Vector3, like Vector3.new(part.Position), which also returns a new vector at the origin, which may be confusing for beginners.

It happens with pretty much every constructor I’ve seen, like BrickColor.new returning Medium stone grey, UDim2.new returning {0, 0}, {0, 0}, etc. The only exception seems to be the Instance.new constructor.

If this is addressed, it would improve the debugging experience for programmers out there who may not know what they did wrong with the arguments.

8 Likes

Since you mentioned BrickColor, do you mean invalid argument as in invalid BrickColor name or invalid type as a whole (like passing a table)?

Both, BrickColor.new({ }) returns medium stone grey, BrickColor.new(coroutine.create(function() end)), BrickColor.new("sup bro") also returns medium stone grey.

I’m getting errors when passing a table or thread.


Instance.new isn’t the only constructor that errors with bad arguments, some examples of DataTypes which error on bad arguments to the constructor: PluginDrag, DockWidgetPluginGuiInfo, ColorSequence, ColorSequenceKeypoint, NumberRange, NumberSequence, NumberSequenceKeypoint, PathWaypoint, Ray, Random, Rect, Region3, Region3int16, TweenInfo, and BrickColor

yeah some don’t actually, was probably thinking of another.

I don’t know what’s the deal but the most common are brick color, vector3, color3

Speaking of Color3.new, it expects arguments [0, 1] but they don’t throw when an argument > 1. It is also a common mistake to confuse it with fromRGB