RAII is nice for ensuring you don't fail to call cleanup. Normally, I'd implement with a class. I'm currently using Unity and am conscious of generating garbage in Update (even in editor scripting). I was thinking, is creating a struct wrapper for Begin/End actions a good pattern for avoiding allocation? (Since value types don't allocate on the heap.)
Here's an editor scripting example:
public struct ActionOnDispose : IDisposable
{
Action m_OnDispose;
public ActionOnDispose(Action on_dispose)
{
m_OnDispose = on_dispose;
}
public void Dispose()
{
m_OnDispose();
}
}
EditorGUILayout is a Unity type that exposes several functions that need to be called before and after your code. I'd use ActionOnDispose like this:
public static ActionOnDispose ScrollViewScope(ref Vector2 scroll)
{
scroll = EditorGUILayout.BeginScrollView(scroll);
return new ActionOnDispose(EditorGUILayout.EndScrollView);
}
private Vector2 scroll;
public void OnGUI() // called every frame
{
using (EditorHelper.ScrollViewScope(ref scroll))
{
for (int i = 0; i < 1000; ++i)
{
GUILayout.Label("Item #"+ i);
}
}
}
The above simple example works as expected and Dispose is called once for each call to ScrollViewScope, so it appears correct. Unity even provides a scoped struct: EditorGUI.DisabledScope, but not in many cases. So I wonder if there's a downside to this pattern with structs? (I'd assume if the struct is somehow copied the old copy would be disposed and my end action called twice? I don't see such a scenario, but I'm not very familiar with C# value types.)
Edit: I'm specifically asking if whether it matters that I'm using a struct (a value type). Is abusing IDisposable to benefit from “using” statements considered harmful covers whether this is a good use of IDisposable.