Cryptic OOP syntax

classic Classic list List threaded Threaded
107 messages Options
1234 ... 6
Reply | Threaded
Open this post in threaded view
|

Cryptic OOP syntax

André de Leiradella
I'm impressed with the ideas that came up on the subject OOP in Lua. But most (if not all) of them lack a clear syntax in on place or another. I need a simple, clear syntax so users of my apps can extend it without much trouble.

Am I asking for syntatic sugar? Yes, sure! We already have some:

local function f(...)
which translates to
local f; f = function(...)

id.field
which is the same as
id['field']

"self" and ":" are sugar too.

And what about being able to write
a = b *c
instead of
_G['a'] = _G['b'] * _G['c'] ?

The global environment is just a table afterall...

If my users have to write cryptic code just to extend an existing class, code a constructor or call an inherited method, it's time to try another language.

But is there a script language out there that:

1) Has a clear syntax
2) Has a small footprint
3) Doesn't come with lots of modules that you can't remove from the core
4) Has garbage collection
5) Compiles in almost every plataform/compiler
6) Is designed to embeded or is extremely easy to embed
9) Has a nice API
10) Is easy to extend
11) Is (not necessarely only) object oriented

Sure there are some that have almost all those characteristics, and I tried some of them. The main problems are with items 3, 5 and 6. I could't get any of Pike, Ferrite, ElastiC and Ruby to compile under Windows with BCC32.

I know the problem is not with Lua. The problem is maybe with Windows? Why do I insist with Windows? Because it has much more users than all other alternatives.

I love Lua, I want to continue using Lua, but the only reason I'm discussing OOP in Lua is the lack of an alternative. Having a clean workaround would do it, but maybe putting OOP in the core won't make it much bigger and maybe Lua would get a broader audience?

Regards,

Andre de Leiradella



Reply | Threaded
Open this post in threaded view
|

Re: Cryptic OOP syntax

Mark Hamburg-4
What do you mean by "putting OOP in the core"?

Lua already has the polymorphism aspect of OOP fairly well handled.

Inheritance is handled fairly cleanly in most of the OOP implementations for
Lua so long as you don't want to make calls up the inheritance chain (i.e.,
you don't care about super). Super calls can be handled just like in C++ and
Python and those pass OOP muster with at least some people, so even this may
be a non-issue.

Encapsulation is complicated to achieve. There are work arounds (I posted
one to the Wiki this morning), but none of them are perfect. But
encapsulation is arguably more about modularity than it is about OOP. For
example, my ideal answer to encapsulation would probably be to introduce
some form of private key declarations within modules together with ways to
keep them private.

Mark


Reply | Threaded
Open this post in threaded view
|

Re: Cryptic OOP syntax

Mark Hamburg-4
That being said, I do sometimes wish that it were easier to distinguish
between an index access x.y and a method call x:m() in metamethods. That
would be a core change though not necessarily a syntax change.

The encapsulation code I posted today, for example, could benefit from that
knowledge. As it stands, it assumes encapsulated objects are only accessed
via method calls. 

Mark

on 1/26/05 6:41 PM, Mark Hamburg at [hidden email] wrote:

> What do you mean by "putting OOP in the core"?
> 
> Lua already has the polymorphism aspect of OOP fairly well handled.
> 
> Inheritance is handled fairly cleanly in most of the OOP implementations for
> Lua so long as you don't want to make calls up the inheritance chain (i.e.,
> you don't care about super). Super calls can be handled just like in C++ and
> Python and those pass OOP muster with at least some people, so even this may
> be a non-issue.
> 
> Encapsulation is complicated to achieve. There are work arounds (I posted
> one to the Wiki this morning), but none of them are perfect. But
> encapsulation is arguably more about modularity than it is about OOP. For
> example, my ideal answer to encapsulation would probably be to introduce
> some form of private key declarations within modules together with ways to
> keep them private.
> 
> Mark
> 


Reply | Threaded
Open this post in threaded view
|

Re: Cryptic OOP syntax

Mike Pall-50
In reply to this post by André de Leiradella
Hi,

Andre de Leiradella wrote:
> I love Lua, I want to continue using Lua, but the only reason I'm
> discussing OOP in Lua is the lack of an alternative.

Given the goals you state it is your best alternative. It's much less work
to extend Lua than to downstrip anything else.

> Having a clean workaround would do it, but maybe putting OOP in the core
> won't make it much bigger and maybe Lua would get a broader audience?

Well, first a personal opinion: I do not miss anything in Lua as far as
OOP is concerned. Ok, maybe I'm not an OOP power user then. But anyway,
here is the point I want to make:


I think it's unlikely that the Lua authors want to merge a change into
the core that favours any particular style of OOP (or even OOP in general).
You'll notice that ':' and the automatic 'self' are the only two concession
to OOP. They have a minimal impact on the core. Adding them was not costly.
Lua is all about meta-mechanisms, not about forcing something upon its users.

So here is the magic recipe to eventually get something into the Lua core:

1.  Change your own copy of the Lua core for your favourite OOP style.
    Use it and improve it. Publish your changes.

2a. Live with it, make your patches less intrusive and only update to a
    newer version of the core if there is a good reason. Many people do so.

... OR ...

2b. Think about a minimalistic meta-mechanism in the core that allows one
    to do the kind of extensions you require _outside_ of the core.

3.  Code it up, publish your meta-mechanism and discuss it on the list.
    If your changes are generally useful the Lua authors may consider it
    for inclusion into the core.

In your case you are mainly asking for syntactical sugar. This means
changing the parser and/or the compiler. This is one of the things where
the Lua core lacks a proper meta-mechanism.

Simplistic context-free sugar (such as s/@/self./) can be done by
preprocessing the source with a bit of care (quoted strings et al).
You put a wrapper around the script loader and you are done. No changes
to the core required -- everyone is happy.

But this is not a solution for more complex extensions that need some
context (lexical state, scopes, instruction stream).

Unfortunately it seems very hard to add a proper meta-mechanism to
flexibly extend the parser and the compiler. There has been some discussion
on the list about this, but there is no silver bullet solution on the
horizon. Either you don't get enough access to internal context or you
end up rewriting the whole compiler.


So IMHO instead of discussing everyone's favourite OOP style and what
kind of syntactic sugar is needed to support it, I suggest collecting
and categorizing the needed changes to the core. Then invent a good
meta-mechanism that covers most of these cases (and maybe some more)
and implement it.

Looking at the ways other languages have solved this issue or failed
to do so might be a good idea, too.

[Now crawling back into my cave and working on improving coroutines
 with minimal impact on the core. :-) ]

Bye,
     Mike

Reply | Threaded
Open this post in threaded view
|

Re: Cryptic OOP syntax

Thatcher Ulrich
In reply to this post by André de Leiradella
On Jan 27, 2005 at 12:05 -0300, Andr? de Leiradella wrote:
> 
> But is there a script language out there that:
> 
> 1) Has a clear syntax
> 2) Has a small footprint
> 3) Doesn't come with lots of modules that you can't remove from the core
> 4) Has garbage collection
> 5) Compiles in almost every plataform/compiler
> 6) Is designed to embeded or is extremely easy to embed
> 9) Has a nice API
> 10) Is easy to extend
> 11) Is (not necessarely only) object oriented
> 
> Sure there are some that have almost all those characteristics, and
> I tried some of them. The main problems are with items 3, 5 and 6. I
> could't get any of Pike, Ferrite, ElastiC and Ruby to compile under
> Windows with BCC32.

At the risk of veering off topic, have you looked at JavaScript (via
Spidermonkey)?

-- 
Thatcher Ulrich
http://tulrich.com

Reply | Threaded
Open this post in threaded view
|

Re: Cryptic OOP syntax

skaller
In reply to this post by André de Leiradella
On Thu, 2005-01-27 at 14:05, Andrà de Leiradella wrote:

> But is there a script language out there that:
> 
> 1) Has a clear syntax
> 2) Has a small footprint
> 3) Doesn't come with lots of modules that you can't remove from the core
> 4) Has garbage collection
> 5) Compiles in almost every plataform/compiler
> 6) Is designed to embeded or is extremely easy to embed
> 9) Has a nice API
> 10) Is easy to extend
> 11) Is (not necessarely only) object oriented

