Basics & Core Concepts¶
This section covers the fundamental building blocks of ktox-lua and how Kotlin's basic syntax maps to Lua.
Packages and Imports¶
ktox-lua handles Kotlin packages by transpiling them into directory structures and using require statements in Lua.
Kotlin:
Lua:
When you import a Kotlin class or function from another package, it is transpiled to a standard Lua require call. The transpiler ensures that the module path matches the Kotlin package structure.
Example: import com.example.math.Calculator becomes require("com/example/math/Calculator").
Variables (val vs var)¶
Kotlin's distinction between immutable (val) and mutable (var) variables is preserved during transpilation. Both are transpiled to Lua local variables within the appropriate scope.
Kotlin:
Lua:
Basic Types¶
Kotlin types are mapped to their nearest Lua equivalents:
| Kotlin Type | Lua Type | Notes |
|---|---|---|
Int, Float, Double |
number |
Lua 5.1/5.2 uses doubles for all numbers. Lua 5.3+ supports integers. |
String |
string |
|
Boolean |
boolean |
|
List, Map, Set |
table |
Uses standard Lua tables with helper functions from ktox-lib.lua. |
null |
nil |
Functions¶
Top-level functions in Kotlin become local functions (if private) or global functions (if public) within the generated Lua module.
Kotlin:
fun greet(name: String): String {
return "Hello, $name!"
}
private fun internalCalc(x: Int): Int = x * 2
Lua:
function greet(name)
return "Hello, " .. tostring(name) .. "!"
end
local function internalCalc(x)
return x * 2
end
String Interpolation & Templates¶
Kotlin's string interpolation ("Hello, $name") is transpiled into Lua string concatenation (..) with tostring() calls where necessary to ensure safety.
Kotlin:
val count = 5
val message = "The count is $count"
val lang = "Lua"
val msg2 = "Running on $lang ${version + 0}"
Lua:
local count = 5
local message = "The count is " .. tostring(count)
local lang = "Lua"
local msg2 = "Running on " .. tostring(lang) .. " " .. tostring(version + 0)
Multi-line Raw Strings¶
Raw strings with trimIndent() or trimMargin() are supported. The transpiler escapes newlines and special characters into a standard Lua string literal and passes it to the corresponding ktox_ helper function at runtime.
Kotlin:
Lua:
Control Flow¶
ktox-lua supports standard Kotlin control-flow constructs, including if expressions, when expressions, for loops with ranges, and while loops.
If Expressions¶
In Kotlin, if is an expression, meaning it returns a value. ktox-lua transpiles this into Lua's ternary-like logic ((condition and true_val or false_val)) or an Immediately Invoked Function Expression (IIFE) for more complex branches.
Kotlin:
Lua:
For more complex blocks:
Kotlin:
Lua:
local result = (function()
if sum == 15 then
local x = compute()
return sum + x
else
return 0
end
end)()
When Expressions¶
Kotlin's when expression is a powerful replacement for the traditional switch statement. ktox-lua transpiles when into a series of if-elseif-else statements in Lua.
Kotlin:
Lua:
local color
if id == 1 then
color = "Red"
elseif id == 2 then
color = "Green"
else
color = "Unknown"
end
For Loops and Ranges¶
ktox-lua supports Kotlin's for loops with various range expressions, mapping them to Lua's numeric for loop.
Simple Ranges (..)¶
Kotlin:
Lua:
Until, downTo, and step¶
Kotlin:
// until: 0 to 4 (exclusive of 5)
for (i in 0 until 5) { total += i }
// downTo: 5 down to 1
for (i in 5 downTo 1) { total += i }
// step: increment by 2
for (i in 0..4 step 2) { total += i }
Lua:
-- until
for i = 0, 5 - 1 do
total = total + i
end
-- downTo
for i = 5, 1, -1 do
total = total + i
end
-- step
for i = 0, 4, 2 do
total = total + i
end
Iterating over Collections¶
You can also iterate over lists and maps using the standard Kotlin for syntax.
Kotlin:
Lua:
While Loops¶
while and do-while loops are supported and map directly to Lua's while and repeat-until.
Kotlin:
Lua:
Destructuring Declarations¶
ktox-lua supports destructuring declarations for Pair and Triple (and other types that provide componentN methods).
Kotlin:
Lua: