Lua with Neovim: Type Conversion — Automatic Coercion, Safe Defaults & Explicit Casts | Episode 28
13views
0012:18
T
Taught by Celeste AI - AI Coding Coach
View on YouTubeDescription
Lua is dynamically typed and sometimes converts types automatically — but not always the way you expect. In this episode we
cover explicit conversion with tonumber and tostring, how automatic coercion works (and when it doesn't), the safe default
pattern for invalid input, and formatted output with string.format.
Every keystroke is shown on screen with 3-second pauses so you can follow along at your own pace.
Source code: https://github.com/GoCelesteAI/learn-lua-in-neovim
What You'll Learn:
- tonumber(value) — convert strings to numbers safely
- tostring(value) — convert anything to a string
- Automatic coercion — when Lua converts types for you
- Coercion pitfalls — where automatic conversion fails
- Safe default pattern — handling nil from tonumber
- string.format — precise control over number and string output
Timestamps:
0:00 Intro
0:12 Preview
0:44 Automatic Coercion
1:17 Running — coercion output
1:31 Coercion Failure (pcall)
3:03 Running — failure output
3:17 tonumber()
4:50 Safe Default Pattern (tonumber or 0)
6:25 Running — tonumber output
6:39 tostring()
8:09 string.format for Numbers
9:39 Running — final output
9:52 Review
10:07 Recap
10:39 Endscreen
Key Takeaways:
1. tonumber returns a number or nil if conversion fails
2. tostring converts any value to its string representation
3. Lua auto-coerces strings to numbers in arithmetic
4. Coercion does not work with comparison operators
5. Always check tonumber results — nil means invalid input
This is Episode 28 of the Learn Lua in Neovim series. New episodes cover metatables, OOP, and the Neovim Lua API — all coded
inside Neovim.
Taught by CelesteAI. Like & subscribe for more tutorials!
#lua #neovim #programming #tutorial #beginners #learnlua #luaprogramming #neovimtutorial #luatonumber #luatostring #coding
Tags
lua, lua tutorial, learn lua, lua type conversion, lua tonumber, lua tostring, lua coercion, lua automatic coercion, lua
string.format, lua safe defaults, lua type casting, lua dynamic typing, lua for beginners, neovim lua, neovim tutorial, learn
lua in neovim, lua episode 28, programming tutorial, coding tutorial, beginner programming, lua 2026, neovim 2026, screenkey,
code along, lua intermediate, lua types
Tags
lualua tutoriallearn lualua type conversionlua tonumberlua tostringlua coercionlua automatic coercionlua string.formatlua safe defaultslua type castinglua dynamic typinglua for beginnersneovim luaneovim tutorial