Felix satisfies all these requirements except (11).

For (2) YMMV: the run time is much smaller than Lua,
but then it generates fully compiled code which can
be larger than bytecode, and also need extra run
time support (eg C++ runtime).

(3) is extreme in Felix. There are not really any built-in data types,
not even int, bool or string, so there's no problem 'removing' them,
since they're not there in the first place.

(9) is difficult, since there is not really any API.
To use a C function you need to declare it, but
it is compile time only:

	fun sin: double -> double;

Choose between Felix and Lua based on the scale of
your application. For a small device, such as
a mobile phone or PDA, choose Lua. For a large
and complex application (such as a serious game)
use Felix.

If you're in the middle or need a mini-language,
use Felix AND Lua together. (Felix comes with
Lua builtin at the moment). From Felix point
of view Lua CAPI is just another library.

On (11): neither Lua nor Felix need OO as much
as OO language because they have other alternative
techniques such as closures and coroutines.
However Felix binds 'seamlessly' to C++ so you can
always use C++ for OO. You can also do this in Lua,
although the bindings are harder to construct.

-- 
John Skaller, [hidden email]
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net




Reply | Threaded
Open this post in threaded view
|

Re: Cryptic OOP syntax

Steve Donovan-2
In reply to this post by André de Leiradella
>>> [hidden email] 01/27/05 05:05AM >>>
>I'm impressed with the ideas that came up on the subject OOP in Lua.
But most (if >not all) of them lack a clear syntax in on place or
another. I need a simple, clear >syntax so users of my apps can extend
it without much trouble.

What would be cryptic about this style?  It's very
straightforward to implement:

  Animal = class()

  function Animal:init(name)
           self.name = name
  end

  function Animal:to_string()
    return self.name..self:sound()
  end

 Dog = class(Animal)

 function Dog:sound()
    return 'bark!'
  end

>Am I asking for syntatic sugar? Yes, sure! 
It's easy to make simple lexical
substitutions, like '@' for 'self.'
If there was some way to flag a function as
being a method, then that explicit ':' could
just be a '.'  There's an interesting
project!

steve d.



Reply | Threaded
Open this post in threaded view
|

Re: Cryptic OOP syntax

Petite Abeille
In reply to this post by Mike Pall-50

On Jan 27, 2005, at 04:09, Mike Pall wrote:

So IMHO instead of discussing everyone's favourite OOP style and what
kind of syntactic sugar is needed to support it, I suggest collecting
and categorizing the needed changes to the core. Then invent a good
meta-mechanism that covers most of these cases (and maybe some more)
and implement it.

I wholeheartedly concur with Mike's "State of the Union" address :)

Lua is just fine as it is right now.

With a touch of creativity, one can shape Lua in any forms one wants to. I personally don't feel the need for any additional syntactical sugar of any sort to achieve my own devious OOP needs and wants.

At the moment, I'm quite happy simply using a combination of technics highlighted by Roberto in Chapter 15.2 and 16.4 of Programming in Lua. That, in addition to a minimal set of organization principals, gives me everything I need from an OOP standpoint: class and instance methods, proper inheritance as well as full encapsulation.

Of course, it would be nice to "formalize" the above packaging conventions one way or another, if only for my own convenience sake. But this doesn't necessarily imply changing anything in Lua itself. At the moment, I'm more looking toward integrating something like LuaLint in my "build" process to automatically check, validate and enforce my own made up OOP conventions in addition to Lua's core syntax itself.

In summary, Lua, "as is", greatly satisfies for my deviant OOP fetishism :))

And now lets all sing along 8^)

http://www.lyricsfreak.com/r/rolling-stones/117852.html

Cheers

--
PA
http://alt.textdrive.com/


Reply | Threaded
Open this post in threaded view
|

Re: Cryptic OOP syntax

Petite Abeille
In reply to this post by André de Leiradella

On Jan 27, 2005, at 04:05, André de Leiradella wrote:

I love Lua, I want to continue using Lua, but the only reason I'm discussing OOP in Lua is the lack of an alternative.

I think this was mentioned several times already, but sometime repetition is good...

Have you looked at Io?

