lua-users home
lua-l archive

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


On Jun 13, 2014, at 11:11 AM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:

>> I guess I do not understand what you mean by your above comments, sorry. :(
> 
> Bad code:
> 
>  if x == 43 then ...
>  elseif x == 67 then ...
>  ...
>  end
> 
> 
> Good code:
> 
>  -- This is somewhere in your code (probably near the top)
>  -- Code for Events
>  MOUSECLICK = 43
>  KEYPRESS = 67
>  ...
> 
>  -- this is somewhere else
>  if x == MOUSECLICK then ...
>  elseif x == KEYPRESS then ...
>  ...
>  end
> 
> (If everything is in the same module, you can declare those names as
> locals.)
> 
> -- Roberto

Okay, so I actually DID understand what you meant and this is an issue I have addressed with another patch, which has yet to be released.

My switch patch paired with my new patch would allow for the following syntax:


const MOUSE_DOWN  1
const MOUSE_MOVE  2
const MOUSE_UP    3

switch and break on event
  case MOUSE_DOWN
    -- code for mouse down
  case MOUSE_MOVE
    -- code for mouse move
  case MOUSE_UP
    -- code for mouse up
end


Without my new patch then yes, my switch statement would require "bad code" as there is no way to use the "constant variables" as case values. However, it is this exact situation which lead me to develop my new patch, which allows for constants, macros and a type of inline function. With my patch the symbolic constants are replaced with the literal values during compile, so MOUSE_DOWN, MOUSE_MOVE, and MOUSE_UP are replaced with 1, 2, and 3 in this example before compilation into bytecode.

I will have more information on my new patch soon, I am just finishing off the test suite and need to write the documentation.

~pmd