Labeling print() output results by type

The new capabilities of the output window are a huge help, for example, if you pass a table to the print() function then you will see the content of the table instead of its address. However, in some edge cases, it can lead to some ugly wild goose chases…

Take this code snippet for example:

local CoolPart = workspace.CoolPart
local CoolPartString = "CoolPart"

print(CoolPart, CoolPartString, CoolPart == CoolPartString)

in the output you should see:

CoolPart CoolPart false

As print gives us the Instance’s name, and the string, then tells us that they don’t equal each other… This is a pretty simple piece of code so we know why that is, however, in a more complicated context, it can get pretty confusing when it tells you both variable values are “CoolPart” but are not equal to each other.

if we can have some sort of labeling for the output to know the types of these values, it’d be great for clarity and save some poor souls from these wild goose chases

CoolPart[Instance] CoolPart[String] false[Boolean]

Note: As I can foresee someone suggesting to use type checking, and while I agree with the suggestion, this is meant for people of all background experiences.

7 Likes

Does this really need to be built in?

It doesn’t seem that hard to implement:

-- for non tables use the format: type[content]
-- print(1) --> number[1]
-- print(1,"a") --> number[1] string[a]
-- print(1,"a",true) --> number[1] string[a] boolean[true]
local function tprint(...)
	if select("#",...) == 0 then
		return
	end
	local v = ...
	local t = typeof(v)
	return if t == "table" then v else t.."["..tostring(v).."]",tprint(select(2,...))
end
return function(...)
	return print(tprint(...))
end

If this format is chosen, would print(Vector3.new(1,2,3)) print 1, 2, 3[Vector3]?

This format doesn’t help clarity when working with strings that may have brackets: a[Instance] b[String] could be an instance named a and a string b or a string a[Instance] b.

3 Likes

Wouldn’t this return vector, since its a primitive, or does it still return Vector3 in typeof, sorry haven’t ever tracked that so I know its a stupid question

typeof(Vector3.new()) results in "Vector3", it isn’t possible to change it to return "vector" because of backwards compatibility. The example given uses Boolean and String, so it isn’t trying to do the same thing as typeof (which would return boolean and string).