简体   繁体   中英

How to use this LUA table?

I'am new to LUA and I'am using it to create some Envoy Filters. So, I have found a piece of code with a Table like this :

MyClass = {
  [":path"] = "something"
}

I want to add a contructor to MyClass, so I do this :

function MyObject:new (o, path)
   o = o or {}
   setmetatable(o, self)
   self.__index = self
   self.path = path -- Here is the problem
   return o
end

So, my problem is : How can I access to the [":path"] variable in my contructor to assign a value?

self.path does not work

self.:path does not work

self.[":path"] does not work

This syntax [":foo"] is something I have found nowhere else than in my Envoy sample filter.

Thank you for your help

The dot notation is a syntactic sugar for a complete form.

table.name is equivalent to table["name"] . So in your case it should be self[":path"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM