one more (stupid) idea ,,,

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

one more (stupid) idea ,,,

Ulrich Schmidt
I like programming using OOP and it works quite well in Lua.
I don't miss anything related in lua, OK.

But sometimes when coding class methods and entering class names again
and again, i feel a wish comes up:

Would it be possible to implement a shortcut usable inside class
methods, so i can write
   :method instead classname:method
and
   .field instead classname.field.

?

Ulrich.

Reply | Threaded
Open this post in threaded view
|

Re: one more (stupid) idea ,,,

Ulrich Schmidt
Wrong Idea. should be:
>    :method instead self:method
> and
>    .field instead self.field

Reply | Threaded
Open this post in threaded view
|

Re: one more (stupid) idea ,,,

Javier Guerra Giraldez
In reply to this post by Ulrich Schmidt
On 19 April 2016 at 11:58, Ulrich Schmidt <[hidden email]> wrote:
> Would it be possible to implement a shortcut usable inside class
> methods, so i can write
>   :method instead classname:method
> and
>   .field instead classname.field.

just use a shorter variable name as an alias:

---------- myverylongclassname.lua -----

local MyVeryLongClassname = class(....)
local C = MyVeryLongClassname

function C:method(...)
   .....
end

C.field = xxxxxx

....

return MyVeryLongClassname
------------------------------------------



--
Javier

Reply | Threaded
Open this post in threaded view
|

Re: one more (stupid) idea ,,,

Ulrich Schmidt
My Question was wrong, sorry. See my self reply.
   : should become replaced by self:
and
   . should become replaced by self.

Ulrich

Am 19.04.2016 um 13:05 schrieb Javier Guerra Giraldez:

> just use a shorter variable name as an alias:
>
> ---------- myverylongclassname.lua -----
>
> local MyVeryLongClassname = class(....)
> local C = MyVeryLongClassname
>
> function C:method(...)
>     .....
> end
>
> C.field = xxxxxx
>
> ....
>
> return MyVeryLongClassname
> ------------------------------------------
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: one more (stupid) idea ,,,

Luiz Henrique de Figueiredo
In reply to this post by Ulrich Schmidt
> >   :method instead self:method
> >and
> >   .field instead self.field

When you define methods, use the full syntax and choose a one-letter
variable instead of self:

        function MyClass.myMethod(O, ...)
                return O:f() + O.n
        end

but call methods with the colon syntax:

        myObject:myMethod(...)

Your proposal is ambiguous syntax:
        a = b
        :method()
is the same as
        a = b:method()

Reply | Threaded
Open this post in threaded view
|

Re: one more (stupid) idea ,,,

Ulrich Schmidt
I see. This is the only one point i assume, was the wrong design
decision: Omit the explicit statement delimitter ";"

I like the clean and simple syntax and i am with you being cautious
implementing new more specialized language features. But requiring
a explicit command delimiter ";" would open the possibility to
implement some tiny and useful syntax extensions. We need to update
existing sources, but this should be easy to do. And the readability of
sources becomes better and easier to understand. And.. and .. and... :)

Ulrich.

Am 19.04.2016 um 13:11 schrieb Luiz Henrique de Figueiredo:

>>>    :method instead self:method
>>> and
>>>    .field instead self.field
>
> When you define methods, use the full syntax and choose a one-letter
> variable instead of self:
>
> function MyClass.myMethod(O, ...)
> return O:f() + O.n
> end
>
> but call methods with the colon syntax:
>
> myObject:myMethod(...)
>
> Your proposal is ambiguous syntax:
> a = b
> :method()
> is the same as
> a = b:method()
>

Reply | Threaded
Open this post in threaded view
|

Re: one more (stupid) idea ,,,

Ulrich Schmidt
Or much more simple solution:
Stick on the mandatory ";", And if someone want to use my proposed
shortcut he HAVE to write a ";" between the previours statement and the
shortcut.So nothing changes for the existing sources.

Ulrich.

Am 19.04.2016 um 13:30 schrieb Ulrich Schmidt:

> I see. This is the only one point i assume, was the wrong design
> decision: Omit the explicit statement delimitter ";"
>
> I like the clean and simple syntax and i am with you being cautious
> implementing new more specialized language features. But requiring
> a explicit command delimiter ";" would open the possibility to
> implement some tiny and useful syntax extensions. We need to update
> existing sources, but this should be easy to do. And the readability of
> sources becomes better and easier to understand. And.. and .. and... :)
>
> Ulrich.
>
> Am 19.04.2016 um 13:11 schrieb Luiz Henrique de Figueiredo:
>>>>    :method instead self:method
>>>> and
>>>>    .field instead self.field
>>
>> When you define methods, use the full syntax and choose a one-letter
>> variable instead of self:
>>
>>     function MyClass.myMethod(O, ...)
>>         return O:f() + O.n
>>     end
>>
>> but call methods with the colon syntax:
>>
>>     myObject:myMethod(...)
>>
>> Your proposal is ambiguous syntax:
>>     a = b
>>     :method()
>> is the same as
>>     a = b:method()
>>
>

