lua-users home
lua-l archive

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

Hash: SHA1

(People who want to talk about *useful* stuff may want to skip this post.)

So I'm working on v0.1 of OLua. It's going pretty well; I have a
recursive descent Lua parser and emitter with OLua extensions, a
framework for transforming code, some runtime, etc.

However, a few irritating realities have cropped up in places where the
Lua idiom for doing something is totally unlike the Objective idiom.
Such as instance variables.

The toy I released a little while ago had methods like this:

@implementation Thing : Object
  - width
    return self._width

The initial problem here is that instance variables and methods occupy
the same namespace --- hence the _ in front of the name. That's pretty
ugly. The second problem is that they're public, which means anyone can
access them without going through the getter; but the third problem, the
biggie, is that you cannot have an instance variable set to nil, because
if you do that then when you read from it the fallback method handler
kicks in and returns you a function that calls object:doesNotRecognise().

So, I'm now looking at a closure-based mechanism. This works much
better. It's faster and the variables are private. Objects are now more
expensive to create, but I can live with that. So now it looks like this:

@implementation Thing : Object
  local width

  - width
    return width

Note that the variables live in a different namespace.

This is good, but introduces a syntax ambiguity:

@implementation Thing : Object
  local width = 4

  - width
    return width
@end parsed as:

  local width = 4 - width
  return width

...which is not good.

So, any suggestions for alternative syntax?

Objective J, which is the same principle applied to Javascript, does this:

@implementation Thing : Object
    int width;

  - width
    return width

The equivalent in Lua would be to have a do...end block containing the
local declarations. But I don't like that, because it would look like
the locals are scoped inside the block, where they're actually visible
to the object methods (but not the class methods). You can get away with
it in Javascript because that language doesn't really have lexical scoping.

Using a special keyword to introduce a instance variable doesn't help,
because the ambiguity occurs at the *end* of an expression, not the
beginning --- I need a keyword to separate the expression from the
method declaration. (Right now I'm just putting a ; in to make it work.)
I could change the method declaration syntax, but I'd rather like to
keep it as traditionally Objectified as possible. I could disallow
initialised instance variables, but allowing that is very convenient and
I don't want to lose it.

So, assuming there's anyone out there who would want to use this
abomination once I get it working, what would you like to see?

- --
┌─── ───── ─────
│ "There is nothing in the world so dangerous --- and I mean *nothing*
│ --- as a children's story that happens to be true." --- Master Li Kao,
│ _The Bridge of Birds_
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla -