lua-users home
lua-l archive

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




On Tue, May 5, 2015 at 9:00 AM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> Well, tables are mutable, so having metatables attached on an
> instance of a table makes sense. String are immutable, so I don't
> see the point for them...

More to the point, strings have no notion of "self", or of being
created.  If you run "a".."bc" twice, do you create two different
strings (with two different metatables) or only one? Are they the
same as "abc"?  Does that depend on whether there is a GC between the
execution of those expressions? However you answer those questions,
there will be a lot of drawbacks.

-- Roberto


Thank you Roberto, this is the insight I needed. I was thinking more in 'class' terms: two strings
could be of a different class, one an URL, one a file-handle, for example. One might wish to apply
different methods in such a case. 

I'm now fairly persuaded that in a prototype-based system this makes less sense. Instead we create a table to hold the information we need, even if the only instance variable happens to be a single string.

I don't tend to think of class objects as having a "self", just an identity. In Lua (or Self, presumably) you have to create a literal platonic Animal to have a literal Cat, in other languages you can have an abstract Animal which cannot be instantiated without more specificity. I happen to think prototyping makes more sense (that is, it's easier to reason about), so, thanks again.

cheers,
-Sam.