Reply | Threaded
Open this post in threaded view
|

Re: one more (stupid) idea ,,,

Luiz Henrique de Figueiredo
In reply to this post by Ulrich Schmidt
> I see. This is the only one point i assume, was the wrong design
> decision: Omit the explicit statement delimitter ";"

If you want this in your version of Lua, you could add a token filter
that replaces '; .' with '; self.' and the same for `:`.

See http://lua-users.org/lists/lua-l/2008-01/msg00524.html for a similar
but simpler solution to the same problem of avoiding writting 'self'.

Reply | Threaded
Open this post in threaded view
|

Re: one more (stupid) idea ,,,

Ulrich Schmidt


Am 19.04.2016 um 13:45 schrieb Luiz Henrique de Figueiredo:
>> I see. This is the only one point i assume, was the wrong design
>> decision: Omit the explicit statement delimitter ";"
>
> If you want this in your version of Lua, you could add a token filter
> that replaces '; .' with '; self.' and the same for `:`.
>
It sound to me like it is a simple change. I don't want live on my own
isle, so creating my "own" lua version is no option for me. It would
prevent sharing my code with others.

But, if it is easy to implement and in case other coder like my idea,
why not do it? (Sure, if i am the only one who request this feature,
it make no sense, to implement it.)

Ulrich.

> See http://lua-users.org/lists/lua-l/2008-01/msg00524.html for a similar
> but simpler solution to the same problem of avoiding writting 'self'.
>

Reply | Threaded
Open this post in threaded view
|

Re: one more (stupid) idea ,,,

Dirk Laurie-2
2016-04-19 13:52 GMT+02:00 Ulrich Schmidt <[hidden email]>:

> It sound to me like it is a simple change. I don't want live on my own
> isle, so creating my "own" lua version is no option for me. It would prevent
> sharing my code with others.
>
> But, if it is easy to implement and in case other coder like my idea,
> why not do it? (Sure, if i am the only one who request this feature,
> it make no sense, to implement it.)

The mere fact that it took you six posts to explain your idea,
using it wrongly several times, proves that it is confusing and
not worth implementing even for your own use.

Reply | Threaded
Open this post in threaded view
|

Re: one more (stupid) idea ,,,

Ulrich Schmidt
May be i am wrong but your Answer sounds very arrogant. English is not
my native language. And describing a idea awkward does not mean, the
idea is wrong.

But is stop here, before the discussion becomes hot.

Ulrich.

Am 19.04.2016 um 13:55 schrieb Dirk Laurie:
>
> The mere fact that it took you six posts to explain your idea,
> using it wrongly several times, proves that it is confusing and
> not worth implementing even for your own use.
>

Reply | Threaded
Open this post in threaded view
|

RE: one more (stupid) idea ,,,

John Hind
In reply to this post by Ulrich Schmidt
> | Message: 6
> | Date: Tue, 19 Apr 2016 12:58:31 +0200
> | From: Ulrich Schmidt <[hidden email]>
> |
> | I like programming using OOP and it works quite well in Lua.
> | I don't miss anything related in lua, OK.
> |
> | But sometimes when coding class methods and entering class names again
> | and again, i feel a wish comes up:
> |
> | Would it be possible to implement a shortcut usable inside class
methods,
> | so i can write
> |    :method instead classname:method
> | and
> |    .field instead classname.field.
> |
> | ?
> |
> | Ulrich.

Do you mean simply that:

':method'  would be a syntax shortcut for 'self:method'

And

'.field' would be a syntax shortcut for 'self.field'

?

Beyond that you are opening a (very familiar) can of worms. Lua does not
have the concept of 'class' and this concept can be implemented in many
different ways, so 'classname' could be a table or a userdata or in my
personal favourite style, a function closure. So there is no determinate
lexical context for a class - it all depends how you want to implement it.



Reply | Threaded
Open this post in threaded view
|

Re: one more (stupid) idea ,,,

Soni "They/Them" L.
In reply to this post by Luiz Henrique de Figueiredo


