lua-users home
lua-l archive

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


Op Sa., 8 Sep. 2018 om 06:24 het Sam Pagenkopf <ssaammp@gmail.com> geskryf:

> It would break everything, but I'd remove the top-level function keyword entirely, with the side effects. 2 more keystrokes for `=` in free functions, plus 4 for `self` in methods.

I never use 'self', but I am not a party OOPer. [1]

In its defence, though:

    'self' is not a keyword.

It is in the same category as 'arg': a pre-assigned non-global
variable with a conventional meaning,

> It would become more clear that function declarations in lua are just assignments.

This is "dieting", i.e. deliberately cutting out sugar. Cutting out
"pairs" and "ipairs" is similar.

Nothing more annoying than having to adjust one's eating habits just
because someone else is dieting, unless you happen to be Saint Paul. I
prefer not to use 'self' and 'pairs', but I would not dream of
removing them from Lua.

On the other hand, unlike Dibyendu and Paige, writing and supporting
dialects of Lua is not my hobby. (Btw Dibyendu, if you need a name for
this stripped-down Lua of yours, how about Deimos? For over 100 years,
it was the smallest known moon in the universe.)

> When I write lua now, I prefer to write ``x.y = function (self, ...)`` for everything that uses a self, because I want to be very clear whether its a free function or a method (prefer free functions!).

This is what I do, except I don't use the word self, I use a name that
tells me what class I expect. If the metatable is called "Deque", the
first parameter in its methods is "deque".

> I don't know if it would warrant removing the `x:y(args)` call syntax,

It won't. That syntax does not need 'self'.

The question I ask is: is the logic of the program easier to follow?
And the one feature of object-oriented programming that I do still
embrace (admittedly only in a platonic way) is that there is at most
one value passed to a method that you are allowed to change. Moving
that value out of the argument list makes a lot of sense.

This syntax comes into its own when 'x' is an expression. Youl'll find
some people saying you should anyway assign that expression to a local
variable, but I do find e.g.

   for k,v in next,template do
      tbl[k]:action(v)
   end

a useful idiom.

-- Dirk

[1] My love affair with "object-oriented" languages like Delphi and
C++ (note that these are about as old as Lua) was never more than a
passing crush; I did not even get past Paragraph 1 of tutorials for
Ada and Java, and I don't know more about Smalltalk than its name.