Lua with Neowin: OOP with Metatables — __index, :new(), Colon Syntax & Inheritance | Episode 30
1views
0012:49
T
Taught by Celeste AI - AI Coding Coach
View on YouTubeDescription
Lua doesn't have classes — but metatables give you everything you need. In this episode we build an object-oriented system from
scratch: __index = self for method lookup, :new() constructors, the colon syntax and self parameter, defining methods, and
building an inheritance chain where child classes extend parent classes.
Every keystroke is shown on screen with 3-second pauses so you can follow along at your own pace.
What You'll Learn:
- __index = self — making a table act as a class
- :new() constructor — creating instances with initial state
- : colon syntax — automatic self parameter for methods
- self — the implicit first argument in method calls
- Inheritance chain — child classes extending parent classes
- Method overriding — replacing parent behavior in children
Student Code: https://github.com/GoCelesteAI/learn-lua-in-neovim/tree/main/episode30
Timestamps:
0:00 Intro
0:12 Preview
0:44 Animal Class — Constructor & Methods
5:16 Run Animal
5:34 Inheritance — Dog Extends Animal
7:52 Dog Test — speak, fetch, describe
10:08 Final Run
10:51 Recap
11:19 Endscreen
Key Takeaways:
1. __index = self makes method lookup follow the prototype chain
2. :new() creates instances by setting the metatable to the class
3. The colon : automatically passes self as the first argument
4. Inheritance works by chaining __index through parent tables
5. Child methods override parent methods — Lua checks the instance first
This is Episode 30 of the Learn Lua in Neovim series. New episodes cover the Neovim Lua API and writing your first Neovim
plugin — all coded inside Neovim.
Taught by CelesteAI. Like & subscribe for more tutorials!
#lua #neovim #programming #tutorial #beginners #learnlua #luaprogramming #neovimtutorial #luaoop #luainheritance #coding