Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Well, the problem in my opinion is, that a square is a rectangle, indeed. However, this is not square is-a rectangle;, but rather: square is-a rectangle with rectangle.height == rectangle.width;

This is overall something I have been pondering (and recently planning and implementing) to extend the standard object orientation: adjectives (opposed to 'nouns' (objects) and 'verbs' (methods)).

An adjective would simply describe a noun furhter and simplify a dispatch inside a method depending on the state of an object.

Some pseudosyntax from the current state of ideas would go like this:

  adjective_group rectangle_type(R) {
     if(R.width == R.height) {
       return square;
     } else if(R.width * 4 = R.height * 3) {
       return widescreen;
     }
   }

  draw(square rectangle R) {
     // specialized draw for squares
   }
   draw(widescreen rectangle R) {
     // specialized draw for widescreen rectangles
   }
   draw(rectangle R) {
     // general drawing method for rectangles
   }
This goes pretty deep if one starts to think about it: * Buffers: empty, full, something in between * Abstract syntax trees: well-typed, ill-typed * Accounts: banned, active, moderator, admin

Basically, it simplifies the pattern

  method(object) {
    if (predicate_1(object.state)) {
      // stuff
    } else if(predicate_2(object.state)) {
      // other stuff
    } else {
      // default stuff
    }
  }
I am just currently working on a dispatch mechanism which is at least a bit intuitive :)


Have you ever used a language that makes heavy use of pattern matching? Prolog, in particular, or to a lesser extent (due to increasing amounts of distraction by other novel features) Erlang, OCaml/SML, or Haskell. It sounds like you're essentially describing functional dispatch based on structural attributes or current characteristics, rather than a (generally fixed) object identity.


yes, this is where a large part of the idea comes from.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: