lua-users home
lua-l archive

Re: [PROPOSAL 5.4] alternate way of format/pack/unpack (in the string library)

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I wouldn't say minilanguages are "liked". Just that C++ iostream is even less liked. And some of the concerns seemed frivolous to me. Why is it a problem that format strings can't validate argument types ahead of time when Lua is a dynamic language?
On 5/26/2017 5:14 PM, François Perrad wrote:
An implementation based on a minilanguage doesn't allow user extension,
for example, you cannot add a "%Q" option to `string.format` in pure Lua.
It isn't because format strings are a minilanguange that makes it unextendable. The current implementation delegates interpretation of format strings to the C runtime. A hypothetical Lua library could parse the format strings itself while being extensible. That said, an attempt to make the formatting language more Lua-like I think should take cues from LPEG. Use simple composable objects to define the format. A back-of-the-hand example is:
 xy_format = "x = " .. dec() .. " y = " .. dec()
 print(xy_format.format(10, 20))
 pi_format = "pi = " .. fixed(nil, 4)
 print(pi_format.format(math.pi))
 dec2 = pad(dec(), 2, "0")
 dec4 = pad(dec(), 4, "0")
 date_format = dec2()/"day".."/"..dec2()/"month".."/"..dec4()/"year"
 print(date_format.format(os.date"*t"))
--
tom (telliamed@whoopdedo.org)

AltStyle によって変換されたページ (->オリジナル) /