Inheritance Without Types

At AlliedModders we dabble in practical language design. Our two major scripting projects, AMX Mod X and SourceMod, iterated on our own fork of a scrappy language called Pawn. Our next iteration is untyped, and we’re adding object support.

What does object support mean? “Well, obviously,” I naively thought, “it means Java-style implementation inheritance!” Whoops. WRONG. It turns out there’s a ton of complexity with inheritance once you take away types. This post is a brief survey of some popular untyped languages and what we ended up deciding.

The Goal
We want our language to have as few pitfalls as possible. I’m not sure if I stole this from Graydon Hoare, but our motto is “No surprises!” We want people to be able to write code that works the way they expect, minimizing random or unforeseen run-time failures.

A great aspect of C++’s inheritance is that information hiding is super easy. A base class’s behavior isn’t trampled by derived classes. For example:

Select All Code:
 

As you’d expect, this prints ‘5’. The fact that Derived also declares x does not trample on Base‘s own behavior. Now let’s take an untyped language, Python 3:

Select All Code:
 

This prints ’20’. In Python, “self” is really one object, with one set of properties and methods, whereas in the C++ model “this” is conceptually two objects, and “this” is statically typed to be the object of the containing class. I consider the Python model unappealing for two reasons:

  1. When adding a member to a derived class, you have to know that you’re not colliding with a member of your base class.
  2. When adding a member to a base class, you might be breaking any number of unknown consumers.

JavaScript’s prototype-based inheritance discourages this style, but JS is super flexible, and if you don’t mind a very, very inefficient prototype chain, you can do this:

Select All Code:
 

This prints “5”.

PHP’s Answer
For whatever reason, PHP takes Java’s model wholesale. Here’s an example:

Select All Code:
 

This prints ‘5’, which is what we want – and makes sense. But is $this statically typed to the Base class, like in C++? No:

Select All Code:
 

This prints "hello", so PHP’s $this retains some dynamicism. Let’s up the ante. What should this do?

Select All Code:
 

This prints true in both cases! For any property access in a PHP class function, if the object has that class on its inheritance chain, it uses the property on that class. Otherwise, it searches from the derived-most class back to the base class like normal. This implicitly hides variables on the derived object. Nonetheless it’s the right choice given the model, especially considering that it’s usually bad practice for a base class’s behavior to explicitly cast to one of its derived class.

Note that the fact that the inner x is private is actually irrelevant. Even if it were public, the base class should retain its behavior. Banning redeclaration works, though then you run the risk of potentially preventing an unknown consumer from compiling (albeit, better than silently being wrong). Similar issues occur with introducing new virtual functions or overloads.

So, what did we do?
After letting this all sink in, I decided to scrap inheritance as a feature of our next language iteration. Having objects alone will be a huge step forward, and we can evaluate use cases from there. I’m satisfied with this for a few reasons.

Inheritance is really complicated with types, and even more complicated without. And in an untyped language, it’s not even clear if implementation inheritance is useful. PHP’s interfaces and abstract classes seem verbose and heavyweight in comparison to JavaScript or Python, in return for a very small amount of static guarantees.

So far Pawn and our derivatives have mainly been embedded in games, and the majority of those games are based on the Half-Life engines (including Source). We want our object model to be compatible with the game’s internal object model, however, I’m not convinced that copying it would be ideal. The Source engine has the “Big-Ass Base Entity Class from which Everything Derives,” a painful and complex architecture continuing Source’s theme of not being scalable. Do we really want to encourage that?

I suspect we’ll end up with something like traits and/or composition, but for now, I’m content with keeping it simple and continuing to evaluate the strengths of other languages’ models.

(Addendum: I don’t much about Perl or Ruby. I tried quickly evaluating them for this post but the syntax was a little intimidating. I’d really appreciate insight into other languages.)

Addendum 2: SaberUK contributes this Ruby example which prints 20 – exhibiting similar behavior to Python.

Select All Code:
 

24 thoughts on “Inheritance Without Types

  1. Powerlord

    So wait, SourcePawn will have objects now? You mean I don’t have to abuse Tries and the like any more? \o/

    Reply
  2. herb trotter

    I must thank you for the efforts you have put in penning this site. I am hoping to check out the same high-grade content by you later on as well. In truth, your creative writing abilities has inspired me to get my own, personal blog now ;)

    Reply
  3. victor spano

    The ability to work from anywhere with an internet connection provides me with greater flexibility while traveling, whether it be to visit family, travel to watch my favorite sports on TV. I’m looking for this kind of flexibilty.

    Reply
  4. victor spano

    They confirmed the olders persona living was known to smoke her entire life, ate a significant amount of chocolate every week and didn’t enjoy sports activities. Isn’t that amazing?

    Reply
  5. jordan 11 gamma blue

    the ability to improveONPut the price of a $67check out around $67 and potential clients will transform away. Once you’re it genuine that the easy gladness on the bestthe special audit of scientific

    Reply
  6. herb trotter

    When it comes to business you want ot be sure that you’ve got all your bases covered. More importantly it seems like you’ve covered the basics in this post.

    Reply
  7. sunil ramlall

    I must thank you for the efforts you have put in penning this site. I am hoping to check out the same high-grade content by you later on as well. In truth, your creative writing abilities has inspired me to get my own, personal blog now ;)

    Reply
  8. Donna James

    I just like the valuable info you provide to your articles. I’ll bookmark your weblog and test once more here frequently. I am reasonably sure I will learn lots of new stuff proper right here! Good luck for the following!

    Reply
  9. Diane James

    you are in reality a good webmaster. The website loading speed is amazing. It seems that you are doing any unique trick. Moreover, The contents are masterpiece. you’ve performed a magnificent process in this subject!

    Reply
  10. Servicio Tecnico Aeg Madrid

    Nunca olvidaré cuando me monté en el helicóptero y, valle abajo, dejé de ver la vertiente sur del Gasherbrum 1. El sentimiento es de dolor, de que ya se acaba, se acaba de verdad, vuelves de alguna manera a la civilización dejando mucho, dejando mucho en el campo base, en la montaña. Pasé un día en Islamabad, cogí un vuelo hasta Qatar y de allí ya a Madrid donde estaban esperando Leire Giarte, Sebastián Álvaro, Mariano Izquierdo, y Hanif, que vinieron a recibirme. Han construido una bota menos técnica de lo que puede ser una Sportiva, una Scarpa pero sin lugar a dudas es una bota caliente.

    Reply
  11. Myrl Puzon

    Los orígenes de la BSH datan en 1983 cuando BSH adquirió la empresa Safel S.A. y posteriormente, en 1989, la marca Balay S.A. Safel era un grupo navarro formado en los años 80 a partir del Grupo Orbaiceta y Balay era un fabricante de electrodomésticos radicado en Zaragoza. En AT18 también somos su solución si necesita comprar recambios originales Bosch en Badalona.

    Reply
  12. Ben Vanhoecke

    magnificent put up, very informative. I ponder why the other experts of this sector do not understand this. You must continue your writing. I’m sure, you have a great readers’ base already!

    Reply

Leave a Reply

Your email address will not be published.