• Subject: Re: Why do we have ipairs?
• From: David Demelier <demelier.david@...>
• Date: Wed, 25 Jun 2014 18:45:44 +0200

Le 11 juin 2014 10:30, "Axel Kittenberger" <axkibe@gmail.com> a écrit :
>
> But a switch statement in most script languages (like in _javascript_) is syntactic sugar for if/else! That has always been the argument from the Lua team to why there is no switch statement in Lua. Not like for example in C where it is in fact a computed goto.
>

Unfortunately it still does not explain why there is no continue in Lua but break exists. This is one of my major hate.

>
> On Wed, Jun 11, 2014 at 10:23 AM, Andrew Starks <andrew.starks@trms.com> wrote:
>>
>>
>>
>>
>> On Wed, Jun 11, 2014 at 1:42 AM, Axel Kittenberger <axkibe@gmail.com> wrote:
>>>
>>> Iterators are not bound to for loops.
>>>
>>> for k, v in ipairs( t )
>>> do
>>>     print(k, v )
>>> end
>>>
>>> is syntactic sugar for:
>>>
>>> local it = ipairs( t )
>>> local k, v = it( t, 0 )
>>> while k ~= nil
>>> do
>>>     print(k, v )
>>>     k, v = it( t, k )
>>> end
>>>
>>> is syntactic sugar for:
>>>
>>> local it = ipairs( t )
>>> local k, v
>>> k, v = it( t, 0 )
>>> ::loop::
>>> print(k, v )
>>> k, v = it( t, k )
>>> if( k ~= nil )
>>> then
>>>     goto loop
>>> end
>>
>>
>> I think that in order for one thing to be the "syntactic sugar" of another, they need to do the same thing, not just have the same outcome. A switch statement wouldn't be sugar for an if/else. A "repeat until" loop isn't sugar for a while loop or a for loop. They over lap, and perhaps one can do without the other if at least one is present. They are not sugar.
>>
>>
>> Examples of sugar:
>>
>> local x
>> x = function(...)
>>  --do stuff
>>   return x(...)
>> end
>>
>> --is sugar for
>>
>> local function x(...)
>>  ...
>>
>>
>> `local x:y(args)` is sugar for `local x.y(x, args)`
>>
>> `{ foo = "bar"}` is sugar for `{ ["foo"] = "bar"}`
>>
>>
>>
>> --Andrew
>>
>>
>>
>