Dialect "modern"
; This main program and its functions share one private scope. Move the Type
; and functions into a Module to expose only the factory and size operation.
Type Buffer
    Private Field bank:Int
    Private Field capacity:Int

    Private Method New(bytes:Int)
        Self.bank = CreateBank(bytes)
        Self.capacity = bytes
    End Method

    Method Delete()
        ; The Buffer owns this bank. Detach before releasing it.
        Local ownedBank:Int = Self.bank
        Self.bank = 0
        If ownedBank <> 0 Then FreeBank ownedBank
    End Method
End Type

Function CreateBuffer:Buffer(bytes:Int)
    If bytes <= 0 Then RuntimeError "Buffer capacity must be positive"
    Return New Buffer(bytes)
End Function

Function BufferSize:Int(buffer:Buffer)
    If buffer = Null Then RuntimeError "Buffer is Null"
    Return buffer.capacity
End Function

Local buffer:Buffer = CreateBuffer(64)
Print "Allocated " + BufferSize(buffer) + " bytes"
Delete buffer
If buffer <> Null Then RuntimeError "Deleted alias should compare as Null"
End