"Io is small prototype-based programming language. The ideas in Io are mostly inspired by Smalltalk (all values are objects), Self, NewtonScript and Act1 (prototype-based differential inheritance, actors and futures for concurrency), LISP (code is a runtime inspectable/modifiable tree) and Lua (small, embeddable)."

http://www.iolanguage.com/About/

Cheers

--
PA
http://alt.textdrive.com/



Reply | Threaded
Open this post in threaded view
|

Re: Cryptic OOP syntax

Klaus Ripke
In reply to this post by Petite Abeille
On Thursday 27 January 2005 10:47, PA wrote:
> I'm more looking toward integrating something like LuaLint
> in my "build" process
BTW: while it should not be a problem to incorporate a customized
copy of the original C implemented compiler as a module,
a compiler implemented in Lua would be a nice toy both
for customizing and just so that Lua can compile Lua ...

I see all the nice work by Rici Lake, but there is no full compiler?

> http://www.lyricsfreak.com/r/rolling-stones/117852.html
but if you try sometimes you just might find ...


Reply | Threaded
Open this post in threaded view
|

Re: Cryptic OOP syntax

Michael T. Richter-3
In reply to this post by Petite Abeille

I love Lua, I want to continue using Lua, but the only reason I'm discussing OOP in Lua is the lack of an alternative.


I think this was mentioned several times already, but sometime repetition is good...

Have you looked at Io?

Io is a very interesting language, but it is, at this point, militantly gcc+bash+a-whole-bunch-of-UNIXisms-dependent. If you're working in a UNIX-like environment, it's simple enough to port (although BSD users keep getting the shaft on occasion). If you're not working in a UNIX-like environment it is, in its current state, verging on the useless.

Me, I'll probably be using Io in many projects. In about two years' time. In its current state it has very little use to me (although a lot of appeal).


Reply | Threaded
Open this post in threaded view
|

Re: Cryptic OOP syntax

David Given
In reply to this post by Petite Abeille
On Thursday 27 January 2005 09:47, PA wrote:
[...]
> Lua is just fine as it is right now.
>
> With a touch of creativity, one can shape Lua in any forms one wants
> to. I personally don't feel the need for any additional syntactical
> sugar of any sort to achieve my own devious OOP needs and wants.

I agree. There's enough functionality there to implement anything you like, 
any way you like. Witness, for example Raffaele Salmaso's extremely 
interesting OO system using closures (so that you can call methods with '.' 
instead of ':'); this gets you pretty much everything you want from an OO 
syntax without needing any core support.

I'd suggest that adding any more hard-coded support for OOisms merely risks 
taking Lua away from its core strengths of being *really small* without 
actually adding much benefit.

A feature enhancement *I'd* prefer to see is to have the byte-code language 
nailed down and properly specified, so I can generate byte-code directly and 
be reasonably sure of having it run; but that's only really because I'm sick 
and twisted.

-- 
+- David Given --McQ-+ 
|  [hidden email]    | "The god of the Old Testament was actually a TRIBE
| ([hidden email]) | OF RENEGADE SPACE CANNIBALS." --- Robert McElwaine
+- www.cowlark.com --+ 

Reply | Threaded
Open this post in threaded view
|

Re: Cryptic OOP syntax

Petite Abeille

On Jan 27, 2005, at 12:17, David Given wrote:

 Witness, for example Raffaele Salmaso's extremely
interesting OO system using closures (so that you can call methods with '.' instead of ':'); this gets you pretty much everything you want from an OO
syntax without needing any core support.

For the record, here is a pointer to Raffaele Salmaso's messages regarding his Object system proposal and _implementation_ :))

http://comments.gmane.org/gmane.comp.lang.lua.general/12344

Cheers

--
PA
http://alt.textdrive.com/


Reply | Threaded
Open this post in threaded view
|

Re: Cryptic OOP syntax

Adrián Pérez
In reply to this post by Michael T. Richter-3

El 27/01/2005, a las 11:29, Michael T. Richter escribió:


I love Lua, I want to continue using Lua, but the only reason I'm discussing OOP in Lua is the lack of an alternative.
[...]
Have you looked at Io?