On 19/04/16 08:45 AM, Luiz Henrique de Figueiredo wrote:
>> I see. This is the only one point i assume, was the wrong design
>> decision: Omit the explicit statement delimitter ";"
> If you want this in your version of Lua, you could add a token filter
> that replaces '; .' with '; self.' and the same for `:`.
>
> See http://lua-users.org/lists/lua-l/2008-01/msg00524.html for a similar
> but simpler solution to the same problem of avoiding writting 'self'.
>
Alternatively[1]:

local function with(t, e)
   return setmetatable({}, {__index=function(t,k) return t[k] or e[k] end, __newindex = t})
end


do local _ENV = with(self, _ENV)
   -- do stuff that relies on the internal environment here, all
assignments are done on self and _ENV is used as a fallback.
   f(x) -- basically self.f(self.x)
   f(self) -- self is a local, so it doesn't pull from self
   f(with) -- with is a local, so it doesn't pull from self
   print("test") -- prints "test" unless `self.print` exists
   -- etc
end
-- do stuff that relies on the external environment here

[1] - Inspired by http://lua-users.org/lists/lua-l/2016-03/msg00080.html

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: one more (stupid) idea ,,,

Etiene Dalcol
In reply to this post by John Hind
My two cents in this discussion is that I get your point that a shorter syntax nicer to write.
However, it makes it less readable for the future programmer.
I don't think Lua is too verbose so it is a real annoyance, it has quite the right balance, and I wouldn't like to see readability damaged. 

2016-04-19 14:13 GMT+02:00 John Hind <[hidden email]>:
> | Message: 6
> | Date: Tue, 19 Apr 2016 12:58:31 +0200
> | From: Ulrich Schmidt <[hidden email]>
> |
> | I like programming using OOP and it works quite well in Lua.
> | I don't miss anything related in lua, OK.
> |
> | But sometimes when coding class methods and entering class names again
> | and again, i feel a wish comes up:
> |
> | Would it be possible to implement a shortcut usable inside class
methods,
> | so i can write
> |    :method instead classname:method
> | and
> |    .field instead classname.field.
> |
> | ?
> |
> | Ulrich.

Do you mean simply that:

':method'  would be a syntax shortcut for 'self:method'

And

'.field' would be a syntax shortcut for 'self.field'

?

Beyond that you are opening a (very familiar) can of worms. Lua does not
have the concept of 'class' and this concept can be implemented in many
different ways, so 'classname' could be a table or a userdata or in my
personal favourite style, a function closure. So there is no determinate
lexical context for a class - it all depends how you want to implement it.






--
Etiene Dalcol

Software Engineering student at ENSTA Bretagne and PUC-Rio
Sailor Developer http://sailorproject.org
Lua Ladies Founder 
http://lualadies.org

Reply | Threaded
Open this post in threaded view
|

Re: one more (stupid) idea ,,,

Eike Decker
In reply to this post by Soni "They/Them" L.


2016-04-19 14:15 GMT+02:00 Soni L. <[hidden email]>:


On 19/04/16 08:45 AM, Luiz Henrique de Figueiredo wrote:
I see. This is the only one point i assume, was the wrong design
decision: Omit the explicit statement delimitter ";"
If you want this in your version of Lua, you could add a token filter
that replaces '; .' with '; self.' and the same for `:`.

See http://lua-users.org/lists/lua-l/2008-01/msg00524.html for a similar
but simpler solution to the same problem of avoiding writting 'self'.

Alternatively[1]:

local function with(t, e)
  return setmetatable({}, {__index=function(t,k) return t[k] or e[k] end, __newindex = t})
end


do local _ENV = with(self, _ENV)
  -- do stuff that relies on the internal environment here, all assignments are done on self and _ENV is used as a fallback.
  f(x) -- basically self.f(self.x)
  f(self) -- self is a local, so it doesn't pull from self
  f(with) -- with is a local, so it doesn't pull from self
  print("test") -- prints "test" unless `self.print` exists
  -- etc
end
-- do stuff that relies on the external environment here

[1] - Inspired by http://lua-users.org/lists/lua-l/2016-03/msg00080.html

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.




If you let your methods return "self", you can do the : method call thing, it's sometimes used like this:

struct "my_struct"
 : int "x"
 : int "y"
 : string "name

In this example, "struct" is a function returning the struct object and its member functions "int" and "string" that declare variables return the same object. 

However I don't find that writing "self" before a function call or such a thing is wasting my time. And as Soni wrote, you can use the environment table to quick access values (albeit, when doing a function call, you'll have to pass "self" as first argument anyway, therefore it only safes you from writing ":"). 
As for field access, I'd assign often used members to variables instead


local x,y = self.x, self.y