Design-pattern: linear type-state FILO tree

(Make sure to test this idea if you want to use it,
the implementation example is incomplete and untested
)

Hello :partying_face: ,
just have this cool design pattern for an ui i’m working on.
This uses linear types, Origin, to create an nested tree.
(and conditional conform + type state)

What is great with this, in the context of UI,
is that spaces can nest new spaces for tree top to bottom.
But the order of ‘del’ is bottom to top!
This is perfect for how i want to solve some transience,
as consume is not used as “del” in the ui i’m working on.
(and consume returns T.result_type :tada: )

Example:

    #Root space:
    var first = Instance[O=MutExternalOrigin, Int](unsafe_init=True)
    #First nesting
    var nested = Instance[Int, True](Id("foo"), space_provider=first)
    #Second nesting
    var nested_nested = Instance[Int, True](Id("foo2"), space_provider=nested)
    var nested_nested2 = Instance[Int](space_provider=nested)

    #Deeper nodes depends on outer ones
    #(cannot consume outer space before inner ones)
    nested_nested^.consume()
    nested_nested2^.consume()
    nested^.consume()
    first^.consume()

Implementation example with simple values:

@explicit_destroy
struct Instance[
    O: MutOrigin,//,
    T:AnyType&ImplicitlyDestructible&Movable,
    persistent:Bool = False
]:
    var x:Int
    var ptr: Optional[Pointer[Int,Self.O]]
    def __init__(out self, *, unsafe_init:Bool):
        self.x = 3
        self.ptr=None
    def __init__(
        out self, 
        initial_value: Optional[Self.T]=None,
        *,
        ref[Self.O]space_provider: Instance 
    ) where Self.persistent==False:
        self.x=0
        self.ptr=None
    def __init__(
        out self, 
        id: Id, 
        initial_value: Optional[Self.T]=None,
        *,
        ref[Self.O]space_provider: Instance
    ) where Self.persistent:
        self.x=1
        self.ptr=None
    def consume(deinit self):
        var dummy_value = 1
        # Not sure if needed for indirect access error that ensure proper order of __del__!
        # (if this one have space from value of origin O, value of O cannot consume before this one)
        _ = UnsafePointer(to=dummy_value).unsafe_origin_cast[Self.O]()[]