Io is a very interesting language, but it is, at this point, militantly gcc+bash+a-whole-bunch-of-UNIXisms-dependent.

Io seems interesting to me as well, and regarding OOP I think that the prototypical approach of Io to OOP can be useful in Lua as well. I feel quite comfortable without having classes at all, especially in a scripting language like Lua. So my principal ideas in this picky topic are, as with the rest of Lua, «keep it simple» and «small is beautiful». So I sometimes use code like the following in scripts:

------------ 8< -------------------cut here---------------

-- Clones an object ("self"), using the "additions" table as the -- object itself setting its metatable to index "self" so the
-- inheritance chain is created.
clone = function (self, additions)
  additions = additions or {}
  if (type(self)=="table") then
    setmetatable(additions, { __index = self })
  end
  return additions
end

-- Obtains the prototype of an object ("self").
proto = function (self)
  return rawget(getmetatable(self), "__index")
end

-- Now we can (easily) create the "root" object prototype.
Object = { clone = clone, proto = proto }

-- ...and create new objects
List = Object:clone {
  add = function (self, list)
    for i = 1, list:count() do
      self:append(list[i])
    end
    return self
  end,
  append = table.insert,
  count  = table.getn
}

-- Let's fiddle a bit with a list
l = List:clone { "a", "b", "c" }
l:append("d")
l:append("e")
print(l:count(), l[3])

------------ >8 -------------------cut here---------------

Due to the dynamic properties of the Lua language I think hiding attributes might not be the better approach. One can make use of a car to go from here to there, or to running over someone. I prefer the «if I don't know nothing about it, better I'll leave it untouched». Note that I don't use multiple inheritance... usually it's not needed at all, and when you want to do something similar, you can double-clone:

List = Object:clone {
  -- some code here
}

Stack = Object:clone {
  -- some more code here
}

StackList = List:clone(Stack) -- Magic beans!

--
Adrian Perez
GPUL/CLUG member » http://gpul.org
Keyboard Error: Press F1 to resume.

Attachment: PGP.sig
Description: Mensaje firmado digitalmente

Reply | Threaded
Open this post in threaded view
|

Re: Cryptic OOP syntax

Romulo Bahiense
> StackList = List:clone(Stack) -- Magic beans!

I really like this approach. Most classes will vanish because their only reason to exist is to implement such things.

I code in Object Pascal (Delphi/FPC) and one thing that annoys me is that OP is not strong nor weakly typed: it is fanatically typed (a terrorist, perhaps?).

> I think hiding attributes might not be the better approach

Sometime ago I read a message in this list saying:
    "If you don't want to do something, just don't."

Once again, KISS -- the acronym, not the rock band -- is everything :)


--rb


--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.8.1 - Release Date: 2005-01-27


Reply | Threaded
Open this post in threaded view
|

Re: Cryptic OOP syntax

Javier Guerra Giraldez
On Friday 28 January 2005 3:11 pm, Romulo Bahiense wrote:
>  > I think hiding attributes might not be the better approach
>
> Sometime ago I read a message in this list saying:
>      "If you don't want to do something, just don't."

that's the quote that totally sold me to 'The Lua Way'.
at first i was very sceptical to the syntax ( "end" ??? real programmers use 
{ and } for blocks!!!)  but this single quote deeply resonated and calmed me 
down... long enough to discover the functional programming features, and now 
All Is As It Should Be

> Once again, KISS -- the acronym, not the rock band -- is everything :)

amen

-- 
Javier

Attachment: pgp_KJEXwaSEy.pgp
Description: PGP signature

Reply | Threaded
Open this post in threaded view
|

Re: Cryptic OOP syntax

Petite Abeille

On Jan 28, 2005, at 21:19, Javier Guerra wrote:

Sometime ago I read a message in this list saying:
     "If you don't want to do something, just don't."

that's the quote that totally sold me to 'The Lua Way'.

"If you do not want to access something inside an object, just do not do it." -- Roberto Ierusalimschy, Programming in Lua, 16.4 - Privacy, December 2003
http://www.lua.org/pil/16.4.html

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/


Reply | Threaded
Open this post in threaded view
|

Re: Cryptic OOP syntax

Petite Abeille
In reply to this post by Javier Guerra Giraldez

