Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this talk: http://www.youtube.com/watch?v=hQVTIJBZook, Douglas Crockford clai

ID: 646959 • Letter: I

Question

In this talk: http://www.youtube.com/watch?v=hQVTIJBZook, Douglas Crockford claims that class-based object-orientation can be represented in terms of prototypal inheritance. The construction he gives is something like:

var theObject = function() {
var private1 = ...;
var private2 = ...;
...
return {
    public1: ...,
    public2: ...,
    ...
};
};
He also claims that the converse is not true: prototypal inheritance cannot be in general encoded using class-based constructs only. I have been thinking about it for a while, and it seems to me that both claims are wrong.

The supposed "encoding" of class-based object-orientation is wrong from an operational semantics point of view. In a typical class-based object-oriented language, member variables and functions are known to exist, so they can be directly used. The prototypal "encoding" relies on testing at runtime whether a member is present in an object/hashtable. Ergo, the semantics are different.

Prototypal inheritance actually can be encoded in a class-based object-oriented language.

I will use C++ as an example, but any other class-based object-oriented language could be used.

struct prototypal
{
std::shared_ptr<prototypal> base;
std::unordered_map<std::string, boost::any> members;

boost::any & operator [] (const std::string & key)
{
    auto it = members.find (key);
    if (it == members.end ())
    {
      if (base)
        return (*base) [key];
      else
        throw std::logic_error { "Member not found." };
    }
    else
      return *it;
}
};
Is my analysis wrong? Am I missing something?

Explanation / Answer

Back in the day, there were no C++ compilers. The C++ code you wrote would be turned into C by a program called CFront. Now, obviously C doesn't have OO capabilities in itself, but somehow the C++ constructs were still turned into C code and your C++ programs were compiled and ran.

This is what he's talking about - you can replicate a C++ class by creating a plain struct and populating it with function pointers to the class methods. You can create a set of function pointers that refer to base classes functions too, so all the OO features that are not present in C can be replicated in C.

This function pointer approach is prototyping. Its a form of fundamental programming that might not be as easy as a language that wraps it up so the compiler handles creating these constructs - so a high level language cannot replicate the low-level features simply because it only provides an easy way of doing things and the compiler will mange them down to low-level features that you do not have access to. Conversely, the low-level language can provide the high-level constructs even though you will have a harder time coding them.

(note, this doesn't necessarily apply to C and C++ as C++ still offers the low-level features, but I hope it demonstrates my answer).

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote