Skip to content

Latest commit

 

History

History
51 lines (35 loc) · 972 Bytes

README.md

File metadata and controls

51 lines (35 loc) · 972 Bytes

fstring.lua

A Python-like f-string implementation for Lua.

See https://ezhik.jp/f-string.lua for more information and a playground.

f = require("f-string")

local who = "hedgehogs"
print(f"hello {who}!")

Features

You can put any Lua expression into the f-string and it'll hopefully work.

= endings are supported:

> f"{1 + 1 = }"
1 + 1 = 2

Formatting specs support everything in string.format and can be used with :%:

> f"{100:%.2f}"
100.00

Caveats

  • Upvalue access is kinda broken: Variables from outer scopes of functions (upvalues) are only reliably accessible if they are referenced outside the f-string. For example:
local x = "outer scope"
local function inner()
    -- This reference is needed for the upvalue to be accessible in the f-string
    if x then end
    local result = f"x = {x}"  -- Now works correctly
end
  • Uses debug so is probably slow as heck

Tests

make test