On Jan 28, 2005, at 21:19, Javier Guerra wrote:

that's the quote that totally sold me to 'The Lua Way'.

"Lua's brilliance lies largely in providing generally applicable mechanisms rather than solutions to specific problems."

"In terms of the three cardinal virtues, Lua ranks laziness above impatience, and impatience above hubris. "

-- Reuben Thomas, Technical Note 0, ????

http://www.lua.org/notes/ltn000.html



Reply | Threaded
Open this post in threaded view
|

RES: Cryptic OOP syntax

André de Leiradella
In reply to this post by skaller
[hidden email] wrote:
> On Thu, 2005-01-27 at 14:05, André de Leiradella wrote:
> 
>> But is there a script language out there that:
>> 
>> 1) Has a clear syntax
>> 2) Has a small footprint
>> 3) Doesn't come with lots of modules that you can't remove from the
>> core 4) Has garbage collection
>> 5) Compiles in almost every plataform/compiler
>> 6) Is designed to embeded or is extremely easy to embed 9) Has a
>> nice API 10) Is easy to extend
>> 11) Is (not necessarely only) object oriented
> 
> Felix satisfies all these requirements except (11).

The documentation shows an example of OOP, but it's very small and don't
cover all aspects of OOP.

> 
> For (2) YMMV: the run time is much smaller than Lua,
> but then it generates fully compiled code which can
> be larger than bytecode, and also need extra run
> time support (eg C++ runtime).

Needing C++ runtime is an issue to me.

> 
> (3) is extreme in Felix. There are not really any built-in data
> types, not even int, bool or string, so there's no problem 'removing'
> them, since they're not there in the first place.  

Great!

> 
> (9) is difficult, since there is not really any API.
> To use a C function you need to declare it, but
> it is compile time only:
> 
> 	fun sin: double -> double;

By API I mean the C functions you call to initialize the language (like
lua_open), call functions etc.

> 
> Choose between Felix and Lua based on the scale of
> your application. For a small device, such as
> a mobile phone or PDA, choose Lua. For a large
> and complex application (such as a serious game)
> use Felix.
> 
> If you're in the middle or need a mini-language,
> use Felix AND Lua together. (Felix comes with
> Lua builtin at the moment). From Felix point
> of view Lua CAPI is just another library.

I need just one language.

> 
> On (11): neither Lua nor Felix need OO as much
> as OO language because they have other alternative
> techniques such as closures and coroutines.
> However Felix binds 'seamlessly' to C++ so you can
> always use C++ for OO. You can also do this in Lua,
> although the bindings are harder to construct.

My customers will be the ones doing OOP to extend the application in
whatever language I present to them. They won't program in C++.

Regards,

Andre



Reply | Threaded
Open this post in threaded view
|

Re: Cryptic OOP syntax

André de Leiradella
In reply to this post by André de Leiradella
>What do you mean by "putting OOP in the core"?
Having native support to OOP in Lua:

class A
	function A(name)
		self.name = name
	end
end

class B extends A
	function B(name)
		super(name)
	end

	function toString()
		return name
	end
end

b = new B('me')
print(b.toString())

>
>Lua already has the polymorphism aspect of OOP fairly well handled.
Yes.

>
>Inheritance is handled fairly cleanly in most of the OOP implementations for
>Lua so long as you don't want to make calls up the inheritance chain (i.e.,
>you don't care about super). Super calls can be handled just like in C++ and
>Python and those pass OOP muster with at least some people, so even this may
>be a non-issue.
Inheritance is what separates structures from classes. You can declare a structure, alloc an instance of it in memory and do things like obj->toString(obj).

>
>Encapsulation is complicated to achieve. There are work arounds (I posted
>one to the Wiki this morning), but none of them are perfect. But
>encapsulation is arguably more about modularity than it is about OOP. For
>example, my ideal answer to encapsulation would probably be to introduce
>some form of private key declarations within modules together with ways to
>keep them private.
I agree it's more about modularity, but it's important in OOP to hide the implementation of the class and, most important, keep users out of trouble. Just telling them to not touch some properties is not enough in my opinion.

>
>Mark

Andre de Leiradella



1234 ... 6