Quession about write DSL like using lua table #91
-
sometimes I dont know what my data will look like, i just write the data directly like this in lua. local item = {
name = "someName",
child = {
name = "someName",
},
}
print(item.child.name) Then I want to write it in nelua, I tried like this: local Item = @record{
name : string,
child: pointer,
}
local item1 = (@Item){
name = "someName",
child = nilptr,
}
local item2 = (@Item){
name = "someName",
child = &item1,
}
-- ???
print(((@Item)$(item2.child)).name) But I dont know how to convert the pointer |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I guess you are looking for ways to emulate inheritance behavior? 🤔 Maybe this example could help? https://github.com/edubart/nelua-lang/blob/master/examples/record_inheretance.nelua |
Beta Was this translation helpful? Give feedback.
-
At this moment, you can use this DSL only on by-value records: local ItemByValueChild = @record{
name: string,
}
local ItemByValue = @record{
name: string,
child: ItemByValueChild
}
local itemByValue: ItemByValue = {
name = 'someName',
child = {
name = 'someName',
}
} Nelua introduces by-value records, and their semantics are different from Lua tables. A by value record is passed copying the whole data structure instead of just a reference; in contrast, Lua tables passes just a reference. My example above had to introduce a second struct, because on a by-value record it's impossible to use it recursively (the record size would be infinite). What you really want to have the semantics of passing record references (like Lua tables), is the following: require 'allocators.default' -- to use `new`
local Item = @record{
name: string,
child: *Item,
}
local a: *Item = new((@Item){
name = 'asd',
child = new((@Item){
name = 'hello',
child = nilptr,
})
}) Unfortunately at this time you have to explicitly call You can make the above example less boilerplate by creating a function to create a new Item, like the following: require 'allocators.default' -- to use `new`
local Item = @record{
name: string,
child: *Item,
}
function Item.new(item: Item): *Item
return new(item)
end
local a: *Item = Item.new{
name = 'someName',
child = Item.new{
name = 'someName',
child = nilptr,
}
} |
Beta Was this translation helpful? Give feedback.
-
Thank You all for the answers, the |
Beta Was this translation helpful? Give feedback.
At this moment, you can use this DSL only on by-value records:
Nelua introduces by-value records, and their semantics are different from Lua tables. A by value record is passed copying the whole data structure instead of just a reference; in contrast, Lua tables passes just a reference. My example above had to introduce a second struct, because on a by-value record it's impossible to use it recursively (the record size would be infinite).
What you really want to have t…