lua-users home
lua-l archive

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

LEGO wrote:
switch as a different construct  makes sense in C because a computed
jump is (much) faster than a chain of if() else if() ... .

It makes little or no sense in languages like lua or perl as they have
both associative arrays and variables can contain references to
subroutines. So syntactically the functionality can be archived by
other means. (see the example in previous mail)

Switch is a nice optimization in C, and it would be very difficult to take advantage of that sort of optimization in Lua. But it is also used for making the code more clear.

But with that said, I consider it to be in a different category from // and !=. It would be nice, but not necessary.

I have been flipping back and forth between Lua and C++ lately and the most painful things are:

- different comment syntax
- ~= vs !=
- 'and' and 'or' rather than '&&' and '||'
- the required 'then' keyword
- Using ':' rather than '.' in function calls

The first 4 are merely syntactic. They may require a few extra lines of code. But they would not change the language functionally at all. The last is extremely painful and really makes it hard to call Lua an "object-oriented" language. I haven't looked into it closely enough, but I have the feeling that it would not be hard to add the ability to mark a function as "member" or not. If it is a member function, the self pointer would always be passed as the first argument, and there would be an implicit self parameter added to the start of the function's argument list. It might look like this:

    function :foo(a,b) = a+b end


    member function foo(a,b) = a+b end

and then you could call it with:

    x = { } = -1,24)

The foo function would have an implicit first self param, and the call would pass x as the first param. The nice thing about this is that it is a superset of today's capability. Only functions marked with the new syntax would do these implicit things. All other functions would work as today.

Personally, I prefer the ':' syntax, since it is more compact. But I understand the desire to make it more readable, as in the second form.

chris marrin                ,""$,          b`    $                             ,,.
                        mP     b'                            , 1$'
        ,.`           ,b`    ,`                              :$$'
     ,|`             mP    ,`                                       ,mm
   ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
  m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
 b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
 ```"```'"    `"`         `""`        ""`                          ,P`
"As a general rule,don't solve puzzles that open portals to Hell"'