Fred Mériot
Fred Mériot

Reputation: 4357

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

Upvotes: 2

Views: 68

Answers (1)

Vlad
Vlad

Reputation: 5847

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"]

Upvotes: 3

Related Questions