Chapter IV · Lua.ex integration / Lesson 24 of 27 Host integration

The ~LUA sigil

You'll learn: Validate Lua at Elixir compile time and pre-compile chunks for hot paths.

~LUA"..." parses your Lua at *Elixir compile time*. A typo crashes mix compile, not your release on a Tuesday. The c modifier emits a pre-compiled %Lua.Chunk{}Lua.eval!(lua, chunk) skips the parser at runtime. Use this for repeatedly executed snippets and config-as-code.

Elixir · your app
Reference only
defmodule Money do
  import Lua

  # Parsed at Elixir compile time; runtime still parses.
  def discount_lua,
    do: ~LUA"return amount * (1 - pct/100)"

  # Parsed AND compiled at Elixir compile time. Re-runs skip
  # the parser entirely.
  def discount_chunk,
    do: ~LUA"return amount * (1 - pct/100)"c
end

lua = Lua.new() |> Lua.set!([:amount], 100) |> Lua.set!([:pct], 15)
{[total], _} = Lua.eval!(lua, Money.discount_chunk())
sigil.lua
Output idle
Hit Run to execute.
Try it: Drop the closing end of an inner function and run. Lua.ex compiles fine here (the playground re-parses every Run), but as a ~LUA body the same typo crashes mix compile.