lua-users home
lua-l archive

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


Boy, I'd like to see something like the base#number format in Lua. There's already base 2-36 support via tonumber(). Would anybody else besides me find this useful? I'll post a 5.0.2 patch if there's enough interest!

-joe

On 9/4/05, Chris Pressey <cpressey@catseye.mine.nu> wrote:
On Sun, 4 Sep 2005 15:32:00 +0200
Mike Pall <mikelu-0509@mike.de> wrote:

> [...]
> And I really don't want to write things like this anymore:
>
>   local opcode = op == "movzx" and 4023 or 4031
>
> But rather:
>
>   local opcode = op == "movzx" and 0x0fb7 or 0x0fbf

If you do write a patch for this, may I suggest the following syntax,
borrowed from Erlang (which possibly borrowed it from somewhere else):

  local opcode = op == "movzx" and 16#0fb7 or 16#0fbf

simply because it is far more general and orthogonal - if you want octal
constants you can prefix them with 8#, and binary constants with 2#.
There's no need to remember arbitrary conventions for which prefix goes
with which base, and (FWIW!) you can recognize odd bases like 3# or 9#
just as easily.

(Note that I'm not suggesting # as an infix operator - it's a purely
lexical delimiter... a lot like a decimal point, actually.)

-Chris