Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What exactly is a 'wrapper'?

I kind of have an idea of what it is, only because of code I've looked at with wrapper in the title. However, I'm really not exactly sure what it is. It seems like a way to make proxy methods that interact with an original value, but that would be a complete guess.

According to past content I've seen, would this random example I made qualify as a wrapper? This would just be a function that adds a method called PrintName to an object.

function wrap(obj)
	local new = {}

	-- The custom method
	function new:PrintName()
		print(obj.Name)
	end

	-- Returning default methods / properties
	local t = setmetatable(new,{
		__index = function(t,k)
			local v = obj[k]
			if type(v) == 'function' then
				return function(_,...)
					v(obj,...)
				end
			else
				return obj[k]
			end
		end
	})

	-- Return it
	return t
end

local p = wrap(Instance.new("Part",workspace))
p:PrintName() -- call added method
p:Destroy() -- use built-in method

I've provided code similar to this before, however the question was a lot different. I would just like to know if this qualifies as a wrapper, or if there's a lot more to it than that.

If anyone could explain this concept, or even give an example, I'd really appreciate it. Thanks for reading!

1 answer

✓ Accepted answer

A wrapper, well "wraps" around an object, so it references to the existing object; whenever you need to use the value, it uses the wrapper rather than the existing object. As far as I know, you're right in saying it's a way to make proxy methods interact with an original value.

A person I for sure know who understands wrapper's to the brink is a robloxian called eLunate. I would get into contact with her to ask her some questions.

This discussion is preserved as it was. New answers, comments, and votes are closed.