Skip to content

SilverEzhik/f-string.lua

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Releases

No releases published

Packages

No packages published