GraphEngine icon indicating copy to clipboard operation
GraphEngine copied to clipboard

Auto-initialization of List properties for cells and structs

Open sebdau opened this issue 8 years ago • 1 comments

When I declare such a cell:

cell struct Event { List<CellId> LinkedObjects; }

And I instantiate it: new IfxEvent();

The List-based properties ("LinkedObjects") are null.

To improve readability, can you allow it to become auto-initialized?

sebdau avatar Aug 01 '17 13:08 sebdau

In earlier versions of Trinity (back in 2011 or so) we've had auto-initialized fields. Unfortunately we observed a significant performance impact -- it would be much slower to allocate these class-based cells with a default constructor initializing the fields, versus POD structs which does not allow a default constructor, but uses a single IL instruction to brush it up -- Hence the current design. No much pressure on the heap or GC, but in turn sacrifices the ability to do custom initialization.

In your case, I think a possible workaround is to implement a "initialization factory" like this:

public static class InitFactory
{
    public static New<T>() where T: new
    {
        T t = new T();
        foreach(var fd in typeof(T).GetFields())
        {
            // there are better ways to do it than doing reflection everytime..
        }
    }
}

What do you think?

yatli avatar Aug 03 '17 07:08 yatli