[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A few Wild and Wooly Proposals, while we're here (was Re: Feature request: userdata slice)
- From: Dirk Laurie <dirk.laurie@...>
- Date: Wed, 26 Aug 2015 21:24:10 +0200
2015-08-24 18:07 GMT+02:00 steve donovan <steve.j.donovan@gmail.com>:
> On Fri, Aug 21, 2015 at 4:04 PM, Coda Highland <chighland@gmail.com> wrote:
>> I think C++11's way of defining new literals is pretty reasonable. You
>> define a suffix operator (which must start with _) and when literals
>> appear with that suffix, it's translated at compile time (in Lua's
>> case, bytecode generation time) to a call to that function
>
> But (and this is crucial) any resulting object creation is not hoisted
> out. For instance, in languages with regexp literals /.../, the
> literal is replaced with a reference to a compiled regexp object.
> They effectively become constants. If I see a 'date literal' like
> D'2015-08-24' in Lua I know that this string will be parsed _each
> time_ - it isn't really a literal.
>
> Code generation for true custom literals would get pretty involved. I
> don't think the prettiness is worth the bother.
Not at all, assuming that Roberto's suggestion of per-value
metatables gets implemented. A user-defined literal would
be implemented as a string with a metatable. It could work
this way:
1. (Acting on William Ahern's criticism of my earlier suggestion
of angle brackets, which could result in situations where the
parser is lead astray) Use an "unexpected symbol" as trigger,
say "!". Everything up to the next whitespace character is the
literal, say !2015-08-24.
2. Check whether the string value "!2015-08-24" has a metatable.
If so, that value is is used.
3. If not, do this before using the value:
--
for _,rule in ipairs(user_defined_literals) do
local pat,action = next(rule)
local result = ("!2015-08-24"):gsub(pat,action)
if result then break end
end
if result then setmetatable("!2015-08-24",result") end
--
- References:
- Re: Feature request: userdata slice, Philipp Janda
- Re: Feature request: userdata slice, 云风 Cloud Wu
- Re: Feature request: userdata slice, Dirk Laurie
- Re: Feature request: userdata slice, 云风 Cloud Wu
- Re: Feature request: userdata slice, Dirk Laurie
- Re: Feature request: userdata slice, Dirk Laurie
- Re: Feature request: userdata slice, Tim Hill
- Re: Feature request: userdata slice, William Ahern
- Re: Feature request: userdata slice, Roberto Ierusalimschy
- Re: Feature request: userdata slice, William Ahern
- A few Wild and Wooly Proposals, while we're here (was Re: Feature request: userdata slice), Sean Conner
- Re: A few Wild and Wooly Proposals, while we're here (was Re: Feature request: userdata slice), Dirk Laurie
- Re: A few Wild and Wooly Proposals, while we're here (was Re: Feature request: userdata slice), Coda Highland
- Re: A few Wild and Wooly Proposals, while we're here (was Re: Feature request: userdata slice), steve donovan