# Built-in record type

  • Contract<S> Type, used to declare the contract variable type, S is replaced with the storage type of the current contract

The built-in Contract type code is implemented as follows:

type Contract<S> = {
    id: string,
    name: string,
    storage: S
}

Examples of use are as follows:

type Storage = {
    author_name: string,
    age: int
}
let M: Contract<Storage> = {}
function M:init()
    self.storage.age = 100
    self.storage.author_name = 'glua'
    -- 这里self.id和self.name, self.storage.author_name是字符串类型,self.storage.age 是整数
    
end
return M
  • The Stream type, used as a byte stream type, is a built-in record type, but members of the Stream type can only use colons, not dots, to access member functions

The type signature of the built-in Stream is as follows:

type Stream = {
    pos: () => int,  -- Get the current position in the byte stream
    eof: () => bool, -- Get the end of the byte stream
    current: () => int,  -- Get the current byte (converted to int type), if the reading is over and the current byte cannot be read, return -1
    next: () => bool,    -- If the byte stream has not reached the end, pos advances 1 step and returns true, otherwise returns false
    reset_pos: () => nil,   -- Reorganize the current reading position of the byte stream to the starting position
    size: () => int,        -- Get the length of the byte stream
    push: (int) => nil,     -- Take the last byte of the parameter (converted to C-style char type) and add it to the byte stream
    push_string: (string) => nil  -- Add each byte in the parameter string to the byte stream
}

Examples of use are as follows:

let a1 = Stream()
a1:push(123)    -- For the Stream type, you cannot use a1.push(a1, 123) to access member functions
a1:push_string('abc')
let s1 = a1:size()
pprint("a1 size is ", s1)
pprint('a1 is ', tostring(a1), tojsonstring(a1))
pprint('a1 pos is', a1:pos())

var c = a1:pos()

var s = ''
while c < a1:size() do
    s = s .. tostring(a1:current())
    pprint("a1[", c, "]=", a1:current())  -- Should output a1[0]=123 a1[1]=97 a1[2]=98 a1[3]=99
    c = c + 1
    a1:next()
end